Do-While statement problem

I'm just making a pretty simple goofy survey and i decided that i want the user to be forced to answer "yes" or "no". before I put in the do-while statement, the user could type yes or no or it would terminate. Now when the user types yes or no, it goes into an infinite loop of "Please answer yes or no". any tips?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main(){
    string a, b, c, d, e, f, g;
    cout << "Let's start with a few questions about yourself.(Press ENTER)";
    cin.ignore();
    cout << "What is your name?(Only FIRST NAME)\n";
    getline (cin, a);
    cout << "Okay, " << a << ", let's begin.\n";
    cout << "What is your favorite type of cake?\n";
    getline (cin, b);
    cout << "Interesting... Are you an outgoing person? (Yes or No)\n";
    getline (cin, c);
    do {cout << "Please answer Yes or No.\n"; //problem occurs here
       getline (cin, c);}
    while (c != "yes", c != "Yes", c != "no", c != "No");
    if (c == "Yes", c == "yes") {
          cout << "Oh really! Do you like to hang out with friends, or play sports? (answer Friends or Sports)\n";
          getline (cin, d);
          }
    else if (c == "No", c == "no") {
         cout << "Do you like to watch TV or Play video games? (answer TV or games)\n";
         getline (cin, d);
         }
See
http://www.cplusplus.com/doc/tutorial/operators/
under "Logical operators" and "Comma operator".
You are using the comma operator in place of && and ||
hello SuperiorJT,

