C++ Guessing game program switch statement that chooses random messages

I have to have 10 messages for when they win, lose and are asked to play another game that are chosen randomly to print. I am suppose to use a switch statement. So I have input some messages and would like to know if it is the right way to enter a switch statement also how would I get the program to randomly choose one. Also when my program runs if the guess is lower than the number it prints Too high try again and Too lower try again , when the number is too high it just prints Too high try again and when you get the right number it prints tries left too high try again and then all of the messages for when the user wins. How can I get the program to only print the appropriate statements? I also noticed that when the user chooses to play again the tries counter is not resetting with the game how do I fix 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
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
84
 #include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
char chr;

int main()
{
	srand(time(NULL));                                           //the function that generates random numbers
	int number=rand()%100+1;                                     //the range of the random numbers
	int guess;                                                   //The guess is stored here
	int tries=0;                                                 //The number of tries stored here
    char answer;                                                 //The answer to the question is stored here
	answer='y';                  

	while(answer=='y'||answer=='Y') 
	{
  	    while (tries<=20 && answer=='y'|| answer=='Y')
		{
		cout<<"Enter a number between 1 and 100 "<<endl;          //The user is asked for a guess
		cin>>guess;                                               //The guess is stored here
		tries++;                                                 //Adding a number for every try
	    if(guess==0||guess>100)                                  //If statement that produces an error message if user enters a number out of the peramiters
	    {
	     cout<<"This is not an option try again"<<endl;          //Error message
		}
		
		if(tries<20)                                            
	    cout<<"Tries left: "<<(20-tries)<<endl;                   //Output to let the user know how many guess they have left
		
		if(number<guess);                                         //if the guess is to high
		cout<<"Too high try again"<<endl;                         //This message prints if guess it to high
		  	
	    if(number>guess)                                           //if the guess is to low
		cout<<"Too low try again"<<endl;                          //This message prints if the guess is to low
	
		if(number==guess)                                          //If the user guesses the number
		{
		cout<<"You got the right number in "<<tries<<" tries"<<endl;  //Lets the user know how many guess they used
	    answer = 'n';
		}
		switch(number==guess)
		{
		case 1: cout<<"Winner Winner Chicken Dinner"<<endl;
		case 2: cout<<"Yippy you got it!"<<endl;
		case 3: cout<<"Congratualtions!! "<<endl;                          //Messages that print out if the user guesses correctly
		case 4: cout<<"Good Work!"<<endl;
		case 5: cout<<"That's how you do it!"<<endl;
		case 6: cout<<"You are a Pro."<<endl;
		case 7: cout<<"You have won!"<<endl;
		case 8: cout<<"You're Victoriuos"<<endl;
		case 9: cout<<"You have been triumphent."<<endl;
		case 10: cout<<"Nice work."<<endl;
		} 


		switch(tries >= 20)                                               //If the user uses all their guesses
		{
		case 1: cout << "You've run out of tries!"<<endl;                      //The message that prints when a user is out of guesses
		case 2: cout<<"You used all your guess."<<endl;
		case 3: cout<<"Bummer you are out of tries."<<endl;
		case 4: cout<<"Whoops all your guesses are gone."<<endl;
		case 5: cout<<"Time to play again no more guesses."<<endl;
		answer='n';
		}
		if(answer=='n')
		{
         cout<<"Would you like to play again?  Enter Y/N"<<endl;       //asking if play would like to play again
		 cin>>answer;                                                  //Store users answer
		 if (answer=='N'|| answer=='n')                                //if the user says no
		 cout<<"Thanks for playing!"<<endl;                            //This message prints out if they choose not to play again
	
		else
			number=rand()%100+1;                                        //This starts the game over if they choose to play again
		}

		}
		}
		
	cin>>chr;
		return 0;

}
 
For your attempt counter problem, you should assign the value 0 to tries inside the first while loop, right at the beginning. You could also move srand(time(NULL)) and rand() % 100 +1; there so you wouldn't have to repeat it at the end of your code. For picking up random output messages, just create a variable that will store another randomly generated number between 1 and 10 and just do switch(variable).

I wish I could be more clear, but my eyes are hurting so bad right now I got to go.

If anyone spots a mistake I might have done, please correct me as I am also a beginner.
Last edited on
Just a beginner here, so I don't know if this would be called a syntax error or logical. Annamarie correct me if I'm wrong please. From my expeirence and I'm 100% sure in this.

There should be no semicolon after if(number<guess)

I know how to so the tries left but I'm in my iPad and it's hard to type XD let me know once you have this semicolon fixed out. Meanwhile, is it necessary to use a switch statement on tries more then twenty? You can just use else statement. Just to make sure, did you run the program and see if everything is reading under the switch (guess==number)? I'm not so sure if that's right because if the number is 55 then there would be no case that holds true for it and there fore it would skip the switch statement.
Topic archived. No new replies allowed.