Sentinel problems

Write your question here.

Hey everybody my project is to make a simple guessing game. Well I've done that part but I later read that the user must be able to terminate the program if they enter 9999. I tried while(guess!=9999)but I don't know where logically to put the brackets. If there is a better way to do it please let me know. Thanks guys.

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>
#include <cstdlib>  //rand and srand functions location.
#include <ctime>    //time function location.

int main ( )
{


   int num = 0;            //the random number to guess.
   const int MAX_n = 100;  //max possible number to guess (will create max random number of 100)
   int guess;
   int count=1;
   char again;

do
{
	
   //seed random-number generator (set up for a random number).
   std::srand (time(NULL));    //time is used for the seed, since time is always changing.

   //generate a random number 1-100 inclusive. Uses modulus (%) to restrict the range.
   num = (std::rand() % MAX_n) +1;    //the +1 takes the 0-99 rand result up to 1-100.





std::cout<<num;
std::cout << "Welcome to the guessing game!\n" << "Try and guess my number. Its between 1-100 (inclusive).";
std::cin>>guess;



for (;count<5;count++)
{
	
	while(guess>100 || guess<1)
	{
	std::cout<<"Sorry your guess has to be 1 thru 100\n. Try again";
	std::cin>>guess;
	}	
	
		if (guess>num)
		{
		std::cout<<"Your guess is too high.\n Try again.";
		std::cin>>guess;
		}
		else if (guess<num)
		{
		std::cout<<"Your guess is too low.\n Try again.";
		std::cin>>guess;
		}
		else if (guess==num)
		{
		std::cout<<"Congratulations you guessed my number\n";
		std::cout<<"It took you "<< count << " try/tries.\n";
		break;  //exit loop if correct
		
		        // <<"It took you "<< # of iterations " try/tries." 
		}
	
		

}//End of for with counter


std::cout<<"Would you like to try again? (Y/N)";
std::cin>>again;
}while (again=='Y'||again=='y');



return (0);
}
  
If you have been introduced to functions:

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
#include <iostream>

bool get_guess( int& guess, int sentinal )
{
    std::cout << "guess the number (enter " << sentinal << " to end program): " ;
    return std::cin >> guess && guess != sentinal ;
}

bool try_again()
{
    std::cout << "would you like to try again? (Y/N): " ;
    char again ;
    return std::cin >> again && ( again == 'y' || again == 'Y' ) ;
}

int main()
{
    const int MAX_GUESSES = 5 ;
    const int SENTINAL = 9999 ;

    int guess = 0 ;
    do
    {
        for( int i = 0 ; i < MAX_GUESSES && get_guess( guess, SENTINAL ) ; ++i )
        {
            std::cout << "guess #" << i+1 << " is " << guess << '\n' ;
            // ...
        }
    }
    while( guess != SENTINAL && try_again() ) ;
}
Last edited on
Topic archived. No new replies allowed.