Guessing game

Trying to make a guessing game that will loop and ask the player if they want to play again by using a sentinel loop but nothing I do is working. Here is what I have for the game without the sentinel, but I'm lost for the sentinel part and compiler keeps throwing different errors at me depending where I put the loop. Please help me to know where I would put this. Thank you in advance.

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
  #include <iostream>
#include <cstdlib> // Header file needed for srand and rand
#include <ctime> // Header file needed to use srand with time as seed
#include <iomanip> //Header to use setw

using namespace std;

int main()
{
	int guess;
	int num;
	srand(time(0));
	num = rand() % 100 + 1; // random number to include 100
	int counter = 1;
	int winner = 0; // variable to hold wins
	int loser = 0; // variable to hold losses
	int winCount = 0; //win counter
	int loseCount = 0; // lose counter
	char letter; // character to say yes or no
	
		cout << "\n\n\n";
		cout << setw(5) << " " << num << "\n"; //Print number to test
		cout << setw(5) << " " << "Guess a number between 1 and 100 then press enter: ";

		do
		{
			cin >> guess;
			cout << endl;
			counter++;

			if (guess == num)
			{
				cout << setw(5) << " " << "Bingo!!\n\n";
				break;
			}
			else
				if (guess < num && counter < 6)
				{
					cout << setw(5) << " " << "Your guess is too low try again: ";
				}
				else
					if (guess > num && counter < 6)
					{
						cout << setw(5) << " " << "Your guess is too high try again: ";
					}
			if (counter > 5)
			{
				cout << setw(5) << " " << "Sorry you lose. The number is: " << num << "\n";
				cout << endl;
			}

		} while (counter <= 5);

			
	return 0;
}
For a sentinel loop you'd need a while loop:
http://en.cppreference.com/w/cpp/language/while

Try this:
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
#include <iostream>
#include <cstdlib> // Header file needed for srand and rand
#include <ctime> // Header file needed to use srand with time as seed
#include <iomanip> //Header to use setw

using namespace std;

int main()
{
	int guess;
	int num;
	srand(time(0));
	num = rand() % 100 + 1; // random number to include 100
	int counter = 1;
	int winner = 0; // variable to hold wins
	int loser = 0; // variable to hold losses
	int winCount = 0; //win counter
	int loseCount = 0; // lose counter
	char letter; // character to say yes or no
	bool sentinel = false; //CHANGE HERE
	
		cout << "\n\n\n";
		cout << setw(5) << " " << num << "\n"; //Print number to test
		cout << setw(5) << " " << "Guess a number between 1 and 100 then press enter: ";


		while (!sentinel) //as long as sentinel is false:
		{
			cin >> guess;
			cout << endl;
			counter++;

			if (guess == num)
			{
				cout << setw(5) << " " << "Bingo!!\n\n";
				sentinel = true; //CHANGE HERE
			}
			else
				if (guess < num && counter < 6)
				{
					cout << setw(5) << " " << "Your guess is too low try again: ";
				}
				else
					if (guess > num && counter < 6)
					{
						cout << setw(5) << " " << "Your guess is too high try again: ";
					}
			if (counter > 5)
			{
				cout << setw(5) << " " << "Sorry you lose. The number is: " << num << "\n";
				cout << endl;
				sentinel = true; //sentinel is true, while loop breaks, games stops
			}
		}

			
	return 0;
}


Hope this helps.

Regards,

Hugo.
Last edited on
Dang it is not working. First I tried to make the changes and then I copied exactly as you have it. I am frustrated beyond belief. Been trying to get this to work for two days.
closed account (E0p9LyTq)
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
#include <iostream>
#include <random>
#include <chrono>

int main()
{
   std::cout << "Welcome to the number guessing game!!!\n";
   std::cout << "I have picked a number between 1 and 100.\n\n";

   // create a random device
   std::default_random_engine urng;

   // get a seed
   unsigned seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());
   urng.seed(seed);

   // set a distribution range (1 - 100)
   std::uniform_int_distribution<int> dist(1, 100);

   while (true)
   {
      // pick and store the random number
      unsigned numPicked = dist(urng);

      unsigned guess = 0; // stores the number the user guessed
      unsigned guessNum = 0; // stores the number of guesses

      for (guessNum = 0; guess != numPicked; guessNum++)
      {
         std::cout << "What would you like to guess? ";
         std::cin >> guess;

         if (guess < numPicked)
         {
            std::cout << "\nYou guessed too low!!!\n\n";
         }
         else if (guess > numPicked)
         {
            std::cout << "\nYou guessed too high!!!\n\n";
         }
      }
      std::cout << "\nYou guessed it!!!\n" << "It took you " << guessNum << " guesses.\n";

      std::cout << "\nDo you want to play again? ";
      char answer;
      std::cin >> answer;

      if (answer == 'N' || answer == 'n')
      {
         break;
      }

      std::cout << "\n\n";
   }
}

