C++ if and bool

Hi, I am trying to make a program to verify if you are a US Citizen or not. How can I do so and what is wrong with my code? Thanks for all answers in advance.

string isCitizen;
cout << "Are you a US citizen?: ";
cin >> isCitizen;
string yes;
if (isCitizen == “Yes”)
bool yes = true;
else bool yes = false;

cout << boolalpha << yes;

You cannot change the type of a variable as you have done with the yes variable. You would need to create a separate variable (like yesBool) or rename the one you took as input (eg. response).

2 Likes

Also remember C++ is a case sensitive language, the user can also say ‘yes’ with a lower key y. Or say something like ‘yup’ or ‘ok’. It is better to give also answering options. In this case Y/N.

Then you could type: if(isCitizen == “Y”||isCitizen == “y”) to counter for both variations the user can type.

True, it is better to tell them in the prompt the accepted responses:

cout << "Are you a US citizen?: (Yes/No)";

Then you should reject any input which is not either of those.