making a game

how do i fix my switch case

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  #include <iostream>
#include <cstdlib>
#include <time.h>
#include <string> 

using namespace std;

int main()
{
    bool play_again = true;
    while(play_again == true)
    {
      srand(time(NULL));  
      int number =  rand() % 1000 + 1;
      bool is_guess_correct = false; 
  	  int input_number; 
  	  int attempts_count = 1;
	  int attempts_left = 10;
	  int type = rand() % 10 + 1;
	  std::cout << "Welcome to the random number game" << endl;
	  std::cout << "You have 10 attempts to guess the number" << endl;
	  std::cout << "If you get it wrong you die" << endl;
	  std::cout << "If you get it right you might live" << endl;

    
  	  while(is_guess_correct == false) 
  	  {
  		    if(attempts_count == 1)
  		    {
  				  std::cout << "Enter Number : ";
  		    }
  		    else
  		    {
    				std::cout << "Enter Number Again : ";
  		    }
  		    std::cin >> input_number;
  		    if(input_number == number)
  		    {

    				 std::cout << "Congratulation! You have guessed the correct number in " << attempts_count << " attempts" << endl;
    				 is_guess_correct = true;
  		    }
  		    else if(input_number != number)
  		    {
    				 attempts_count++;
					 attempts_left--;
    				 if(input_number < number) 
    				 {
						 std::cout << "you have " << attempts_left << " attempts left" << endl;
    				   std::cout << "Entered number is smaller than then number to be guess." << endl;
    				 }
					 else
    				 {
						 std::cout << "you have " << attempts_left << " attempts left" << endl;
    				   std::cout << "Entered number is greater than then number to be guess." << endl;
    				 }
				while(attempts_left == 0)
					switch(type)
				{
					case 1: 
						cout << "Its time to die" << endl;
						cout << "You will die by: falline 10000 feet... have a safe death :)" << endl;
						break;
					case 2:
						cout << "Its time to die" << endl;
						cout << "You will die by: a blind man shooting you... have a safe death :)" << endl;
						break;
					case 3:
						cout << "Its time to die" << endl;
						cout << "You will die by: heat stroke... have a safe death :)" << endl;
						break;
					case 4:
						cout << "Its time to die" << endl;
						cout << "You will die by: drowining... have a safe death :)" << endl;
						break;
					case 5:
						cout << "Its time to die" << endl;
						cout << "You will die by: a million bees... have a safe death :)" << endl;
						break;
					case 6:
						cout << "Its time to die" << endl;
						cout << "You will die by: a beaver... have a safe death :)" << endl;
						break;
					case 7:
						cout << "Its time to die" << endl;
						cout << "You will die by: 15 heart attacks in a row... have a safe death :)" << endl;
						break;
					case 8:
						cout << "Its time to die" << endl;
						cout << "You will die by: decapitation... have a safe death :)" << endl;
						break;
					case 9:
						cout << "Its time to die" << endl;
						cout << "You will die by: Hanging from the eiffel tower... have a safe death :)" << endl;
						break;
					case 10:
						cout << "Its time to die" << endl;
						cout << "You will die by: explosive decompression... have a safe death :)" << endl;
						break;
					default:
						cout << "you live" << endl;
				}
				cout << "How was your death hahahaha" << endl;
				
			}
  	  }
      string choice;
      std::cout << "Press Y to play again or any other key to terminate : ";
      std::cin >> choice;
      if(choice != "Y" && choice != "y")
      {
        play_again = false;
      }
	 }
	system("pause");
   return 0; 
}
Last edited on
What do you mean by "fix my switch case" ? What is the problem that you're seeing?

You'd be amazed how much quicker this is, when you don't make us play guessing games.
i need to figure out how to have it output the
1
2
3
4
5
6
7
8
9
10
  
}
      string choice;
      std::cout << "Press Y to play again or any other key to terminate : ";
      std::cin >> choice;
      if(choice != "Y" && choice != "y")
      {
        play_again = false;
      }
	  

right after it tells you what death you have i fixed the switch case but now its how to output that after you die
Why do you have a while loop at line 57? This will cause your switch loop to execute indefinately.

Your cout at line 103 is in the wrong place. It appears after every guess.

