I have a simple recursive factorial program. The main point of this exercise is to handle exceptions. We've been imposed with the limitations of having no negative numbers and no number larger than 12.
{
int factorial(int);
int number = 0;
string s;
while(number != -1)
{
cout << "Please enter a number 1 - 12 " <<endl;
cout << "or -1 to quit the program: ";
try{
cin >> number;
if (number < 0 || number > 12)
throw number;
else
cout << number << " factorial is: " << factorial(number)<<"\n" << endl;
}catch(int number)
{
cout<< "Your number violates the rules; " <<number<<" is either negative or greater than 12.\n"<<endl;
}
}
}
int factorial(int number) {
int temp;
if(number <=1)
return 1;
temp = number * factorial(number - 1);
return temp;
}
My error handling seems like it's working fine, and it's a new concept to me. However, I'd like to do a better job of handling errors. For example, when I type in anything that's not a number, like say a "p" the program starts an infinite loop. How could I code to check and make sure that it's indeed a number a user is putting in?
Thank you.
Add another condition to test the input. For example you can do this.
...
try{
cin >> number;
if (!cin) {
//error msg here
break;
} else if (number < 0 || number > 12)
...
Related
When I run the program for an even number less than 5, It prints out the error message but it also gives me the factorial. I don't want the factorial. Here's the code, how do I correct it?
#include <iostream>
using namespace std;
int main()
{
int number, factorial = 1;
cout << "Enter an even number between 5 & 15 to find it's factorial: ";
cin >> number;
if(number % 2 == 0) {
while(number < 5) {
cout << "Error! Enter an even number greater than five: ";
break;
}
while(number > 15) {
cout << "Error! Enter an even number less than fifteen: ";
break;
}
for(int a = 1; a <= number; a++) {
factorial = factorial * a;
}
cout << "factorial of " << number << " is " << factorial;
}
else {
cout << "Error!Enter an even number between 5 & 15 to find it's "
"factorial: ";
}
return 0;
}
This answer shows some corrections to be in your code and provide a working piece of code.
Corrections:
Not a very good idea to write everything in the main function. Try to follow SOLID principles where S stands for single responsibility. Which tells that functions must be small and do exactly one thing. And in your code, you have defined a very big function which does a lot of things.
Avoid nested loops. In your example, you have an if , inside which you have while, while and inside while you have for. This looks messy and is not readable.
Try to be clear in your expression of code, such that when a person reads they understand the flow of data.
Break is something that comes out of a while loop, but it does not mean it will exit the function. So in your example,
while(number<5){
std::cout<<"Error! Enter an even number greater than five: ";
break;
}
After it comes out of this loop, it will go to the next line which is another while loop and then goes on to calculate the function.
Try to use uniform initialization. Instead of int i = 0 use int i{0}. You can read more about uniform initialization on google.
Try not to use "using namespace std or anything else". Very bad practice.
Working piece of code:
bool check_value(int number){
int range = (number < 5 || number > 15) ? false : true;
int even = (number % 2 == 0) ? true : false;
if(range & even){
return true;
}
else{
std::cout << "Error! Please input even number between 5 & 15\n";
return false;
}}
int main(){
int number,factorial=1;
std::cout<<"Enter an even number between 5 & 15 to find it's factorial: \n";
std::cin >> number;
if(check_value(number)){
for (size_t a{1}; a <= number; a++){
factorial = factorial*a;
}
std::cout<<"factorial of " << number <<" is " << factorial << "\n";
}
return 0;
}
As Karl pointed out in the comments, the break statement will break out of exactly one loop, in this case a while loop, and the code outside of the loop will continue to execute.
Also, as Damien pointed out, you need to use a long long int to compute the factorial since the result can exceed the max size of the int datatype.
You could also reformulate your code to make it more readable, something like this:
#include <iostream>
/* Checks if number is valid and can throw error msg */
bool isValidNumber(int num) {
if(num % 2 != 0 || num < 5 || num > 15) {
std::cout << "Error! The number entered is not valid." << std::endl;
return false;
}
return true;
}
/* Calculates factorial and prints its value */
void calculateFactorial(int num) {
long long int factorial = 1;
for(int a = 1; a <= num; a++) {
factorial = factorial * a;
}
std::cout << "factorial of " << num << " is " << factorial << std::endl;
}
int main()
{
int number;
bool isValid;
do {
std::cout << "Enter an even number between 5 & 15 to find it's factorial: ";
std::cin >> number;
isValid = isValidNumber(number);
if(isValid) {
calculateFactorial(number);
}
} while(!isValid);
return 0;
}
Note: Using using namespace std; is considered bad practice because of the possibility of method collisions between a method or methods in the std namespace and methods of some other nanmespace you might create down the road.
I just started learning c++ and is learning topics related to cin, cin.get. In one of my assignments, the requirement is this:
Write a program that reads in a list of integer numbers and prints the largest and the
smallest number.
You can assume that the the user's input is always valid.
You can assume that the numbers in the list are separated by one space character and
that the character following the last number in the list is the newline character .
Implement a loop in which the above actions are repeated until the user requests to quit.
The code I came up with is:
using namespace std;
int main()
{
char ch = ' ';
int max=0;
do
{
int x;
cin >> x;
ch = cin.get();
if (max = 0) { max = x; };
if (x > max) { max = x; };
} while (ch != '\n');
cout << "maximum=" << max << endl;
return 0;
}
I was expecting to have this return the maximum of the numbers in the list. But it turned out only to return the last integer in the list.
I also don't quite get why the line:
cin >> x;
ch = cin.get();
makes the program able to accept a list of numbers. Isn't cin suppose to ask the user to input some stuff, as well as cin.get? In another word, shouldn't the user encounter input two times? But why is it when I run I am only asked to input once?
After some adjustment using the comments in this post, I was able to come up with the code as such:
int main()
{
cout << "Enter the list of integer numbers: ";
char ch = ' ';
int max=0;
int min = 0;
do
{
int x;
cin >> x;
ch = cin.get();
if (max == 0) { max = x; };
if (x > max) { max = x; };
if (min == 0) { min = x; };
if (x < min) { min = x; };
} while (ch != '\n');
cout << "maximum=" << max << endl;
cout << "minimum=" << min << endl;
return 0;
My final question is: How can I satisfy the requirement of this assignment "Implement a loop in which the above actions are repeated until the user requests to quit."
Your initial problem lies with the code:
if (max = 0) { max = x; };
The single = is assignment, not comparison. What is does is set max to zero, then use that zero as a truth value, which always equates to false.
Hence the effect is that max will be set to zero, and the body of the if will never be executed.
You need to use == for comparison.
In terms of looping until user indicates they want to quit, you can use the fact that invalid input can be detected with the >> extraction operator.
As an aside, since extraction of an integer first skips white-space, you don't have to worry about handling spaces and newlines at all, they'll naturally be skipped as part of the "get next integer" operation.
By way of example, the following complete program will exit when you enter q (or any invalid number for that matter, though if you enter 47q, it will handle the 47 first before exiting on the q):
#include <iostream>
int main () {
int inVal;
while (true) {
std::cout << "Enter number (q to quit): ";
if (! (std::cin >> inVal)) break;
std::cout << inVal << '\n';
}
std::cout << "Done\n";
}
A sample run follows:
Enter number (q to quit): 1
1
Enter number (q to quit): 2
2
Enter number (q to quit): 3
3
Enter number (q to quit): 55
55
Enter number (q to quit): 42
42
Enter number (q to quit): q
Done
So you can use that to detect non-numeric input and act accordingly:
#include <iostream>
int main () {
int inVal, maxVal, minVal, firstTime = true;
while (true) {
std::cout << "Enter number (q to quit): ";
if (! (std::cin >> inVal)) break;
if (firstTime) {
firstTime = false;
minVal = maxVal = inVal;
} else {
if (inVal > maxVal) {
maxVal = inVal;
}
if (inVal < minVal) {
minVal = inVal;
}
}
}
if (firstTime) {
std::cout << "*** You didn't enter any numbers.\n";
} else {
std::cout << "*** Minimum was " << minVal
<< ", maximum was " << maxVal << ".\n";
}
}
Some sample runs of that:
pax:~> ./testprog
Enter number (q to quit): q
*** You didn't enter any numbers.
pax:~> ./testprog
Enter number (q to quit): 1
Enter number (q to quit): q
*** Minimum was 1, maximum was 1.
pax:~> ./testprog
Enter number (q to quit): 9
Enter number (q to quit): 8
Enter number (q to quit): -3
Enter number (q to quit): 7
Enter number (q to quit): 5
Enter number (q to quit): 42
Enter number (q to quit): q
*** Minimum was -3, maximum was 42.
I'm assuming here the allowed assumption that the user input is always valid applies to the allowed characters being input (such as "no 47q allowed"), and that, if the marker complains that you're meant to stop on the first newline, you can argue that any number after that newline constitutes invalid data, and is therefore an invalid test case.
In the real world, you'd write your code robustly enough to handle edge cases like that but I suspect it's not necessary for educational work (even if it may earn you some extra marks).
I've been working on a program that calculates the mean of the user's inputs. I couldn't figure out yet, what to use for the input checker. I can't use arrays or strings yet. How do I check that both inputs are numerical values? And if they are not; how do I ask again for the correct input?
#include <iostream>
using namespace std;
int main()
{
// Get number from user
int input = 0;
double accumulator = 0;
double mean;
cout << "How many numbers would you like me to average together?\n";
cin >> input;
if (input >= 0){ //to check if input is a numerical value
// Compute and print the mean of the user input
int number = 1;
double x;
while (number <= input) //while corrected
{
cout << "Please type a numerical value now: \n";
cin >> x;
if (x < 0 || x > 0){ //to check if x is a numerical value
accumulator = accumulator + x;
}
else {
cout << "Input incorrect"<< endl;
}
number = number + 1;
}
mean = accumulator / input; // formula corrected
cout << "The mean of all the input values is: " << mean << endl;
cout << "The amount of numbers for the average calculation is: " << input << endl;
}
else {
cout << "Input incorrect"<< endl;
}
return 0;
}
You can use cin.fail to check for errors. Note that if user inputs a number followed by letters, lets say 123abc, then x will be stored as 123 but abc remains in the input buffer. You may wish to clear that right away so abc doesn't appear in the next loop.
while (number <= input) //while corrected
{
cout << "Please type a numerical value now: \n";
cin >> x;
bool error = cin.fail();
cin.clear();
cin.ignore(0xFFFF, '\n');
if (error)
{
cout << "Input incorrect" << endl;
continue;
}
accumulator = accumulator + x;
number = number + 1;
}
Alternatively you can initialize x. For example
double x = numeric_limits<double>::min();
cin >> x;
cin.clear();
cin.ignore(0xFFFF, '\n');
if (x == numeric_limits<double>::min())
{
cout << "Input incorrect" << endl;
continue;
}
If error occurs then x remains unchanged and you know there was an error, because it is unlikely that the user inputs a number matching numeric_limits<double>::min()
Not related to this issue, but you should also account for divide by zero error.
if (input == 0)
mean = 0;//avoid divide by zero, print special error message
else
mean = accumulator / input;
I am implementing a Guessing game where computer generates random number with the following code:
int main()
{
srand(time(NULL));
while (true){
int num = rand() % 10, guess, tries = 0;
while (true){
cout << "Enter number 1 to 10:";
cin >> guess;
if (tries > 2)
{
break;
}
if (guess > num)
{
cout << "Too High ! Try again"<<endl;
}
if (guess > 10)
{
cout << "Error ReEnter 1 to 10\n";
}
else if (guess < num)
{
cout << "Too Low! Try again"<<endl;
}
else
{
break;
}
tries++;
}
if (tries > 2)
{
cout <<"\nYou ran out of tries!\n";
cout << "\nThe answer is:" << num << endl;
}
else
{
cout << "\nCONGRATZ!! You guess correctly!\n";
}
return 0;
}
}
One of the problems is: yet when user attempt 3 times, the program shows "ran out of tries" even though the user input is correct on 3rd try.
Questions:
1.How do I inform user that their input exceeds 10 and show an error message to user to enter values from 1 to 10 ?
2.How to correct the aforementioned problem?
instead of writing the program for you here is some pseudo code.
get a random number rand()%10+1 1..10 call it R
loop
get user input N
if N == R then show OK and break loop
if N < R show too low
else show too high
increment tries
if tries == 3 then break loop
end loop
You have too many if else conditions that make your code unnecessarily complex, to answer your second question specifically the unwanted behaviour is caused from the:
if (tries > 2)
{
break;
}
which exits the loop regardless of the guess, as it is dependant only on the number of tries. Regarding your first question, I decided to provide you with more simple implementation that includes an answer to it as well.
You could replace your while loop with a do-while loop, terminated when the random number is guessed, i.e.:
int main(){
// initialize random seed
srand (time(NULL));
// generate a random number within [1,10]
int secretRandom = rand() % 10 + 1;
// initialize
int yourGuess = 11;
// input loop
string promptMessage = "Type a a number from 1 to 10."
do{
cout << promptMessage << '\n';
// read input
cin >> yourGuess >> endl;
// guessed number relatively to the randomly generated
if (secretRandom < yourGuess) cout << "The secret number is lower\n";
else if (secretRandom > yourGuess) cout << "The secret number is higher\n";
}while(yourGuess != secretRandom)
// if you guess the random number exit the loop and display success message
cout << "You guessed right!\n";
return 0;
}
To reduce the amount of guesses to specific number you can enclose the do-while loop and the success message in a for loop, for example:
int numberOfGuesses = 3;
for (int i = 0; i <= numberOfGuesses; ++i){
//...
}
If you want to enforce the user to input a number in the range from 1 to 10, you could do it by:
int yourGuess = 11;
int lowerBound = 0;
int upperBound = 10;
do{
cin >> yourGuess;
// not type safe
}while(yourGuess < lowerBound || yourGuess > upperBound);
I am just a newbie to programming and I was trying to write a while loop that runs as long as the input (num) is not a integer which doesn't ends with zero. What happen is when I enter a number that ends with zero, the program runs the loop correctly, but when i enter something nonsense such as rofl the program only print The input is not valid. and won't repeat the loop. I have tried to look for solutions but I am still stuck after a hour. Anyone can help me here? Thx so much!
void rev_sum() {
int num;
int a = 1;
while (a < 2) {
cout << "Please input a natural number without zero at the end:\n";
cin >> num;
if (!cin) {
cout << "The input is not valid.\n";
cin.clear();
cin.ignore(INT_MAX);
}
if (num % 10 == 0) {
cout << "The number cannot have zero at the end\n";
} else {
cout << "gj\n";
break;
}
}
}
Try replacing
cin.ignore(INT_MAX);
With
cin.ignore(numeric_limits<streamsize>::max(), '\n');
And change the
if (num % 10 == 0)
To
else if (num % 10 == 0)
Your final code should look like this:
void rev_sum() {
int num;
int a = 1;
while (a < 2) {
cout << "Please input a natural number without zero at the end:\n";
cin >> num;
if (!cin) {
cout << "The input is not valid.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
else if (num % 10 == 0) {
cout << "The number cannot have zero at the end\n";
} else {
cout << "gj\n";
break;
}
}
}
if (num % 10 == 0) {
could be
else if (num % 10 == 0) {
otherwise the other else case might get executed
You set a = 1 at the start of the loop and then never change a which means the only way to leave your while loop is if you hit the break statement. If your loop is not looping then it must be stuck somewhere. I'm unfamiliar with the statements if (!cin) and cin.ignore(...) so those are top suspects to check (or change). The statement cin >> num; completes no matter what they type so you can check to see what 'num' is equal to when you enter "rofl". Then after it fails, you still use num so you are processing this unintentional entry. You can add continue; after cin.ignore(...) to jump back to the top of the while loop and ask the question again. You can also print something after the while loop in order to know when you get out.
With all that said, I would never trust the user to enter acceptable information and I would never trust cin to process it for me. Personally, I would read cin as a string using cin.getline(buffer, buffer_size); And then I would complain to the user if they filled the buffer or gave me something that was not an integer (which you can check with a function like scanf()). Then you can spit back exactly what they gave you and you can be specific about your complaint.
Would it not make more sense to only check that the number ends in 0 if the input was valid....
void rev_sum() {
int num;
int a = 1;
while (a < 2) {
cout << "Please input a natural number without zero at the end:\n";
cin >> num;
if (!cin) {
cout << "The input is not valid.\n";
cin.clear();
cin.ignore(INT_MAX);
} else {
if (num % 10 == 0) {
cout << "The number cannot have zero at the end\n";
} else {
cout << "gj\n";
break;
}
}
}
}
cin.ignore(numeric_limits<streamsize>::max())