Do-while and for loops for guessing game


My code is running correctly, only I'm not sure why I'm having to press "ENTER" when the user gets the answer wrong and loops back around for the same hint. I would like for it to say, "That is incorrect. Please try again." and the user answers right there instead of having to repeat the hint.

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
71
72
73
74
75

#include <iostream>     //needed for cout and cin
#include <vector>       //needed for vectors
#include <string>       //needed for strings
#include <ctime>	//needed for time
#include <cstdlib>	//needed for srand

using namespace std; 

int main ()
{
   cout << "Color-me-Rad Guessing Game! \n" << endl; 
   cout << "Lets play a game!\n" << endl;
   cout << "Guess the color using only the hint that is given to you." << endl; 
   cout << "You only have three tries! GOOD LUCK! \n" << endl;

   //declarations
   vector <string> Colors;
   vector <string> Hints;
   int number = 0;
   int option = 0;
   int guessCount = 1;
   const int guesses = 3;
   string answer, color; 

   Colors.push_back("red");
   Colors.push_back("yellow");
   Colors.push_back("purple");
   Colors.push_back("blue");
   Colors.push_back("green");
   Colors.push_back("orange");
	
   Hints.push_back("\nThe color of Ariel's hair in Disney's The Little Mermaid: ");
   Hints.push_back("\nThe most common color of a glass of lemonade: ");
   Hints.push_back("\nMixing blue and red gives you this color: ");
   Hints.push_back("\nThe color of nobility, calmness, and serenity: ");
   Hints.push_back("\nThis is the color of the forest: ");
   Hints.push_back("\nThe color of the sunset: ");

   srand((unsigned) time(NULL));
   number = rand(); 
   option = number % 6;
	

   do{
   number = rand() % Colors.size();
   guessCount = 1;
      for (unsigned int i = 0; i < guesses; ++i){
	cout << Hints.at(number);
	getline(cin, color);	
	cin.clear();

	if (color == Colors.at(number)){
	    cout << "Good Job!";
	    i = guesses;
	    }
	else {
	    cout << "That is incorrect. Please try again.";
	    getline(cin, color);
	    cin.clear();
	    guessCount++;
	}
	    if (guessCount == 4){
		cout << "Good try, but none of your answers were correct"
		<< "\nThe correct answer is " << Colors.at(number) << "\n" << endl;
	    }
        }
	    cout << "\n\nDo you want to play another round==> ";
	    getline(cin,answer);
   } while (answer == "yes, Yes, Y, YES"); 
        cout << "Thank you for playing. GOOD BYE!" << endl;
			
	return 0;
}

Last edited on
Remove line 59 and 60 and your code will work as intended.


If you need more help check out some of these videos:

https://www.youtube.com/watch?v=dQw4w9WgXcQ
https://www.youtube.com/watch?v=kKACK2TJ6AY
Topic archived. No new replies allowed.