cin.ignore problem

I am trying to write a simple program for a class.
I have it all complete except for trying to verify user input.
I want input to only be an integer between 1 and 999.

Here is what I have so far...

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
43
44
45
46
47
48
49
50
51
52
53
//This program generates a random number and asks the user to guess it
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{

	//Variables
	unsigned randomnum = time(0);
	unsigned guess;
	unsigned counter;
	

	//generating pseudorandom number
	srand(randomnum);
	randomnum = 1 + randomnum % 999;
	
	cout <<"Guess a number between 1 and 999:  ";
	for (counter = 1 ; counter <= 10 ; counter++)
	{
		if (counter >= 2)
		cout <<"Guess again:  ";
		cin >>guess;
		while (guess < 1 || guess > 999)
		{	
			cin.clear(); 
			cin.ignore(1024);
			cout <<"Invalid Input: Enter integer between 1 and 999:  ";
			cin >>guess;
		}
		if (randomnum == guess)
		{
			cout <<"Your guess is correct";
			break;
		}
		else if (randomnum < guess)
		{	
			cout <<"Your guess is higher than stored number"<<endl;
			continue;
		}
		else if (randomnum > guess)
		{	
			cout <<"Your guess is lower than stored number"<<endl;
			continue;
		}
		
	}
	if (counter == 11)
		cout <<"You are not very bright";
	return 0;
}


The bolded part is give me the problem when a string is entered instead of integers.

If I don't use cin.ignore at all I get an infinate loop.

If I use cin.ignore(1024, ' ').. It sort of works but repeats the cout line as many times as there are characters in the string.

If I use cin.ignore(1024, /n) it terminates.

You can't do it like that. If the first cin fails, the value of guess is undetermined when you do the first check. Calling cin.clear() only makes sense when cin is in a fail state (for example, you tried reading an int, but encountered characters instead). I don't know exactly why cin behaves that way for you, but I can at least tell you that /n is not a character... I think you're mixing that up with '\n', though you probably got that right in your code.
I got it to work finally. It was the \n mistake. Correct line is cin.ignore(1024,'\n'); I also added "cin.fail()" to the conditions of the while loop.



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
43
44
45
46
47
48
49
50
51
52
53
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{

	//Variables
	unsigned randomnum = time(0);
	unsigned guess = 0;
	unsigned counter;
	

	//generating pseudorandom number
	srand(randomnum);
	randomnum = 1 + randomnum % 999;
	
	//Asking user to guess number
	cout <<"Guess a number between 1 and 999:  ";
	for (counter = 1 ; counter <= 10 ; counter++)
	{
		if (counter >= 2)
		cout <<"Guess again:  ";
		cin >>guess;
		while (cin.fail() || guess < 1 || guess > 999)
		{	
			
			cout <<"Invalid Input: Enter integer between 1 and 999:  ";
			cin.clear();
			cin.ignore(1024,'\n');
			cin >>guess;
		}
		if (randomnum == guess)
		{
			cout <<"Your guess is correct";
			break;
		}
		else if (randomnum < guess)
		{	
			cout <<"Your guess is higher than stored number"<<endl;
			continue;
		}
		else if (randomnum > guess)
		{	
			cout <<"Your guess is lower than stored number"<<endl;
			continue;
		}
		
	}
	if (counter == 11)
		cout <<"You are not very bright";
	return 0;
}
Last edited on
Topic archived. No new replies allowed.