srand() at line 13 should not be called inside a loop. It reinitializes the random number generator. It should be before the loop.

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
        if (attempts_left == 0)
        {  // no attempt left				    
            cout << "Its time to die" << endl;  // no need to repeat in every case
            switch (type)
            {
            case 1:   cout << "You will die by: falline 10000 feet... have a safe death :)" << endl;
    	      break;
...etc 
            //  Default will never be reached (type can only be 1-10)
            }  // end of switch
            cout << "How was your death hahahaha" << endl;
            is_guess_correct = true;  // terminate inner while loop
        } // end of if statement
    }  // end of inner while (is_correct_guess) loop
    //  Play again code goes here 
} // end of outer while loop 

Last edited on
whats wrong with it 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string> 

using namespace std;

int main()
{
    bool play_again = true;
	 srand(time(NULL));  
    while(play_again == true)
    {
     
      int number =  rand() % 1000 + 1;
      bool is_guess_correct = false; 
  	  int input_number; 
  	  int attempts_count = 1;
	  int attempts_left = 10;
	  int type = rand() % 10 + 1;
	  std::cout << "Welcome to the random number game" << endl;
	  std::cout << "You have 10 attempts to guess the number" << endl;
	  std::cout << "If you get it wrong you die" << endl;
	  std::cout << "If you get it right you might live" << endl;

    
  	  while(is_guess_correct == false) 
  	  {
  		    if(attempts_count == 1)
  		    {
  				  std::cout << "Enter Number : ";
  		    }
  		    else
  		    {
    				std::cout << "Enter Number Again : ";
  		    }
  		    std::cin >> input_number;
  		    if(input_number == number)
  		    {

    				 std::cout << "Congratulation! You have guessed the correct number in " << attempts_count << " attempts" << endl;
    				 is_guess_correct = true;
  		    }
  		    else if(input_number != number)
  		    {
    				 attempts_count++;
					 attempts_left--;
    				 if(input_number < number) 
    				 {
						 std::cout << "you have " << attempts_left << " attempts left" << endl;
    				   std::cout << "Entered number is smaller than then number to be guess." << endl;
    				 }
					 else
    				 {
						 std::cout << "you have " << attempts_left << " attempts left" << endl;
    				   std::cout << "Entered number is greater than then number to be guess." << endl;
    				 }
				if(attempts_left == 0)
					switch(type)
				{
					case 1: 
						cout << "Its time to die" << endl;
						cout << "You will die by: falline 10000 feet... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						cout << "\\ /" << endl;
						cout <<	" |" << endl;
						cout << "/o\\" << endl;
						break;
					case 2:
						cout << "Its time to die" << endl;
						cout << "You will die by: a blind man shooting you... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						cout << " O      " << endl;
						cout << "(Y==o---" << endl;
						cout << "/_\      " << endl;
						cout << "/ >    " << endl;
						break;
					case 3:
						cout << "Its time to die" << endl;
						cout << "You will die by: heat stroke... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						          
cout << "               \		" << endl;
cout << "                 \    	" << endl;
cout << "              .    \     	" << endl;
cout << "                     \.       	" << endl;
cout << "          ~-_    *  ./	" << endl;
cout << "             ~-_   /       	" << endl;
cout << "        *       ~-/            	" << endl;
cout << "          .      |     	" << endl;
cout << "              * |        	" << endl;
cout << "     -----------|      	" << endl;
cout << "       .        |           	" << endl;
cout << "             *   |    	" << endl;
cout << "                _-\       	" << endl;
cout << "          .  _-~ . \               	" << endl;
cout << "          _-~       `\                 	" << endl;
cout << "         ~           /`--___   ___           	" << endl;
cout << "                *  /        ---    	" << endl;
cout << "                 /     *     |     	" << endl;
cout << "               /             |          	" << endl;
cout << "     \\o/                  .  |       	" << endl;
cout << "     _|                      |	" << endl;
cout << "                             |	" << endl;
						break;
					case 4:
						cout << "Its time to die" << endl;
						cout << "You will die by: drowining... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
cout << " ::::::::::::::::::::::::::::::::::::::::::::::::::::::	" << endl;
cout << " ::::::::::::::::::::::::::::::::::::::::::::::::::::::	" << endl;
cout << " ::::::::::::::::::::::::::::::::::::::::::::::::::::::	" << endl;
cout << " ::::::::::::::::::::::::::_:::::::::::::::::::::::::::	" << endl;
cout << " :::::::::::::::::::::::::/ |::::::::::::::::::::::::::	" << endl;
cout << " :::::::::::::::::::::/ \\ | | /\\:::::::::::::::::::::::	" << endl;
cout << " ::::::::::::::::::::::\\ \\| |/ /:::::::::::::::::::::::	" << endl;
cout << " :::::::::::::::::::::::\\ Y | /___:::::::::::::::::::::	" << endl;
cout << " :::::::::::::::::::::.-.) '. `__/:::::::::::::::::::::	" << endl;
cout << " ::::::::::::::::::::(.-.   / /::::::::::::::::::::::::	" << endl;
cout << " ~^~_-~^^-^~^_~^_^-~^~^-~| ' |~^~_-~^_^-^~^_~^_^-~^~^-~	" << endl;
cout << " ~^-_~^-~^_~^-~^_~^-_ _^,|___| ,~_ _~^-_~^-~^_~^-~^_~^-	" << endl;
cout << " ~^-~^~-_~^__.===~'`__..[_____]-..__`'~===.__~^-~^~-_~^	" << endl;
cout << " ~_~^_.=~~'   ~_.==~-.-~|     |~=.-~==._~^-^'~~=._~_~^	" << endl;
cout << " ~-:`-~^-~^_~^:-~^~-_~-.`-===-'_.=~-_~^-_:~^-~^-_~`;-~	" << endl;
cout << "  ~-'._~^-~^-_^~=._~-~_~-'~~'~`_^-~_^_.=~-~^-_~^-_.'^-	" << endl;
cout << " _~^-~^~=._~^-~^_-^~~==..,~_^_,..==~~-_~^-~^-_.=~_~^-~^	" << endl;
cout << " _-~^-~^_~^`~==.__-~^_~^-_~^-_~^-_~^-~__.==~`_-~^-~^_~^	" << endl;
cout << " -~_~^~-~^-~^~_~^~`~~~==,,....,,==~~~`-~_~^~-~^-~^~_~^~	" << endl;
cout << "  ~jgs^-~^-_~^~^_-^~^-~^~-_~^-~^-~^_~^~-~^~-~^-~^-~^-~^	" << endl;
cout << "  ~^~^-~^-~^_~^~-^~_~^-^~^~^-~^-~^~^~-^~-~^-~^~~-^~-^~^	" << endl;
						break;
					case 5:
						cout << "Its time to die" << endl;
						cout << "You will die by: a million bees... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
cout << "				...vvvv)))))).	" << endl;
cout << "       /~~\\               ,,,c(((((((((((((((((/	" << endl;
cout << "      /~~c \\.         .vv)))))))))))))))))))\\``	" << endl;
cout << "          G_G__   ,,(((KKKK//////////////'	" << endl;
cout << "        ,Z~__ '@,gW@@AKXX~MW,gmmmz==m_.	" << endl;
cout << "       iP,dW@!,A@@@@@@@@@@@@@@@A` ,W@@A\\c	" << endl;
cout << "       ]b_.__zf !P~@@@@@*P~b.~+=m@@@*~ g@Ws.	" << endl;
cout << "          ~`    ,2W2m. '\[ ['~~c'M7 _gW@@A`'s	" << endl;
cout << "            v=XX)====Y-  [ [    \\c/*@@@*~ g@@i	" << endl;
cout << "           /v~           !.!.     '\\c7+sg@@@@@s.	" << endl;
cout << "          //              'c'c       '\\c7*X7~~~~	" << endl;
cout << "         ]/                 ~=Xm_       '~=(Gm_.	" << endl;

						break;
					case 6:
						cout << "Its time to die" << endl;
						cout << "You will die by: a beaver... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 7:
						cout << "Its time to die" << endl;
						cout << "You will die by: 15 heart attacks in a row... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 8:
						cout << "Its time to die" << endl;
						cout << "You will die by: decapitation... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 9:
						cout << "Its time to die" << endl;
						cout << "You will die by: Hanging from the eiffel tower... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 10:
						cout << "Its time to die" << endl;
						cout << "You will die by: explosive decompression... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					default:
						cout << "you live" << endl;
				}
				    
				if(is_guess_correct = true)
				{
      string choice;
      cout << "Press Y to play again or any other key to terminate : ";
      cin >> choice;
      if(choice != "Y" && choice != "y")
      {
        play_again = false;
      }
				}
				}
				}
				}
	 
	 
	system("pause");
   return 0; 
}
Last edited on
come on guys i really need help
what kind of help?

