1

I'm new to programming and trying a program that You need to guess the name of a video game character, there are only 3 guesses if you ran out of guesses you will lose. I used a do-while loop here so I can do it again and again... The problem here is that every time The loop starts again It displays the prompt 2 times even though It's supposed to be 1 time prompt per guess but it displays 2 prompt. Can you please help me maybe I'm doing the algorithm wrong, Thanks!

#include <iostream>

using namespace std;

int main()
{

    char rerun_option;

    do {
        string secretWord = "Arthur Morgan";
        string guess;
        int guessCount = 0;
        int guessLimit = 3;
        bool outofGuesses = false;

        while (secretWord != guess && !outofGuesses) {
            if (guessCount < guessLimit) {
                cout << "Enter video game character name guess: ";
                getline(cin, guess);
                guessCount++;
            }
            else {
                outofGuesses = true;
            }
        }
        if (outofGuesses) {
            cout << "You Lose!" << endl;
            outofGuesses = false;
        }
        else {
            cout << "You Win!" << endl;
        }

        cout << "Try Again?(Y/N) ";
        cin >> rerun_option;
    } while (rerun_option == 'Y' || rerun_option == 'y');

    return 0;
}
drescherjm
  • 8,907
  • 5
  • 42
  • 60
yvez
  • 11
  • 2

1 Answers1

0

EDIT: stackoverflow.com/a/21567292/4645334 is a good example of your problem, explains why you are experiencing it, and explains how you can solve it. I have provided a working example of your code below, as well as a link to more information on the use and description of cin.ignore().

#include <iostream>

using namespace std;

int main()
{

    char rerun_option;

    do {
        string secretWord = "Arthur Morgan";
        string guess;
        int guessCount = 0;
        int guessLimit = 3;
        bool outofGuesses = false;

        while (secretWord != guess && !outofGuesses) {
            if (guessCount < guessLimit) {
                cout << "Enter video game character name guess: ";
                cin.ignore(); // <-- ADD THIS LINE RIGHT HERE
                getline(cin, guess);
                guessCount++;
            }
            else {
                outofGuesses = true;
            }
        }
        if (outofGuesses) {
            cout << "You Lose!" << endl;
            outofGuesses = false;
        }
        else {
            cout << "You Win!" << endl;
        }

        cout << "Try Again?(Y/N) ";
        cin >> rerun_option;
    } while (rerun_option == 'Y' || rerun_option == 'y');

    return 0;
}

https://www.tutorialspoint.com/what-is-the-use-of-cin-ignore-in-cplusplus

Joe Davis
  • 205
  • 8
  • No idea why this was downvoted as it fixes OPs problem and provides reference to where they can learn about why it fixes it. – Joe Davis Nov 12 '20 at 14:50
  • I downvoted because of two reasons. The dupe's answer has a very good explanation with solutions. This question shouldn't be answered but closed as a duplicate. If you want to answer it, describe the problem in the answer. Do not just add a link. Explain why and when this problem occurs and how it can be solved. – Thomas Sablik Nov 12 '20 at 14:53
  • 1
    @ThomasSablik Doesn't seem like good reason for a downvote? Doesn't go against any rules or conduct of SO, so its just your personal preference for doing so. I have seen many answers with correcting code and no explanation without any downvotes. – Joe Davis Nov 12 '20 at 14:55
  • Your answer hides a much better answer. That's a reason for a downvote for me. A question can be marked as duplicate. You don't have to answer with a link to a duplicate. – Thomas Sablik Nov 12 '20 at 15:00
  • 1
    @ThomasSablik "a much better answer" is just arguing semantics and personal preference. Better is in the context of the problem and goal of the user. – Joe Davis Nov 12 '20 at 15:03
  • Stackoverflow is platform with a democratic concept. People can vote according to their opinion. In my opinion this answer should be removed and therefore I downvoted and commented. If other people have a different opinion they can upvote your question. I'm fine with that. – Thomas Sablik Nov 12 '20 at 15:05