Do-while loop displays 2 prompt at a time, please reply

Hello, 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!

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
#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;
}
Last edited on
1
2
3
4
  int guessCount = 0;
        int guessLimit = 3;
        bool outofGuesses = false;


Try putting these before the do {. Everytime through the loop you are resetting these values to their initial condition. Also secretWord can be moved to before the do{ as well - as it only needs to be initialised once.
Thank you for answering though It still didn't work It just displays "You Win!" or "You Lose!" If I rerun it
Last edited on
You are mixing std::getline and std::cin. std::getline retrieves (and removes) all the characters in the stream, including the enter character. std::cin leaves the enter character in the stream to be extracted with std::getline when the do loop repeats. You need to ignore (remove) the remaining characters in the stream after using std::cin.

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
#include <iostream>
#include <string>
#include <cctype>   // ::tolower
#include <limits>   // numeric_limits

int main()
{
   char rerun_option { 'n' };

   do
   {
      std::string secretWord { "Arthur Morgan" };
      std::string guess { };
      int guessCount { };
      int guessLimit { 3 };
      bool outofGuesses { false };

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

      if (outofGuesses)
      {
         std::cout << "\nYou Lose!\n\n";
         outofGuesses = false;
      }
      else
      {
         std::cout << "\n*** You Win! ***\n\n";
      }

      std::cout << "Try Again?(Y/N) ";
      std::cin >> rerun_option;
      std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // clear the input stream
      std::cout << '\n';
   }
   while (::tolower(rerun_option) == 'y');
}
Enter video game character name guess: a
Enter video game character name guess: b
Enter video game character name guess: c

You Lose!

Try Again?(Y/N) y

Enter video game character name guess: d
Enter video game character name guess: Arthur Morgan

*** You Win! ***

Try Again?(Y/N) n
Last edited on
I noticed most of you here use std:: and not "using namespace std", Why? and should I do it too?
It worked now big thanks to furry guy!
I personally consider using it being lazy, and can lead to problems when you use 3rd party libraries or create your own custom libraries.

https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice

There are very good reasons why the C++ standard introduced namespaces, take advantage of those reasons. Saving a couple of keystrokes is not worth the hassles when names clash.

After a while always typing std:: will become almost automatic.

So, yes, I would recommend you never have using namespace std; in your code, always type the namespace identifier.

I don't even recommend using using std::cout;, using std::cin;, etc.

For me having the namespace identifier prefix instantly identifies what library I'm using. Makes the code easier to read.

Others will disagree, of course. :)
different styles... I prefer using std::cout; and similar. I find std:: on everything to be visual clutter. I use namespace std for short programs, eg samples on this site, as well, even though its not 'professional'.
Topic archived. No new replies allowed.