if(is_guess_correct = true)
Helps if you listen to the advice given to begin with. Also if you are doing using namespace std; you no longer need std:: in front of the standard library calls such as string, cin, cout, or endl. You have to move the rand() call out of the while loop because every time it starts the while loop over for the next guess it would change the number to be guessed, but you have to call it again if the play wants to play again or else they will be guessing the same number. Had to change the other while loop because it was an infinite loop and would never end.
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  #include <iostream>
#include <cstdlib>
#include <time.h>
#include <string> 

using namespace std;

int main()
{
    bool play_again = true;
    srand(time(NULL));
    int number = rand() % 1000 + 1;
    int type = rand() % 10 + 1;
    while(play_again == true)
    {
      // srand(time(NULL));  // move outside the loop
      // int number =  rand() % 1000 + 1;  // move outside the loop
      bool is_guess_correct = false; 
  	  int input_number; 
  	  int attempts_count = 1;
	  int attempts_left = 10;
	  // int type = rand() % 10 + 1;  // move outside of loop
	  std::cout << "Welcome to the random number game" << endl;
	  std::cout << "You have 10 attempts to guess the number" << endl;
	  std::cout << "If you get it wrong you die" << endl;
	  std::cout << "If you get it right you might live" << endl;
	  // cout << number << endl; // output answer for easier testing

    
  	  while(is_guess_correct == false) 
  	  {
  		    if(attempts_count == 1)
  		    {
  				  std::cout << "Enter Number : ";
  		    }
  		    else
  		    {
    				std::cout << "Enter Number Again : ";
  		    }
  		    std::cin >> input_number;
  		    if(input_number == number)
  		    {

    				 std::cout << "Congratulation! You have guessed the correct number in " << attempts_count << " attempts" << endl;
    				 is_guess_correct = true;
  		    }
  		    else if(input_number != number)
  		    {
    				 attempts_count++;
					 attempts_left--;
    				 if(input_number < number) 
    				 {
						 std::cout << "you have " << attempts_left << " attempts left" << endl;
    				   std::cout << "Entered number is smaller than then number to be guess." << endl;
    				 }
					 else
    				 {
						 std::cout << "you have " << attempts_left << " attempts left" << endl;
    				   std::cout << "Entered number is greater than then number to be guess." << endl;
    				 }
				// while(attempts_left == 0) // becomes an infinite loop
				if(attempts_left == 0)
					switch(type)
				{
					case 1: 
						cout << "Its time to die" << endl;
						cout << "You will die by: falline 10000 feet... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 2:
						cout << "Its time to die" << endl;
						cout << "You will die by: a blind man shooting you... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 3:
						cout << "Its time to die" << endl;
						cout << "You will die by: heat stroke... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 4:
						cout << "Its time to die" << endl;
						cout << "You will die by: drowining... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 5:
						cout << "Its time to die" << endl;
						cout << "You will die by: a million bees... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 6:
						cout << "Its time to die" << endl;
						cout << "You will die by: a beaver... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 7:
						cout << "Its time to die" << endl;
						cout << "You will die by: 15 heart attacks in a row... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 8:
						cout << "Its time to die" << endl;
						cout << "You will die by: decapitation... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 9:
						cout << "Its time to die" << endl;
						cout << "You will die by: Hanging from the eiffel tower... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					case 10:
						cout << "Its time to die" << endl;
						cout << "You will die by: explosive decompression... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						break;
					default:
						cout << "you live" << endl;
				}
				// cout << "How was your death hahahaha" << endl; // put it into each case
				
			}
  	  
  	  }
      char choice;
      std::cout << "Press Y to play again or any other key to terminate : ";
      std::cin >> choice;
      if(choice == 'Y' || choice == 'y')
      {
		  number = rand() % 1000 + 1;
        play_again = true;
      }
      else
      {
		  play_again = false;
	  }
	 }
	system("pause");
   return 0; 
}
Last edited on
this is making me mad haha it works if i guess the correct number but if my attempts are up it tells me my death and still outputs enter number again
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
 #include <iostream>
