New to c++ and need finding an error in my code

Hello!
I have an upcoming project for one of my coding classes and I am having trouble finding where my error is in my code. The project outline is as follows.

Write a program that models a number guessing game. The program should ask the user to select a range of numbers(integers) from zero to a positive integer, by requesting that the user enter a number representing the top number of the range. Check that the user indeed enters a positive number. The program should use a random number generator to generate a magic number within this range. The program should give the user 5 chances to guess the value of the magic number. When the user enters a guess, the program should report whether the guessed value is too high or too low or correct. If the user is correct, the program should congratulate them and report the number of tries the user took to guess the magic number. If the user doesn't guess the magic number within 5 tries, the program should tell the user the value of the magic number and end.

This program must use 3 different functions AskRange, GetMagicNumber, and Guess. The main function should only call to these functions and include what is necessart to support them.

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
  #include<iostream>
#include<cstdlib>
#include <ctime>

using namespace std;

int AskRange()
{
	int n = -1;

	while (n <= 0)
	{
		cout << "Enter the top value for the guessing range: " << endl;
		cin >> n;
	
	if (n <= 0)
		{
			cout << "The top value must be a positive number greater than 0" << endl;
	    }
	}
	return n;
}

int GetMagicNumber(int top)
{
	int MagicNumber = rand() % top;
	return MagicNumber;
}

void Guess(int range)
{
	int GuessCounter = 0;
	int magicnumber = GetMagicNumber(range);
	int guess;
	bool win = false;

	cout << "I am thinking of a number between 0 and " << range << endl;

	while (GuessCounter < 5)
	{
		cout << "Please enter your guess: " << endl;
		cin >> guess;
	}

	if (guess > magicnumber)
		{
			cout << "Your guess is too high!" << endl;
		}
	else if (guess < magicnumber)
		{
			cout << "Your guess is too low!" << endl;
		}
	else if (guess = magicnumber)
		{
			cout << "Your correctly guessed the number I was thinking of! Congratulations!" << endl;
		}
	GuessCounter++;

	if (!win)
	{
		cout << "Game Over! Sorry, you ran out of guesses. The number I was thinking of was " << magicnumber << endl;
	}

	int main()
	; {
		int choice;
		int range;

		cout << "Welcome to the Number Guessing Game" << endl;

		do
		{
			range = AskRange();
			Guess(range);
			cout << "Would you like to play again? Y/N " << endl;
			cin >> choice;
		} while (choice == 'Y' || choice == 'y');
		cout << "Goodbye! I had fun." << endl;
		}
	  }
Tell us what makes you think there is an error in your code.

Your indentation is all over the place, by the way. It's not consistent, making it difficult to read. You have a semi-colon after line 64 which definitely shouldn't be there. It appears that you're trying to define your main() function inside your Guess function, which isn't allowed.
Last edited on
Im sorry! Please forgive my sloppiness. After looking over the program, I fixed the colon and the int main which took away the errors but the main problem now is it continuously asks the user to enter their guess. Im assuming I will need a loop?
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
#include<iostream>
#include <ctime>

using namespace std;

int AskRange();
int GetMagicNumber(int);
bool Guess(int);

int main()
{
    char choice = 'Y';
    int range = 0;
    
    cout << "Welcome to the Number Guessing Game" << endl;
    
    do
    {
        range = AskRange();
        Guess(range);
        
        cout << "Would you like to play again? Y/N " << endl;
        cin >> choice;
    } while (choice == 'Y' || choice == 'y');
    cout << "Goodbye! I had fun." << endl;
    
    return 0;
}

int AskRange()
{
    int n = 0;
    
    while
        (cout << "Enter the top value for the guessing range: " &&
         cin >> n && n <= 0
         )
    {
        cout << "The top value must be a positive number greater than 0" << endl;
    }
    
    return n;
}

int GetMagicNumber(int aRange)
{
    srand (time(NULL));

    int MagicNumber = rand() % aRange + 1;
    return MagicNumber;
}

bool Guess(int aRange)
{
    int GuessCounter = 0;
    int magicnumber = GetMagicNumber(aRange);
    int guess;
    
    cout << "I am thinking of a number between 0 and " << aRange << endl;
    //cout << "Magic number = " << magicnumber << endl;
    
    while (GuessCounter < 5)
    {
        cout << "Please enter your guess: " << endl;
        cin >> guess;
        
        if (guess > magicnumber)
        {
            cout << "Your guess is too high!" << endl;
        }
        else if (guess < magicnumber)
        {
            cout << "Your guess is too low!" << endl;
        }
        else
        {
            cout << "You correctly guessed the number I was thinking of! Congratulations!" << endl;
            return true;
        }
        GuessCounter++;
    }
    
    cout << "Game Over! Sorry, you ran out of guesses. The number I was thinking of was " << magicnumber << endl;
    return false;
}

Probably still has a few bugs ...
Topic archived. No new replies allowed.