Welcome to the number guessing game!!!
I have picked a number between 1 and 100.

What would you like to guess? 50

You guessed too high!!!

What would you like to guess? 25

You guessed too high!!!

What would you like to guess? 15

You guessed too low!!!

What would you like to guess? 2

You guessed too low!!!

What would you like to guess? 20

You guessed too low!!!

What would you like to guess? 23

You guessed too low!!!

What would you like to guess? 24

You guessed it!!!
It took you 7 guesses.

Do you want to play again? y


What would you like to guess? 50

You guessed too high!!!

What would you like to guess? 25

You guessed too low!!!

What would you like to guess? 35

You guessed too high!!!

What would you like to guess? 30

You guessed too high!!!

What would you like to guess? 28

You guessed too high!!!

What would you like to guess? 27

You guessed too high!!!

What would you like to guess? 26

You guessed it!!!
It took you 7 guesses.

Do you want to play again? n
Last edited on
What are the changes you're trying to make, and what errors are you getting when you compile your code?
I am trying to add a sentinel that will ask the player if they want to play again and then the loops starts over again. The player gets 5 tries and then they are out. I have the first part done and it works but as soon as I start trying to add the sentinel it doesn't work anymore. Here is what I have and see what it does. It is now wrong. It doesn't play right and after this is done he is going to tell me to add a counter to count the wins and loses. I am not in a class just working with a teacher via email and I am literally in tears right now.


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
76
#include <iostream>
#include <cstdlib> // Header file needed for srand and rand
#include <ctime> // Header file needed to use srand with time as seed
#include <iomanip> //Header to use setw

using namespace std;

int main()
{
	int guess; //
	int num; //my version of rand
	srand(time(0)); //
	num = rand() % 100 + 1; // random number to include 100
	int counter = 1; //
	int winner = 0; // variable to hold wins
	int loser = 0; // variable to hold losses
	int winCount = 0; //win counter
	int loseCount = 0; // lose counter
	char letter = 'Y'; // character to say yes or no
	bool win = false; //

	cout << "\n\n\n";
	cout << setw(5) << " " << num << "\n"; //Print number to test
	


	while (letter == 'Y') //loop to control number of games
	{
		do // loop to control game
		{
			cout << setw(5) << " " << "Guess a number between 1 and 100 then press enter: ";
			cin >> guess;
			cout << endl;
			counter++;

			if (guess == num) //
			cout << setw(5) << " " << "Bingo!!\n\n";
			//win = true;
			break;

			//else 
				if (guess < num && counter < 6)// ****THIS SAYS ERROR EXPECTED 'WHILE'
					
			{
				cout << setw(5) << " " << "Your guess is too low try again: ";
			}
			
			else if (guess > num && counter < 6)
			{
				cout << setw(5) << " " << "Your guess is too high try again: ";
			}//end else if
			if (counter > 5)
			{
				cout << setw(5) << " " << "Sorry you lose. The number is: " << num << "\n";
				cout << endl;
			}//end if
		}//end do

		
		
	while (guess != num && counter <= 5);

		cout << "Do you want to play again? (Y/N)?";
		cin >> letter;

		letter = toupper(letter);

		system("cls");
		counter = 0;
		cout << endl;

	}//end while 
	

	return 0;
}
Here is a backdrop on me. I took both C++ 1 and C++ 2 classes. Five hours a week but the lectures on most days wee only 10 minutes or so and I learned idea but not how to program. I am on summer break now and am trying to learn this language on my own because this is my degree path. I am super frustrated because I spent all that time and didn't learn what I supposed to learn. I came out of those classes in a bad way. I really need guidance.
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
76
77
78
79
80
81
82
83
#include <iostream>
#include <cstdlib> // Header file needed for srand and rand
#include <ctime> // Header file needed to use srand with time as seed
#include <iomanip> //Header to use setw

using namespace std;

