Trying to get code to work for my guessing game. Please Help!

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
I keep getting stuck trying to get a the portion with a return variable to work. Not sure if I am doing it right or not. I had the program working before I added the int reviewGuess(int randomNumber,int userChoice). 

[code]#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

bool Again()


   int reviewGuess(int RandomNumber, int Guess)
{
    const int HighestNum = 51;
    const int LowestNum = 0;
    int RandomNumber = LowestNum + rand() % HighestNum;
	int x;

    cout << "Guess the random number between " << LowestNum << " and " << HighestNum << "!\n\n";

    int attempts = 15;
    while (attempts != 0)
    {
        int Guess;
        cout << "You have " << attempts << " attempt" << (attempts > 1 ? "s" : "") << " remaining\n";

        cout << "Enter a number: ";
        cin >> Guess;
        cout << endl;

		if(RandomNumber == Guess)
		{
			x = 0;
		}
		else if(RandomNumber >  Guess )
		{
			x = 1;
		}
		if(RandomNumber < Guess)
		{
			x = -1;
		}
			switch (x)
			{
				case 1:
					 x = 0;
					cout << "You Win!!" << endl;
                                break;
				case 2:
					 x = 1;
					cout << "The number is higher than your guess" << endl;
                                 break;
				case 3:
					x = -1;
					cout << "The number is lower than your guess" <<endl;
                                 break;
				default:
					cout << "Try again, the number is between 1-50" << endl;
                                 break;
			}
					
		
        --attempts;
    }

    if (attempts == 0)
    {
        cout << "Sorry, no guesses remain. The random number was... " <<     RandomNumber << "!";//so the user can see the random number at the end of their attempts
        cout << "\n";
        cin.get();
        Again();
    }
return x;
}

bool Menu()
{
    cout << "Guess Number Game\n\n";

    cout << "Menu\n\n";

    cout << "1) Play Game\n";
    cout << "2) Exit\n\n";

    cout << "Enter your selection: ";
    int selection = 0;

    while (true)
    {
        cin >> selection;
        cout << endl;

        if (selection == 1)
			return true;
        else if (selection == 2)
            return false;
        else
            cout << "Try again, choose 1 or 2: ";
    }
}

bool Again()
{
    cout << "1) Play Again\n";
    cout << "2) Exit\n\n";

    cout << "Enter your selection: ";
    int selection = 0;

    while (true)
    {
        cin >> selection;
        cout << endl;

        if (selection == 1)
        {
            return true;
        }
        else if (selection == 2)
        {
            return false;
        }
        else
        {
            cout << "Try again, choose 1 or 2: ";
        }
    }
}

int main()
{
    srand(time(0));
    int Guess;
    int RandomNumber;

    bool play = Menu();
    if (play)
    while (true)
    {
       reviewGuess (RandomNumber, Guess);

        if (!Again())
            break;
    }

    return 0;
}[code]
Last edited on
Please edit your code and use code tags - http://www.cplusplus.com/articles/jEywvCM9/
Lines 35-43: Your case branches do not have break statements. This will cause the code to flow through to the next branch.

Line 48: --attempts will never be executed because of the preceding return statement.

Line 55:
 
void Again();

This is a function declaration, not a function call. Remove the void

Line 57: What value is returned if you fall through to the end of the function? Better question, why is this an int function since you don't use the return value.

Line 111:
 
int reviewGuess(int randomNumber,int guess);

Again a function declaration, not a function call.

My line numbers are approximate since you did not use code tags.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
When I remove the void and int it gives me errors every time I try to run it. I updated the breaks in the switch statement and moved the return x.
Thanks for adding the code tags. You don't need the
[code] tag
at line 141.

Line 67: You're trying to call Again(), but the compiler hasn't seen Again() yet since it appears at line 98. You need a forward declaration for it.

Line 134: Your call to reviewGuess needs arguments. At line 8, you declared that it takes two arguments, but you did not provide any in the call.
Now it is saying redefinition of formal parameters.
Show your latest code at the EXACT error messages.
Topic archived. No new replies allowed.