program sometimes takes letters, it shouldn't.

When I run the program and try entering a letter it will either tell the user to re-try and enter a number or it will say incorrect (it's not supposed to. Also when I enter a decimal it sees the first two numbers after as entries for the questions bellow.

here is a screenshot of the problems
[img]http://i.gyazo.com/c751b29e3091b07757244eb01e934b08.png [/img]

Why does this happen?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

int main() // The main function
{
	srand(time(NULL)); 
	


	while (true)
	{
		printmessage(); // prints the welcome message 
		cout << "\bMenu\n"; // A menu to make it easier to use the program
		cout << "Press 1 to begin" << endl;
		cout << "Press 2 for instructions and help\n";
		cout << "Press 3 to exit" << endl;

		cin >> userOp; //takes the users choice
		if (cin.fail()) { // This is used to make sure letters don't break the program
			cin.clear();
			cin.ignore(numeric_limits<streamsize>::max(), '\n');
		}
		switch (userOp) // A switch statement to make the users choice happen
		{
		case 1:
			for (int loops = 0; loops < 10; loops++)
			{
				iNumber1 = rand() % 10 + 1;
				iNumber2 = rand() % 10 + 1;
				cout << iNumber1 << " x " << iNumber2 << endl;
				do {
					
					cin >> input;
						if (cin.fail()) { // again this is used so that only numbers are entered
							cin.clear();
							cin.ignore(numeric_limits<streamsize>::max(), '\n');
						}
					if (input > 100 || input < 1) // This is so that only numbers between 0 1 and 100 can be inputed
					{
						cout << "Re-try, enter a number equal to or less than 100 and more than 0.\n";
					}
				
				} while (input > 100 || input <1); // This is so that the program does not continue if they enter a number out of the specified range
			


Last edited on
If input fails, it C++03 value of integral variable is not changed. So if previous variable value were in range [1; 100], it get checked in loop conition again.
Also when I enter a decimal it sees the first two numbers after as entries for the questions bellow.

hmmm... you've never used a type that uses the decimal part like double and float so that would be natural(i mean the output.). :)

you can try changing
input
to a double or float.

From the way that the output goes, it looks like the previous input is being considered as the current input, since you never made it go back(override) to let's say a previous acceptable value.

try adding input = 0; after the totalScore(); in the loop and tell me how it goes. :)
Last edited on
Thank you, it works now :-) could you give me more explanation on why and where it was going wrong?
int input

Taking note that this one is declared without definition (*the right side value),

cin >> input;

so the output of this input stream(cin) here would be undefined before you enter any valid number. In simple terms because its not for catching non-numerical values. :)

Now, in this logic:
if (cin.fail()) { // again this is used so that only numbers are entered
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');

It enters that statement when the value is undefined, and clears(cin.clear()) the flag(well, in this case the "error" checked). Then ignore the rest of the input. So if the value of the input was accepted by the FIRST loop, then that value is STILL the value of the input variable, since no alterations whatsoever was made.

-rjvc
Last edited on
Topic archived. No new replies allowed.