int main()
{
	int guess; //
	int num; //my version of rand
	srand(time(0)); //
	num = rand() % 100 + 1; // random number to include 100
	int counter = 1; //
	int winner = 0; // variable to hold wins
	int loser = 0; // variable to hold losses
	int winCount = 0; //win counter
	int loseCount = 0; // lose counter
	char letter = 'Y'; // character to say yes or no
	bool win = false; //

	cout << "\n\n\n";
	cout << setw(5) << " " << num << "\n"; //Print number to test
	


	while (letter == 'Y') //loop to control number of games
	{
		while (guess != num && counter <= 5) // loop to control game  //CHANGE HERE
		{
			cout << setw(5) << " " << "Guess a number between 1 and 100 then press enter: ";
			cin >> guess;
			cout << endl;
			counter++;

			if (guess == num)
			{
			    cout << setw(5) << " " << "Bingo!!\n\n";
			    //win = true;
			    cout << "Do you want to play again? (Y/N)?";  //CHANGE HERE
		            cin >> letter;                                                 //CHANGE HERE

		            letter = toupper(letter);                                  //CHANGE HERE

		            system("cls");                                                //CHANGE HERE
		            counter = 0;                                                  //CHANGE HERE
		            cout << endl;                                                //CHANGE HERE

                            guess = -1;
		            num = rand() % 100 + 1;
		        
		            cout << "\n\n\n";
	                    cout << setw(5) << " " << num << "\n"; //Print number to test
			}                           
		        else if (guess < num && counter < 6)// ****THIS SAYS ERROR EXPECTED 'WHILE'	
			{
				cout << setw(5) << " " << "Your guess is too low try again: ";
			}
			else if (guess > num && counter < 6)
			{
				cout << setw(5) << " " << "Your guess is too high try again: ";
			}

			if (counter > 5)
			{
			    cout << setw(5) << " " << "Sorry you lose. The number is: " << num << "\n";
			    cout << endl;
				
		            cout << "Do you want to play again? (Y/N)?";
		            cin >> letter;

		            letter = toupper(letter);

		            system("cls");
		            counter = 0;
		            cout << endl;
			}
		}
	
	}

	return 0;
}


Your problem is that If you want to be able to play the game over and over again, you can't have do while loops, because when the condition for the while statement isn't met any more, the do will not be executed any more and you can't go back to it.

If you look at how I edited your code: I replaced the do while loop by a simple while loop (line 29)


Also lines 36 through 39:
1
2
3
4
    if (guess == num) //
    cout << setw(5) << " " << "Bingo!!\n\n";
    //win = true;
    break;


With an if statement, if you don't put any {}, it will only affect the ONE following line. If you want to put multiple statements after if don't forget your {} (same goes for if, else, else if, for, while, do).

In that if statement I removed the break; because that, once again will cause your while loop to break. Since you want to be able to replay the game over and over again, you don't want that.

Lines 49 through 53:
1
2
    guess = -1;
    [code]num = rand() % 100 + 1;


cout << "\n\n\n";
cout << setw(5) << " " << num << "\n"; //Print number to test[/code]

This is when player guesses the right number and choses to continue. guess = -1; resets the guess variable to -1 (so that it can't be equal to num). num = rand() % 100 + 1; resets the variable num to another random number. Then the game continues...


You'll fine //CHANGE HERE on your code where I changed things around.


Hope this helps,

Regards,

Hugo.


EDIT: Compiling gives these errors:
 In function 'int main()':
15:6: warning: unused variable 'winner' [-Wunused-variable]
16:6: warning: unused variable 'loser' [-Wunused-variable]
17:6: warning: unused variable 'winCount' [-Wunused-variable]
18:6: warning: unused variable 'loseCount' [-Wunused-variable]
20:7: warning: unused variable 'win' [-Wunused-variable]


I don't know If you're planning on using those, but for now you're not using any of the following variables: winner, loser, winCount, loseCount, win.
Last edited on
Thank you Hugo for the time you took to correct my mistakes. Yes I am going to use those variables later to add the win losses counters. There will probably be more tears along the way though :)
My pleasure. Glad to be of any help!
Don't hesitate to ask for help when those tears come :)
Thank you. There will be many tears. I cannot believe I spent all that time in those two classes and that teacher didn't teach anything but concepts. At least I am trying to learn it now though. This is exactly why I took the summer off from classes just so I can try to learn what I should have already learned in class. :)

Between this forum, my books, the internet and the teacher form a different teacher I might come out knowing some of this enough to get good.
I'd say that the best thing to do for now, especially if you're beginning, is to acquire mileage.

By doing things over and over, you're going to get mileage and things will get easier and easier as time goes. I'm sure your hard work will pay, if you keep on that way! :)
I sure hope so . I am now trying to build a program that will take a user choice number and then print all the prime numbers between 3 and that number. Been racking my brain all day to come up with pseudocode to help me get a plan. this is what I have but it seems I may be overthinking it.
Take number given as upper limit number.

start count at i=3 to print out
check next number i++
check if number divides by 2, 3, 4, etc.. with no remainder
if it does then number is not prime or if % == 0 it is not prime
if number is prime, print number.

I think it could done better with an array but the other teacher is starting me back at beginning which is not up to arrays yet, because I did not learn before. Two semesters and 8 months of my life gone over that class. I am a straight A student but A's don't help if I cannot actually program.
Last edited on
Topic archived. No new replies allowed.