im a newb and need help

So i am trying to Write an application that asks a user to type A,B,C, or Q to quit. When the user types Q the program ends. When the user types A, B , or C, the program displays the message "Good Job!" and then asks for another input. When the user types anything else, issue an error message and then ask for another input.

#include <iostream>
#include <string>
using namespace std;
int main ()
{

char userResponse=0;
cout << " Please enter A B C or Q to quit"<<endl;
cin>> userResponse;

while((userResponse != 'A') && ( userResponse != 'B') && (userResponse!= 'C')
&& (userResponse != 'Q') && (userResponse != 'a') && (userResponse != 'b')
&&(userResponse != 'c') && (userResponse != 'q'))
{cout << "Invalid response!!!"<<endl;
cout<< "Please enter A B C or Q to quit"<<endl;
cin>> userResponse;}

while ((userResponse == 'A') || ( userResponse == 'B') || (userResponse == 'C')
|| (userResponse == 'a') || (userResponse == 'b') || (userResponse == 'c'))
{ cout<< "Good job you entered " << userResponse << endl;
cout << "You can enter A, B , or C again or any other key to quit!" << endl;
cin >> userResponse;}

return 0;
}


My question is.. In my 2nd While loop is there a way that I can queue Q to quit rather then just any other key then A, B, OR C? The first loop works correctly but I cant seem to do it for then second one.. I can only get it to be any key to quit!! Sorry i been only programming 2 weeks and just cant seem to figure it out lol..
Last edited on
You only need to test for 'Q' or 'q' in the condition of the while loop. If it sees a q, it will end, so it's more of

while (userresponse != 'Q' || userrespense != 'q' )

Then within the body of the while loop, get input, test it for valid response, but keeping q out of it so it doesn't give an error response.

This should give you only one loop to deal with.
I would suggest only using 1 loop. The logic goes something like:

1
2
3
4
5
6
7
8
9
10
11
get user input

while ( user input is not equal to 'Q' )
{
     if ( user input is valid )
          tell user he did a good job
     else
          tell user his input is invalid.

    get next user input
}
Topic archived. No new replies allowed.