Need help with this guessing game

Ok so this was the assignment: Write a program that picks a number between 1 and 100, and then lets the user guess what the number is. The program should tell the user if their guess is too high, too low, or just right. ok the problem i am having is when i guess the number it keep changing meaning if i guess seven it will say too high but if i should keep entering seven it would say you guess correct, that should not be happening. also i changed it from 100 to 10 so i can have a smaller number to work out the bug but i'm stump. I'm guessing it have something to do with my function or possible the way i call the function, i'm very new to programing so i only use the stuff i've learn as i'm sure there is probly better way to write the program. any help will be appreciated i've attach the code at the bottom.

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

using namespace std;

int numGenerator()
{
srand(time(0));

int random_number = 1 + (rand() % 10);

return random_number;

}//end of numGenerator function

int main()
{
int guess = 0;

numGenerator();

while(1)
{

cout << "Please guess the number i'm thinking of " <<endl;
cin >> guess;

if (guess > numGenerator())
{
cout << "Your guess is too high, Try again" << endl;

}//end of if statement

else if (guess < numGenerator())
{
cout << "Your guess is too low, Try again" << endl;

}//end of else if statement
else
{
cout << "Congratulation you guessed the right number" << endl;
return 0;

}//end of else statement

}//end of while loop

}//end of main
Just putting it in code tags to make it easier to read.

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

using namespace std;

int numGenerator()
{
    srand(time(0));
    int random_number = 1 + (rand() % 10);
    return random_number;
}   //end of numGenerator function

int main()
{
    int guess = 0;
    numGenerator();
    while(1)
    {
        cout << "Please guess the number i'm thinking of " <<endl;
        cin >> guess;

        if (guess > numGenerator())
        {
            cout << "Your guess is too high, Try again" << endl;
        }   //end of if statement

        else if (guess < numGenerator())
        {
            cout << "Your guess is too low, Try again" << endl;
        }   //end of else if statement
        else
        {
            cout << "Congratulation you guessed the right number" << endl;
            return 0;

        }   //end of else statement

    }   //end of while loop

}   //end of main n 
Last edited on
how do i put in the code tags... as you can see i'm new to posting so next time i will know how to do it
It is the button that has this inside: <>

When scrolled over, it says "source code."
The problem is you are calling the numGenerator function on every if statement, and every time it is called the value is changed.

You should only call that function once at the beginning of your program and set a value equal to it, so that it will never change. Then compare any user guess to that beginning number.

For example
1
2
3
4
5
6
7
8
9
10
11
int Target = numGenerator();

while(1)
{
    if(guess > Target)
    {

    }

    //etc
}
WOW thank you soooo much i was pondering over this for almost 2hrs before i give in and post it on boards. what a simple over sight thanks for you help
Topic archived. No new replies allowed.