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;
}
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.
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.
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;
}
}