Loop does not allow input

The following code is within one of my programs. If any number is entered, the code correctly runs. However, if anything other than a number is assigned to *Exc_Select, the loop runs continously, displaying all output without allowing the player to enter in another choice. I've spent some time trying to correct this bug and can't quite figure it out. Thanks for your help.



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
void Exercise_Slct(int* Exc_Select) // user picks which exercise program to perform
{
	bool Choice_Confirm = false; // has the player confirmed his exercise plan choice?
	string Choice_String; // for inputting "yes" or "no"

	*Exc_Select = -1; // so the loop will run

	while ( !Choice_Confirm )
	{

		while ( *Exc_Select < 0 || *Exc_Select > 4 )
		{

			cout << endl;
			cout <<"1.) Beginner\n";
			cout <<"2.) Intermediate\n";
			cout <<"3.) Advanced\n";
			cout <<"4.) Free-for-all\n";

			cout <<"Choose a program for more options: ";

			cin >> *Exc_Select; // input the value into Exercise_Slct via dereferenced pointer
			cin.ignore();

		

			if ( *Exc_Select < 0 || *Exc_Select > 4 )
			{
				cout << "\nPlease enter a number between 1 and 4.";
				*Exc_Select = -1; // so the loop will run again


			}

		

		}
	

	cout << endl << endl; // 2 new lines 




Sean
*Exc_Select is a pointer to an int.

Therefore cin >> *Exc_Select; expects an int. If you input anything other than that cin will set an error flag.

What I usually do is instead of getting the int directly, I get an entire line and then transform it to int, therefore making sure correct input is given.

1
2
3
4
char buffer[20];
cin.getline(buffer, 20);

*Exc_Select = atoi(buffer) //Reads an int from the string 


Depending on your compiler you might need to #include <cstdlib>

That should do the trick.

EDIT: Don't forget that in case of atoi failling it will return 0. (if the input is for instance "h", atoi(buffer) == 0)

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
	while ( !Choice_Confirm )
	{

		while ( *Exc_Select < 0 || *Exc_Select > 4 )
		{

			cout << endl;
			cout <<"1.) Beginner\n";
			cout <<"2.) Intermediate\n";
			cout <<"3.) Advanced\n";
			cout <<"4.) Free-for-all\n";

			cout <<"Choose a program for more options: ";


			char buffer[20];
			cin.getline(buffer, 20);

			*Exc_Select = atoi(buffer); //Reads an int from the string 
		

			if ( *Exc_Select <= 0 || *Exc_Select > 4 ) //Notice the <=0
			{
				cout << "\nPlease enter a number between 1 and 4.";
				*Exc_Select = -1; // so the loop will run again


			}

		

		}
	}
Last edited on
Mmmm, very interesting. Input the number into a char array first, then put the contents into the integer variable. I modified my source code and it worked. Thanks.
It can be a string (with a small modification) as well, but this way you'll save memory in case someone inputs a very large string.

Speaking of wich, you should put cin.sync() after the getline statement. This way there won't be anything left in the buffer if someone inputs more than 19 chars.
Would cin.ignore(); do the same trick?
if you use cin.ignore than if you input less than 20 chars it will make you hit enter before you continue.

cin.ignore() says basicly: Get input from user, then assume the user is stupid and discard that input. :P

cin.sync(); syncs the buffer, discarding whatever was left there to read. Depending on your compiler you might have to use cin.clear() and then cin.sync().
Last edited on
Topic archived. No new replies allowed.