#include <cstdlib>
#include <time.h>
#include <string> 

using namespace std;

int main()
{
    bool play_again = true;
    srand(time(0));
    int number = rand() % 1000 + 1;
    int type = rand() % 10 + 1;
    while(play_again == true)
    {
      
      int number =  rand() % 1000 + 1; 
      bool is_guess_correct = false; 
  	  int input_number; 
  	  int attempts_count = 1;
	  int attempts_left = 10;
	  int type = rand() % 10 + 1;
	  std::cout << "Welcome to the random number game" << endl;
	  std::cout << "You have 10 attempts to guess the number" << endl;
	  std::cout << "If you get it wrong you die" << endl;
	  std::cout << "If you get it right you might live" << endl;
	  cout << number << endl;

    
  	  while(is_guess_correct == false) 
  	  {
  		    if(attempts_count == 1)
  		    {
  				  std::cout << "Enter Number : ";
  		    }
  		    else
  		    {
    				std::cout << "Enter Number Again : ";
  		    }
  		    std::cin >> input_number;
  		    if(input_number == number)
  		    {

    				 std::cout << "Congratulation! You have guessed the correct number in " << attempts_count << " attempts" << endl;
    				 is_guess_correct = true;
  		    }
  		    else if(input_number != number)
  		    {
    				 attempts_count++;
					 attempts_left--;
    				 if(input_number < number) 
    				 {
						 std::cout << "you have " << attempts_left << " attempts left" << endl;
    				   std::cout << "Entered number is smaller than then number to be guess." << endl;
    				 }
					 else
    				 {
						 std::cout << "you have " << attempts_left << " attempts left" << endl;
    				   std::cout << "Entered number is greater than then number to be guess." << endl;
    				 }
				while(attempts_left == 0)
					switch(type)
				{
					case 1: 
						cout << "Its time to die" << endl;
						cout << "You will die by: falline 10000 feet... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					case 2:
						cout << "Its time to die" << endl;
						cout << "You will die by: a blind man shooting you... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					case 3:
						cout << "Its time to die" << endl;
						cout << "You will die by: heat stroke... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					case 4:
						cout << "Its time to die" << endl;
						cout << "You will die by: drowining... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					case 5:
						cout << "Its time to die" << endl;
						cout << "You will die by: a million bees... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					case 6:
						cout << "Its time to die" << endl;
						cout << "You will die by: a beaver... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					case 7:
						cout << "Its time to die" << endl;
						cout << "You will die by: 15 heart attacks in a row... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					case 8:
						cout << "Its time to die" << endl;
						cout << "You will die by: decapitation... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					case 9:
						cout << "Its time to die" << endl;
						cout << "You will die by: Hanging from the eiffel tower... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					case 10:
						cout << "Its time to die" << endl;
						cout << "You will die by: explosive decompression... have a safe death :)" << endl;
						cout << "How was your death hahahaha" << endl;
						attempts_left = 1;
						break;
					default:
						cout << "you live" << endl;
				}
				
				
			}

  	  }
       char choice;
      std::cout << "Press Y to play again or any other key to terminate : ";
      std::cin >> choice;
      if(choice == 'Y' || choice == 'y')
      {
		  number = rand() % 1000 + 1;
        play_again = true;
      }
      else
      {
		  play_again = false;
	  }
	 }
	system("pause");
   return 0; 
}
If you are having "Enter Number Again : " show up when you do not want it to, use a cout for the condition that should prevent this message from showing again to see what it prints.

