Input Validatiion Help

I cannot for the life of me figure out how to make this piece of code not accept a character as well as numbers < 1 and numbers > 5. Everything I have tried does an infinite loop

with this configuration of this piece of code, I am trying to make it to where if the user enters anything besides the correct selections, then the variable characters is set equal to -1. Then the while loop tests while character = -1. Logically this should work. I have used breakpoints and cout statements to see what the value of characters is holding at those specific points where it assigns -1 and it does hold -1. So why is my loop infinite still when I put in a character, when I have tested to see what value character holds after entering a character and it does hold -1...

im confused. Ive also tried cin.fail() but could not get it to work.
The cout's were there for testing purposes. you can delete them if annoying.

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
  cout << "Please choose the character you would like to play\n\n";

   // Display character selection menu.
   characterSelection();

   // Choose your Character

   cin >> character;

   cout << character;

   if (character != 1 && character != 2 && character != 3 && character != 4 && character != 5)
      {
         character = -1;
      }

   cout << character;

   while (character == -1)
   {
      cout << "Please choose a correct selection...\n\n";
      cin >> character;

      if (character != 1 && character != 2 && character != 3 && character != 4 && character != 5)
     {
         character = -1;
     }

      cout << character;
   }
Hi vaze159,

Have a look at this:

http://www.cplusplus.com/forum/beginner/104553/2/#msg564228


The switch control structure can be used with constant chars or ints, sounds as though you are after the char version. switch is great for this, make sure you have a default case to catch bad input.

Hope all goes well
Your while loop iterates whenever your character is invalid, and you never change it away from the invalid flag.

Normally, you do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
while (cin >> c) {
	if (invalid(c)) {
		break; //or throw error
	}
	//do things
}

// or

cin >> c;
while (!invalid(c)) {
	//do things
	cin >> c;
}


cin.fail() only returns true if something went wrong with reading the input stream.
Last edited on
I do not see how it does not change from the invalid flag. it is while character = -1. If i enter 1 or 2 or 3 or 4 or 5, then those are valid and it continues on. If I enter anything less than 1 or greater than 5, then it asks me to input again and does not spiral into infinite loop. but if I enter a character, bam. even though the character value itself is like -81929873249829 or whatever, I am assigning it to -1 when it is "invalid" and testing if character = -1, which it should and then ask me again.

The switch statement would solve this but I a really curious as to why this itself is not working. this is the most odd thing I have encountered in c++ so far.

it uses that if statement twice. The first to check if it was any of the choices and if not character = -1 before the loop. In the loop, it does the same thing to make sure that if the user entered another invalid choice, that character = -1 again to continue the loop.

it works if a valid choice was entered first, it continues on. if an invalid integer is entered it stops and asks, then if valid choice is entered it continues on.
Also I have noticed that if I enter a character, it completely skips over the cin >> character statement that I have to allow the user to try again and just goes infinite. HOW??????
What data type is the "character" variable?
it is an int. I think I may have learned that if I am trying to enter a char into a declared int variable, then no value is given. Im not completely sure if this is correct.
Last edited on
When you enter a character when cin is expecting an int you will cause cin to fail. You can use cin.clear() to get rid of the failbit on cin and then use cin.ignore(numeric_limits<streamsize>::max(),'\n'); to ignore the input that caused cin to fail in the first place. Try working this into your program and see if that resolves your problems.

1
2
3
4
5
if(cin.fail()) //if the user enters bad input
{
	cin.clear(); // Clear the failbit from cin
	cin.ignore(numeric_limits<streamsize>::max(),'\n'); //Ignore everything on cin until the next newline
}
isnt it #include <limits> that I must add as well?
it is telling me not enough actual parameters for macro max.
Last edited on
If you want to use numeric_limits<streamsize>::max() then you must #include <limits>, but the first argument for cin.ignore() is just the number of characters to ignore. You could just as easily replace it with 100 or another number. numeric_limits<streamsize>::max() is nice because it gives the max size cin can hold.
it is telling me not enough actual parameters for macro max.


To fix this error, you have to define NOMINMAX before your includes like so:

1
2
3
4
5
#define NOMINMAX
//whatever you're including
#include <windows.h>
#include <iostream>
#include <limits> 


EDIT* or at least, define it before windows.h, which is typically the culprit behind this error.
Last edited on
Thanks all. This has solved my problem and now nothing is able to be entered that I will not allow.
Topic archived. No new replies allowed.