I'm having trouble with lines 44 to 46. Basically, I want it so that if the user enters a letter or something, it says to enter a number. Whenever I enter a letter, the program repeats like 5 times then closes.
I'm assuming this is because the Answer variable is an integer ? And you can't enter letters into integers ? I need help! I really have no idea what to do.
Indeed, you break your cin when it expects a number and gets a non-number.
You need to do this check before entering the if statement:
1 2 3 4 5 6 7 8 9
int x;
cout << "Please enter a number: ";
while (!(cin >> x)) { // extract and check for a broken cin
cin.clear(); // reset your cin object
cin.ignore(80, '\n'); // clear the buffer
cout << "Numbers only please, try again: ";
}
cin.ignore(80, '\n'); // incase the first entry was actually a number
It's also a handy function :).
The if statement could be made a little better, consider this:
1 2 3
if (x >= 100);
elseif (x >= 50);
else;
In this situation, the else if will only be reached if x is less than 100. The else will only be reached if x is less than 50. I guess what I am saying is that although 50 is a special number in your case, it doesn't make total sense to have it first in the conditional.
~Perhaps Obama's age should come from ctime, that way your program will still work next year~
I'm still a total newbie and I honestly barely understood what you just said. I've changed it so that it works with my program and its perfect!. I'm going to research into this so I can try and get my head around it.
As for the if and else statements, yeah, that's a great idea!
As for "ctime", no idea how to use it ... I'll look into it.
cin.ignore(80, '\n'); // incase the first entry was actually a number
Also, what does this do?
I can tell that you want to ignore something, but why is 80 there?
I have a feeling that it has something to do with a buffer =S
I had this problem a while ago and quickly got in way over my head. This looks useful, but i've never seen the cin.something commands before.