Last edited on
It is because you don't have anything set to make it exit the outermost while loop after you die so it starts over.
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include <iostream>
#include <cstdlib>
#include <time.h>
#include <string> 

using namespace std;

int main()
{
    bool play_again = true;
    bool dead = false;
    srand(time(NULL));
    int number = rand() % 1000 + 1;
    int type = rand() % 10 + 1;
    while(play_again == true)
    {
      bool is_guess_correct = false; 
  	  int input_number; 
  	  int attempts_count = 1;
	  int attempts_left = 10;
	  
	  cout << "Welcome to the random number game" << endl;
	  cout << "You have 10 attempts to guess the number" << endl;
	  cout << "If you get it wrong you die" << endl;
	  cout << "If you get it right you might live" << endl;
	      
  	  while(is_guess_correct == false) 
  	  {
  		    if(attempts_count == 1)
  		    {
  				  std::cout << "Enter Number : ";
  		    }
  		    else
  		    {
    				std::cout << "Enter Number Again : ";
  		    }
  		    cin >> input_number;
  		    if(input_number == number)
  		    {

    				 cout << "Congratulation! You have guessed the correct number in " << attempts_count << " attempts" << endl;
    				 is_guess_correct = true;
  		    }
  		    else if(input_number != number)
  		    {
    				 attempts_count++;
					 attempts_left--;
    				 if(input_number < number) 
    				 {
						 cout << "you have " << attempts_left << " attempts left" << endl;
						 cout << "Entered number is smaller than then number to be guess." << endl;
    				 }
					 else
    				 {
						 cout << "you have " << attempts_left << " attempts left" << endl;
    				     cout << "Entered number is greater than then number to be guess." << endl;
    				 }
				
				if(attempts_left == 0)
					switch(type)
					{
						case 1: 
							cout << "Its time to die" << endl;
							cout << "You will die by: falline 10000 feet... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						case 2:
							cout << "Its time to die" << endl;
							cout << "You will die by: a blind man shooting you... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						case 3:
							cout << "Its time to die" << endl;
							cout << "You will die by: heat stroke... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						case 4:
							cout << "Its time to die" << endl;
							cout << "You will die by: drowining... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						case 5:
							cout << "Its time to die" << endl;
							cout << "You will die by: a million bees... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						case 6:
							cout << "Its time to die" << endl;
							cout << "You will die by: a beaver... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						case 7:
							cout << "Its time to die" << endl;
							cout << "You will die by: 15 heart attacks in a row... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						case 8:
							cout << "Its time to die" << endl;
							cout << "You will die by: decapitation... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						case 9:
							cout << "Its time to die" << endl;
							cout << "You will die by: Hanging from the eiffel tower... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						case 10:
							cout << "Its time to die" << endl;
							cout << "You will die by: explosive decompression... have a safe death :)" << endl;
							cout << "How was your death hahahaha" << endl;
							dead = true;
							break;
						default:
							cout << "you live" << endl;
					}
				
				
			}
			if(dead)break;
  	  
  	  }
      char choice;
      cout << "Press Y to play again or any other key to terminate : ";
      cin >> choice;
      if(choice == 'Y' || choice == 'y')
      {
		  number = rand() % 1000 + 1;
          play_again = true;
          dead = false;
      }
      else
      {
		  play_again = false;
	  }
	 }
	system("pause");
   return 0; 
}
A couple of additional comments.
1) You're going to die the same way every time. You calculate type once in the beginning. You should calculate type inside the main loop.

