Input Validation Strings

Jul 6, 2008 at 4:22pm
Hello all,

I am trying to check user input, the program will only accept 4 answers, A, B, C and D. I planned on using a do-while, but i can't get the validation working. Here is my code:

1
2
3
4
5
6
7
8
9
10
	for (counter = 0; counter < SIZE; counter++)
	{
		do
		{
			cout << "Please input students answers for question " << 
					(counter + 1) << ".";
			cin >> stuAns[counter];
		}while(strcmp(stuAns[counter], "A") != 0 || strcmp(stuAns[counter], "B") != 0
			|| strcmp(stuAns[counter], "C") != 0 || strcmp(stuAns[counter], "D") != 0);
	}

I am using Borland C++ 5.5.1 with jGRASP as my text editor. The code will compile as it stands. Could any tell me the error? Thanks in advance.
Last edited on Jul 6, 2008 at 4:35pm
Jul 6, 2008 at 6:25pm
The while logic should be AND && not OR ||

1
2
3
4
5
6
do
	{
	    cout << "Please input students answers for question " << (counter + 1) << ".";
	   cin >> stuAns[counter];
	}while(strcmp(stuAns[counter], "A") != 0 && strcmp(stuAns[counter], "B") != 0 
                        && strcmp(stuAns[counter], "C") != 0 && strcmp(stuAns[counter], "D") != 0);
Last edited on Jul 6, 2008 at 6:26pm
Jul 6, 2008 at 7:46pm
Thanks a bunch guestgulkan. I tried the && and everything works thanks again.
Topic archived. No new replies allowed.