How to restart my game?

I have a little guessing game from 1 - 100 and you have to guess the right number, but problem is, my assignment requires me to ask the user if the user wants to replay the game at the end, how would I go about doing so?


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>
#include <time.h>

using namespace std;

int main()
{
	srand((unsigned)time(0));
	int answer;
	for (int i = 0; i < 1; i++)
	{
		answer = (rand()%100)+1;
	}
	//Rules of the game

	cout << "           Guessing Game Rules\n";
	cout << endl << "The program will simply generate 1 - 100\n";
	cout << "randomly targeting 1 number for the player\n";
	cout << "  to try and guess the correct number.\n";

	//Begin the game
	cout << endl << "===========================================\n";
	cout << endl << "                 Points\n";
	cout << endl << "Try and guess the correct answer the least\n";
	cout << "as you can to earn more points and a title!\n";

	int count = 0;
	int num;
	int point = 100;

	cout << endl << "The program has generated a number!\n";
	cout << "What is the generated digit? ";
	cin >> num;
	

	//Calculate point + count
	while (num != answer)
	{
		cout << endl << "That is not the right answer!\n";
	if (num > answer)
	{
		point -= 5;
		count += 1;
		cout << endl << "You have guessed too high! -5 pts\n";
		cout << "What is the generated digit? ";
		cin >> num;
	}
	else
	{
		point -= 5;
		count += 1;
		cout << endl << "You have guessed too low! -5 pts\n";
		cout << "What is the generated digit? ";
		cin >> num;
	}
	}
	
	if (num == answer)
	{
		point -= count * 5;
		cout << endl << "Congradulations! You have guessed the right number!\n";
		cout << endl << "It took you " << count << " tries! Total Points: " << point;
	}

	//Titles
	if (count >= 8 )
	{
		cout << endl << "Try improving your luck and come try again!\n";
	}
	else if (count >= 6)
	{
		cout << endl << "You are very renowned!\n";
	}
	else if (count >= 4)
	{
		cout << endl << "You are the guessing master!\n";
	}
	else
	{
		cout << endl << "You are now the god of prediction!\n";
	}
}
You can put the code inside a loop that will run until it's time to exit. At the end of the loop you can ask the user if he wants to play again.
What do you mean Peter? I tried a do while loop within the whole code like what I did with this other little assignment but it doesn't work.


http://pastie.org/3928136
while ((ans != 'Y')&&(ans != 'y')&&(ans != 'N')&&(ans != 'n'));
This will make the loop run again only if the user enters anything other than 'Y', 'y', 'N' or 'n'. You could change it to
while (ans == 'Y' || ans == 'y');
or
while (ans != 'N' && ans != 'n');
Thanks peter, that makes a lot more sense lol.

What about this? Still doesn't work, saids my ans isn't being used correctly.

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
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

int main()
{
	char ans;
	do
	{
	srand((unsigned)time(0));
	int answer;
	for (int i = 0; i < 1; i++)
	{
		answer = (rand()%100)+1;
	}
	//Rules of the game

	cout << "           Guessing Game Rules\n";
	cout << endl << "The program will simply generate 1 - 100\n";
	cout << "randomly targeting 1 number for the player\n";
	cout << "  to try and guess the correct number.\n";

	//Begin the game
	cout << endl << "===========================================\n";
	cout << endl << "                 Points\n";
	cout << endl << "Try and guess the correct answer the least\n";
	cout << "as you can to earn more points and a title!\n";

	int count = 0;
	int num;
	int point = 100;
	

	cout << endl << "The program has generated a number!\n";
	cout << "What is the generated digit? ";
	cin >> num;
	

	//Calculate point + count
	while (num != answer)
	{
		cout << endl << "That is not the right answer!\n";
	if (num > answer)
	{
		point -= 5;
		count += 1;
		cout << endl << "You have guessed too high! -5 pts\n";
		cout << "What is the generated digit? ";
		cin >> num;
	}
	else
	{
		point -= 5;
		count += 1;
		cout << endl << "You have guessed too low! -5 pts\n";
		cout << "What is the generated digit? ";
		cin >> num;
	}
	}
	
	if (num == answer)
	{
		point -= count * 5;
		cout << endl << "Congradulations! You have guessed the right number!\n";
		cout << endl << "It took you " << count << " tries! Total Points: " << point;
	}

	//Titles
	if (count >= 8 )
	{
		cout << endl << "Try improving your luck and come try again!\n";
	}
	else if (count >= 6)
	{
		cout << endl << "You are very renowned!\n";
	}
	else if (count >= 4)
	{
		cout << endl << "You are the guessing master!\n";
	}
	else
	{
		cout << endl << "You are now the god of prediction!\n";
	}
		cout << endl << "Would you like to continue? (Y / N): ";
	}
	while ((ans == 'Y')&&(ans == 'y'));
	
	
}


The problem is on line 89, the while statement is only true if ans is equal to both Y and y. However, ans cannot be 2 different characters at once. The pipe separator, ||, would solve this, allowing your program to repeat if ans is equal to Y or y.

well written code otherwise.
Last edited on
Topic archived. No new replies allowed.