2) As I pointed out in a previous reply, you will never receive a "You live" response. type can only be 1-10. You will never reach the default case.

I'd probably organize this a little differently myself. Try to think of your problem in bigger chunks, then drill down until you've got everything defined. It's called top-down programming, and I think it will help you visualize how your code needs to be structured.

For instance:

1
2
3
4
5
6
7
8
9
10
int main()
{
do
{
promptuser();
getinput();
processinput();
}
while(playagain());
}


So now all we need to do is flesh out our code. You'll define each function one by one. You may even break those functions out into separate problems. After everything is defined, it should work as intended. Not to mention if anyone looks at your code they can easily see what you're trying to accomplish because main is a bit more readable and expositional.
If you still have not figured this out, you need to assign is_guess_correct to true when you have run out of chances. The name of the variable may have thrown you off. is_guess_correct should have been named more_guesses_allowed or something along those lines.

Also your final while loop is not properly bracketed. You bracketed the switch, but not the loop itself.
@AbstractionAnon
I only took a few of the fixes mentioned above and applied them to his code. Personally, I always put a default:break; statement even if it will never be executed as it makes a decent debugging technique. For example, lets say he had accidentally made it so that type went over 10, but if we know it should never drop through to default and we do a test and get "You live" then we instantly know type's range is off.

