Using the if statement with inputs


I am receiving an error code saying that y, Y, n, and N are undefined. Although I know they are undefined I just want to use the user input for my if statements. I used the data type char for grad.

How do I make the if statement valid? I dont want the y's or n's to be variables but just user input.


1
2
3
4
5
6
7
8
9
10
11
12

cout << "Are you graduating this year? Y for yes or N for no";
	cin >> grad;

	if (grad != (y || Y))
	{
		gradfee = 00.00;
		if (grad != (n || N))
			cout << "Error! You must enter Y for yes or N for No.";
	}
	else
		gradfee = 35.00;
Those booleans in those if statements don't actually evaluate to how you think they do. You have to type out everything, and by everything I mean
if( grad != 'y' || grad != 'Y' )

Also I think you meant to put a single quote in there, due to them being characters, not variables.
I usually do it like this an Yes no Question:
1
2
3
4
5
6
char yesOrNo;

std::cout << "Do you like cats (y or n)? ";
std::cin >> yesOrNo;

...


And mark the thread as solved if you got it working.

edit: Thanks, YFGHNG.
Last edited on
Input operator goes the other way, bro. >_>
Thanks for the help, got the y's and n's to work. Cant get the error message to properly display but ill figure that out on my own. Thanks again!
Topic archived. No new replies allowed.