Play aga`in loop broken

OK, so the program is working minus the play again loop. I even tried a goto statement for it but no luck. Basically what its doing is after the game runs its course it asks if you would like to play again? If you hit Y it starts over but if you it N it starts over. I want it to out put thank you for playing and close after user hits a button.


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
// C// Guess My Number
// The classic number guessing game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
int main()
{
	
    srand(static_cast<unsigned int>(time(0)));  //seed random number generator

	int secretNumberE = rand() % 10 + 1; 
	int secretNumberM = rand() % 100 + 1;// random number between 1 and 100
	int secretNumberH = rand() % 1000 + 1;
	int tries = 0;
	int guess;
    char play;
	cout << "\tWelcome to Guess My Number\n\n";
START:
	cout << "Difficulty Levels\n\n";
    cout << "1 - Easy\n";
    cout << "2 - Normal\n";
    cout << "3 - Hard\n\n";

 
	int choice;
    cout << "Choice: ";
    cin >> choice;

    switch (choice)
{
    case 1:
    cout << "You picked Easy.\n";
	do
	{
		cout << "Enter a guess 1-10: ";
		cin >> guess;
		++tries;

		if (guess > secretNumberE)
		{
			cout << "Too high!\n\n";
		}
		else if (guess < secretNumberE)
		{
			cout << "Too low!\n\n";
		}
		else
		{
			cout << "\nThat's it! You got it in " << tries << " guesses!\n";
		}

	} while (guess != secretNumberE);
	goto BEGIN;
	break;
	case 2:
	cout << "You picked Normal.\n";
	do
	{
		cout << "Enter a guess 1-100: ";
		cin >> guess;
		++tries;

		if (guess > secretNumberM)
		{
			cout << "Too high!\n\n";
		}
		else if (guess < secretNumberM)
		{
			cout << "Too low!\n\n";
		}
		else
		{
			cout << "\nThat's it! You got it in " << tries << " guesses!\n";
		}

	} while (guess != secretNumberM);
	goto BEGIN;
	break;
	case 3:
	cout << "You picked Hard.\n";
do
	{
		cout << "Enter a guess 1-10: ";
		cin >> guess;
		++tries;

		if (guess > secretNumberH)
		{
			cout << "Too high!\n\n";
		}
		else if (guess < secretNumberH)
		{
			cout << "Too low!\n\n";
		}
		else
		{
			cout << "\nThat's it! You got it in " << tries << " guesses!\n";
		}

	} while (guess != secretNumberH);
	goto BEGIN;
	break;
	BEGIN:
		cout << "Would you like to play again? y or n: ";
	cin >> play;
	if ( play == 'y' || 'Y') {
    goto START;
}
	else if ( play == 'n' || 'N') {
	cout << "Thank you for playing ";
	goto END;
}
	default:
	cout << "You made an illegal choice.\n";
	goto START;
	END:
    return 0;
}
}
Take out the goto statements. It's a very bad way to program. This is quite a small program and I'm already finding it f**king annoying to follow the code. Try something like this:

1
2
3
4
5
6
while(play == 'y' || play == 'Y')
{ 
    //Put all the game play code here. 
   std::cout << Do you want to play again y/n?;
   cin >> play;
}


It will make the code much better and easier to read and will also fix your bug.
Last edited on
Topic archived. No new replies allowed.