0

So I'm trying to create a little game in which I think of an animal (in this case) and prompt the user to guess what it is, even giving them little hints if they keep getting it wrong. However, the program only seems to work once and when I ask the user if they want to try again, it just keeps repeating the same message. Any hints on solving this would help me a lot.

PS: I've just started coming back to C++ after a long absence so my skills are a little rusty.

#include <iostream>
using namespace std;

int main()
{
    string animal = "fish";
    string guess;
    char choose = 'Y';
    int count = 0;
    int limit = 5;
    bool out_of_guesses = false;

    cout << "I am thinking of an animal.\n" << endl;

    do
    {
        while (animal != guess && !out_of_guesses)
        {
            if (count < limit)
            {
                cout << "Can you guess what animal I am thinking of?: ";
                getline(cin, guess);
                count++;
                if (animal != guess)
                {
                    cout << "\nHmm, nope. That's not the animal I'm thinking of." << endl;
                    if (count > 2 && count < 5)
                    {
                        cout << "I'll give you a hint. It lives in water." << endl;
                    }
                }
            }
            else
            {
                out_of_guesses = true;
            }
        }
        if (out_of_guesses)
        {
            cout << "\nI'm sorry, but you are out of guesses.\n" << endl;
        }
        else
        {
            cout << "\n*** Good job! You guessed the correct animal! ***\n" << endl;
            cout << "\t\t><)))º> ❤ <º)))><\t\t" << endl;
        }
        cout << "\nWould you like to try again?: ";
        cin >> choose;
    } while (choose == 'Y' || 'y');
    return 0;
}
user4581301
  • 29,019
  • 5
  • 26
  • 45
  • Read the linked question to understand what happens after your `cin >> choose;`, and when the loop runs again, and attempts to call `getline()`. – Sam Varshavchik Feb 21 '19 at 02:45
  • You've made a mistake in your condition. There are several duplicates out there but they're difficult to find. Your condition `while(choose == 'Y' || 'y')` should be `while(choose == 'Y' || choose == 'y')`. Since `'y'` is a valid pointer, it will evaluate as `true` – Tas Feb 21 '19 at 02:46

0 Answers0