Click to See Complete Forum and Search --> : Program nested if help


gurlygurlz
January 10th, 2003, 07:47 PM
I am trying to nest if statements in my program, but it asks all of the questions even if it doesn't meet the criteria.

For Example:

cout << "Does " << patient_name<<" have a cough? ";
cin >> Q4;
if (Q4 == "yes" || "Yes")
{
cout << "Does "<< patient_name<<" have a dry cough? ";
cin >> Q4_a;
if (Q4_a == "no" || "No")
{
cout << "Does "<< patient_name<<" have a mild hacking cough? ";
cin >> Q4_b;
if (Q4_b == "no" || "No")
{
cout << "Does "<< patient_name<<" have a severe cough? ";
cin >> Q4_c;
}
}
}

Why is it asking if the user has a mild hacking cough and a severe cough if the user inputs that he already has a dry cough? I want it to prompt the user if the patient has a dry cough, and if he does I don't want it to ask if he also has a mild hacking and a severe cough.

THANKS :)

mdmd
January 10th, 2003, 09:26 PM
Hi gurlygurlz

I suspect Q4 and the other input variables are string ? or are
they char* ?

anyway the lines like
if (Q4 == "yes" || "Yes")

should be this if they are string or
if ((Q4 == "yes") || (Q4 == "Yes"))

If they are char* then use strcmp() instead of ==

mdmd

gurlygurlz
January 10th, 2003, 09:35 PM
Thanks a bunch! It was so easy that I overlooked it thinking it would be something more complicated than that. Thanks again :)

DanM
January 10th, 2003, 10:31 PM
Don't want to be rude but you had very similar problems a while ago :)

if (symptom_number == 1&&2&&4&&5)


http://www.codeguru.com/forum/showthread.php?s=&threadid=223790&highlight=gurlygurlz

Dan