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>
usingnamespace 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
elseif (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
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.
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