@horrifiicmonster
That is how every programmer does it, but it depends on the programmer as to how they ultimately implement that method. I would have rather broke it down into multiple files and functions than how he did it.

Unfortunately, you can see by his posts above, he hasn't paid much attention to our advice.

Here is the last code update I'm doing:
http://codeviewer.org/view/code:4029

@BHX Specter - I absolutely agree with you about the importance of always having a default branch. I wasn't suggesting the OP remove the default branch.

My point to the OP was that he has a logic error in his program. According to the comment in his line 26, the OP believes the the player "might live", but the current logic proves otherwise.
@AbstractionAnon
My apologies. I mistakenly took that as meaning to remove default:break; because it wouldn't be executed.
closed account (j3Rz8vqX)
Although not necessary, I wanted to mention an alternative to a switchs. Example:
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
using namespace std;

string nLine = "\n";
string image[10] =
{
    //Case 1:
    "",
    //Case 2:
    "",
    //Case 3:
    nLine +
    "               \		" + nLine +
    "                 \    	" + nLine +
    "              .    \     	" + nLine +
    "                     \.       	" + nLine +
    "          ~-_    *  ./	"
    "             ~-_   /       	" + nLine +
    "        *       ~-/            	" + nLine +
    "          .      |     	" + nLine +
    "              * |        	" + nLine +
    "     -----------|      	" + nLine +
    "       .        |           	" + nLine +
    "             *   |    	" + nLine +
    "                _-\       	" + nLine +
    "          .  _-~ . \               	" + nLine +
    "          _-~       `\                 	" + nLine +
    "         ~           /`--___   ___           	" + nLine +
    "                *  /        ---    	" + nLine +
    "                 /     *     |     	" + nLine +
    "               /             |          	" + nLine +
    "     \\o/                  .  |       	" + nLine +
    "     _|                      |	" + nLine +
    "                             |	",
    //Case 4:
    nLine +
    " ::::::::::::::::::::::::::::::::::::::::::::::::::::::	" + nLine +
    " ::::::::::::::::::::::::::::::::::::::::::::::::::::::	" + nLine +
    " ::::::::::::::::::::::::::::::::::::::::::::::::::::::	" + nLine +
    " ::::::::::::::::::::::::::_:::::::::::::::::::::::::::	" + nLine +
    " :::::::::::::::::::::::::/ |::::::::::::::::::::::::::	" + nLine +
    " :::::::::::::::::::::/ \\ | | /\\:::::::::::::::::::::::	" + nLine +
    " ::::::::::::::::::::::\\ \\| |/ /:::::::::::::::::::::::	" + nLine +
    " :::::::::::::::::::::::\\ Y | /___:::::::::::::::::::::	" + nLine +
    " :::::::::::::::::::::.-.) '. `__/:::::::::::::::::::::	" + nLine +
    " ::::::::::::::::::::(.-.   / /::::::::::::::::::::::::	" + nLine +
    " ~^~_-~^^-^~^_~^_^-~^~^-~| ' |~^~_-~^_^-^~^_~^_^-~^~^-~	" + nLine +
    " ~^-_~^-~^_~^-~^_~^-_ _^,|___| ,~_ _~^-_~^-~^_~^-~^_~^-	" + nLine +
    " ~^-~^~-_~^__.===~'`__..[_____]-..__`'~===.__~^-~^~-_~^	" + nLine +
    " ~_~^_.=~~'   ~_.==~-.-~|     |~=.-~==._~^-^'~~=._~_~^	    " + nLine +
    " ~-:`-~^-~^_~^:-~^~-_~-.`-===-'_.=~-_~^-_:~^-~^-_~`;-~	    " + nLine +
    "  ~-'._~^-~^-_^~=._~-~_~-'~~'~`_^-~_^_.=~-~^-_~^-_.'^-	    " + nLine +
    " _~^-~^~=._~^-~^_-^~~==..,~_^_,..==~~-_~^-~^-_.=~_~^-~^	" + nLine +
    " _-~^-~^_~^`~==.__-~^_~^-_~^-_~^-_~^-~__.==~`_-~^-~^_~^	" + nLine +
    " -~_~^~-~^-~^~_~^~`~~~==,,....,,==~~~`-~_~^~-~^-~^~_~^~	" + nLine +
    "  ~jgs^-~^-_~^~^_-^~^-~^~-_~^-~^-~^_~^~-~^~-~^-~^-~^-~^	" + nLine +
    "  ~^~^-~^-~^_~^~-^~_~^-^~^~^-~^-~^~^~-^~-~^-~^~~-^~-^~^    ",
    //Case 5:
    nLine +
    "				...vvvv)))))).	" + nLine +
    "       /~~\\               ,,,c(((((((((((((((((/	" + nLine +
    "      /~~c \\.         .vv)))))))))))))))))))\\``	" + nLine +
    "          G_G__   ,,(((KKKK//////////////'	" + nLine +
    "        ,Z~__ '@,gW@@AKXX~MW,gmmmz==m_.	" + nLine +
    "       iP,dW@!,A@@@@@@@@@@@@@@@A` ,W@@A\\c	" + nLine +
    "       ]b_.__zf !P~@@@@@*P~b.~+=m@@@*~ g@Ws.	" + nLine +
    "          ~`    ,2W2m. '\[ ['~~c'M7 _gW@@A`'s	" + nLine +
    "            v=XX)====Y-  [ [    \\c/*@@@*~ g@@i	" + nLine +
    "           /v~           !.!.     '\\c7+sg@@@@@s.	" + nLine +
    "          //              'c'c       '\\c7*X7~~~~	" + nLine +
    "         ]/                 ~=Xm_       '~=(Gm_.	",
    //Case 6:
    "",
    //Case 7:
    "",
    //Case 8:
    "",
    //Case 9:
    "",
    //Case 10:
    ""
};

