simple help

hi all im new in c++ and i want to know how do this example : I think a question
if the user of program answer by yes it shows a result and if the user's answer by no it show another result


i do this

# include <iostream>
int main ()
{
using namespace std;
int shdnum;
cout << "Are You a Program? Y/N" << endl;
cin >> shdnum;
if ('Y' == 'Y') cout << "You are a good person" << endl;
if ('N' == 'N') cout << "bitchplease" << endl;
system ("pause");
return 0;
}


i know it's incorrect please help me
Your two options are Y and N which are characters. You want the type of shdnum to be char, not int. Int is only meant for numbers.

'Y' == 'Y' is comparing a literal to a literal (more specifically, character literals). What you're doing is like saying:

if (2 == 2)

That means the condition will always be true, because 2 will always == 2. Also it doesn't actually do anything. You want to compare the literal with a variable, in this case it's shdnum.

if (shdnum == 'Y')

So when I cin >> shdnum by typing 'Y', shdnum gets the value of 'Y' and it actually becomes if ('Y' == 'Y').

Also characters are case sensitive. Y and y and two completely different things.
Thanks for help men
please if the user type another world example S or G i want he say wrong type Y or N
try an else / if statement

int repeat=0;

do{
repeat=0;
if(shdnum =='Y) .....
else if(shdnum == 'N')....
else {
cout<< "please enter Y or N"
repeat=1;
}
while(repeat);

system....





I guess repeat could also be a bool.
i don't understand please give me an example
try a switch statement

cout << "Are You a Program? Y/N";
cin >> shdnum;
switch(shdnum)
case 'Y':
cout << "You're a program" <<endl;
break;
case 'N':
cout<< "Not a program" <<endl;
break;
default
cout << "Please enter Y or N";
cin >> shdnum;

This is the best way to deal with one character problems where you need to limit the cases.
Mostly for my own benefit,

I'm only partially familiar with switch statements

would the final three lines there in the default case be enough to go back to the top of the switch?
Yes, but add brackets and a colon...

cout << "Are You a Program? Y/N";
cin >> shdnum;
switch(shdnum)
{
case 'Y':
cout << "You're a program" <<endl;
break;
case 'N':
cout<< "Not a program" <<endl;
break;
default:
{
cout << "Please enter Y or N";
cin >> shdnum;
}
}
Sorry, you're going to need a loop to go back to the top.
Topic archived. No new replies allowed.