Question =(

INSTRUCTIONS: Given a string variable s that has already been declared, write some code that repeatedly reads a value from standard input into s until at last a "Y" or "y"or "N" or "n" has been entered.

Here is what I put in:

1
2
3
4
5
6
7
    while (s == Y || s == y || s == N || s == n)
    
       {
       
         cin >> s;
    
       }


Why is this not working? Isn't it saying if variable s is "Y" then proceed, or s is "y" proceed, or s is "N" proceed, or s is "n" proceed, otherwise, prompt the user to put input in again with a cin >> s; command? This is the errors I'm getting:

When we compiled your code with our code (to test for errors), the following compiler error messages were produced:

CTest.cpp: In function 'int main()':
CTest.cpp:11: error: 'Y' was not declared in this scope
CTest.cpp:11: error: 'y' was not declared in this scope
CTest.cpp:11: error: 'N' was not declared in this scope
CTest.cpp:11: error: 'n' was not declared in this scope


Last edited on
You're entering Y, y, N, n as variables. You need to enclose them with single quotes. (Eg; 'Y','y','N','n')

Also, a while loop will continue as long as the parameter is true, so if "s" is equal to Y, y, N, or n, it will ask the user to input something. This is reversed from what you are wanting.

I'd recommended looking at the do while loop here: http://www.cplusplus.com/doc/tutorial/control/
Last edited on
Topic archived. No new replies allowed.