int main()
{
    int type;
    bool dead = false;
    cout<<"Enter the type of death: ";
    cin>>type;
    do
    {
        const static string str[12] =                                                       //Array of strings
        {
                "Its time to die",                                                          //Death Prompt
                "You will die by: falline 10000 feet... have a safe death :)",              //Case: 01
                "You will die by: a blind man shooting you... have a safe death :)",        //Case: 02
                "You will die by: heat stroke... have a safe death :)",                     //Case: 03
                "You will die by: drowining... have a safe death :)",                       //Case: 04
                "You will die by: a million bees... have a safe death :)",                  //Case: 05
                "You will die by: a beaver... have a safe death :)",                        //Case: 06
                "You will die by: 15 heart attacks in a row... have a safe death :)",       //Case: 07
                "You will die by: decapitation... have a safe death :)",                    //Case: 08
                "You will die by: Hanging from the eiffel tower... have a safe death :)",   //Case: 09
                "You will die by: explosive decompression... have a safe death :)",         //Case: 10
                "How was your death hahahaha"                                               //Death Laugh
        };
        if(type>0)
        {
                cout<<str[0]<<endl;         //Death Prompt
                cout<<str[type]<<endl;      //Case of Death
                cout<<str[11]<<endl;        //Death Laugh
                cout<<image[type-1]<<endl;
                dead=true;
                break;
        }
        else
        {
                cout << "you live" << endl; //Default: You lived!
                break;//Breaking for this demonstration
        }
    }while(true);
    return 0;
}

In the above code, your output procedure would take fewer steps to print your death statements; in a switch, it would have had to consider cases 1 through 9 before printing out 10's prompt.

My intentions were not to change your code, use what you like and what you know best; it'll work either way.
Last edited on
Hey guys thanks for all the help and i tried the is_guess_correct and it didnt work for me i might have just screwed up putting it in the wrong place but yeah its the end of the school year and half the year i didnt pay attention cause i got into drugs but i straightened up and its been a little rough trying to figure the stuff i missed on my own but thanks for the help guys
Topic archived. No new replies allowed.