#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
void functions();
usingnamespace 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};
constint 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!
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.