Need Help With Do While Loops

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>


void functions();
using namespace std;

int main()
{
cout << "\n\t\t\t\tWelcome to Jumble\nEnter 'hint' for the hint, or 'quit' to quit\n\n";
    string ans;

do
{
    functions();
    cout << "Would you like to play again?\n\n";
    cin >> ans;
}while(ans=="yes" || "Yes" || "YES");
cout << "Thanks for playing\n\n\n";
}


void functions()
{
    enum file{WORDS, HINT, NUM_FILE};
const int NUM = 3;
string Word[NUM][NUM_FILE] = {{"Jumble", "Name of the game"}, {"Word", "What you speak with"}, {"Cat", "EVIL"}};

srand(time(0));
int choice = rand()%NUM;
string theWord = Word[choice][WORDS];
string theHint = Word[choice][HINT];

string jumble = theWord;
int length = theWord.size();
for(int i = 0; i<length; i++)
{
    int index1 = rand()%length;
    int index2 = rand()%length;
    char type=jumble[index1];
    jumble[index1] = jumble[index2];
    jumble[index2]=type;
}

cout << "The jumble is: " << jumble;

string answer;
cout << "\n\nEnter your guess: ";
cin >> answer;

while(answer!=theWord && answer != "quit")
{
    if(answer=="hint")
    {
        cout << theHint;
    }
    else
    {
        cout << "\nSorry, wrong answer\n\n";
    }
    cout << "Enter your guess: ";
    cin >> answer;
}

if(answer==theWord){
    cout << "\n\nYou did it! Well done!\n\n";
}
}


Everything works fine but one thing.

1
2
3
4
5
6
7
8
    string ans;

do
{
    functions();
    cout << "Would you like to play again?\n\n";
    cin >> ans;
}while(ans=="yes" || "Yes" || "YES");

When I enter "no", or something that isnt yes, it still plays the game. Is it not changing the string ans? Is ans just yes the whole time? How can I fix this? Thanks!
Last edited on
Please edit your post and make sure your code is [code]between code tags[/code] so that it has line numbers and syntax highlighting, as well as proper indentation.

You can't write shorthand like that, you have to write each condition in full:

while(ans == "yes" || ans == "Yes" || ans == "YES");

It may be a good idea to simply convert to lowercase first.
Last edited on
Topic archived. No new replies allowed.