1
2
3
4
5
6
    cout << "Interesting... Are you an outgoing person? (Yes or No)\n";
    getline (cin, c); // <-- this will have no affect
    do {cout << "Please answer Yes or No.\n"; //problem occurs here
       getline (cin, c);} // <-- due to 'do' you'll always ask this twice
    while (c != "yes", c != "Yes", c != "no", c != "No"); // <-- Uh, you don't want ',' you want '&&'
    if (c == "Yes", c == "yes") { // <-- same here 

On the if it should be || not &&
Is there a way for me to only ask to answer yes or no when they type anything other than that? because it will ask it anyway if user types yes or no.
Also, sometimes the program will not write the results to a txt file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
int main(){
    string a, b, c, d, e, f, g;
    cout << "Let's start with a few questions about yourself.(Press ENTER)";
    cin.ignore();
    cout << "What is your name?(Only FIRST NAME)\n";
    getline (cin, a);
    cout << "Okay, " << a << ", let's begin.\n";
    cout << "What is your favorite type of cake?\n";
    getline (cin, b);
    cout << "Interesting... Are you an outgoing person? (Yes or No)\n";
    getline (cin, c);
    do {cout << "Please answer Yes or No.\n"; 
       getline (cin, c);}
    while (c != "yes"&&c != "Yes"&&c != "no"&&c != "No");
    if (c == "Yes"||c == "yes") {
          cout << "Oh really! Do you like to hang out with friends, or play sports? (answer Friends or Sports)\n";
          getline (cin, d);
          }
    else if (c == "No"||c == "no") {
         cout << "Do you like to watch TV or Play video games? (answer TV or games)\n";
         getline (cin, d);
         }
         if (d == "Friends"||d == "friends") {
               cout << "Thank you for taking this survey! You are " << a << ",an outgoing person who likes to hang out with friends and eat " << b << " cake!\n";
               }
         else if (d == "Games"||d == "games") {
              cout << "Do you like Halo Reach, or Call of Duty: Black Ops? (Answer Halo or Cod)\n";
              getline (cin, e);
              }
         else if (d == "Sports"||d == "sports") {
              cout << "How fascinating! What is your favorite sport?\n";
              getline (cin, f);
              cout << "Thank you for taking this survey! You are " << a << ",an outgoing person who likes to play " << f << " and eat " << b << " cake!\n";
              }
         else if (d == "TV"||d == "Tv"||d == "tV"||d == "tv") {
              cout << "What is your favorite TV show?\n";
              getline (cin, g);
              cout << "Thank you for taking this survey! You are " << a << ",a stay at home person who likes to watch " << g << " and eat " << b << " cake!\n";
              }
              if (e == "Halo"||e == "halo") {
                    cout << "Isn't it just amazing! Anyway, thank you for participatng in this completely\nvoluntary survey. You are " << a << ", a stay at home person who likes\nto play Halo Reach and eat " << b << " cake!\n";
                    }
              else if (e == "Cod"||e == "cOd"||e == "coD"||e == "COd"||e == "cOD"||e == "CoD"||e == "COD") {
                   cout << "How disappointing...you are " << a << ", a stay at home person who eats " << b << " cake and wastes his life playing a stupid game that Treyarch created from their ass-holes.....like always\n";
                   }
    cin.get();
    ofstream results;
    results.open ("results.txt", ios::app);
    results <<"Name:"<< a <<"\n"<<"Cake:"<< b <<"\n"<<"Outgoing:"<< c <<"\n"<< d <<"\n"<< e <<"\n"<< f <<"\n"<< g <<"\n";
    results.close();
    return 0;
}
As coder777 said the problem is here:
10
11
12
13
14
    cout << "Interesting... Are you an outgoing person? (Yes or No)\n";
    getline (cin, c);
    do {cout << "Please answer Yes or No.\n"; 
       getline (cin, c);}
    while (c != "yes"&&c != "Yes"&&c != "no"&&c != "No");

The loop starting at line 12 will always be executed. You won't have this problem if you turn the do while into a while
Last edited on
I got that to work, but i have another problem. i decide to make more of these while statements and they start to contradict with eachother. when i decide to not be an outgoing person and i dont type "games" or "tv" in the next line, it tells me to answer "friends" or "sports." Even then, when i answer "friends" or "sports," it becomes an infinite loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
cout << "Interesting... Are you an outgoing person? (Yes or No)\n";
    getline (cin, c);
    while (c != "yes"&&c != "Yes"&&c != "no"&&c != "No"){
          cout << "Please answer Yes or No.\n"; 
          getline (cin, c);}
    if (c == "Yes"||c == "yes") {
          cout << "Oh really! Do you like to hang out with friends, or play sports? (answer Friends or Sports)\n";
          getline (cin, d1);
          }
    else if (c == "No"||c == "no") {
         cout << "Do you like to watch TV or Play video games? (answer TV or games)\n";
         getline (cin, d2);
         }
         while (d1 != "Friends"&&d1 != "friends"&&d1 != "Sports"&&d1 != "sports"){
               cout << "Please answer Friends or Sports.\n";
               getline (cin, d1);}
         while (d2 != "Games"&&d2 != "games"&&d2 != "TV"&&d2 != "Tv"&&d2 != "tV"&&d2 != "tv"){
               cout << "Please answer Games or TV.\n";
               getline (cin, d2);}
         if (d1 == "Friends"||d1 == "friends") {
               cout << "Thank you for taking this survey! You are " << a << ",an outgoing person who likes to hang out with friends and eat " << b << " cake!\n";
               }
         else if (d2 == "Games"||d2 == "games") {
              cout << "Do you like Halo Reach, or Call of Duty: Black Ops? (Answer Halo or Cod)\n";
              getline (cin, e);
              }
         else if (d1 == "Sports"||d1 == "sports") {
              cout << "How fascinating! What is your favorite sport?\n";
              getline (cin, f);
              cout << "Thank you for taking this survey! You are " << a << ",an outgoing person who likes to play " << f << " and eat " << b << " cake!\n";
              }
         else if (d2 == "TV"||d2 == "Tv"||d2 == "tV"||d2 == "tv") {
              cout << "What is your favorite TV show?\n";
              getline (cin, g);
              cout << "Thank you for taking this survey! You are " << a << ",a stay at home person who likes to watch " << g << " and eat " << b << " cake!\n";
              }
              if (e == "Halo"||e == "halo") {
                    cout << "Isn't it just amazing! Anyway, thank you for participatng in this completely\nvoluntary survey. You are " << a << ", a stay at home person who likes\nto play Halo Reach and eat " << b << " cake!\n";
                    }
              else if (e == "Cod"||e == "cOd"||e == "coD"||e == "COd"||e == "cOD"||e == "CoD"||e == "COD") {
                   cout << "How disappointing...you are " << a << ", a stay at home person who eats " << b << " cake and wastes his life playing a stupid game that Treyarch created from their ass-holes.....like always\n";
                   }
    cin.get();
    ofstream results;
    results.open ("results.txt", ios::app);
    results <<"Name:"<< a <<"\n"<<"Cake:"<< b <<"\n"<<"Outgoing:"<< c <<"\n"<< d1 <<"\n"<< d2 <<"\n"<< e <<"\n"<< f <<"\n"<< g <<"\n";
    results.close();
    return 0;
}

Topic archived. No new replies allowed.