I am trying to write a Guess number which generate a number for player to guess until the player get it correct. i had checked many time about the code, it keep show me expected unqualified- id. Please tell me where i made the mistake. Thank you.
//Guessing game
#include<iostream>
usingnamespace std;
int randomNumber();
//Generate a random number within a given range of value(100)
//void guess(int rNumber);
//Processes one guess from the player and provide hints.
voidtry(int rNumber);
//Allow for plyayer to guesses until the player guesses the number.
int main()
{
int i;
i = randomNumber();
try(i);
cout << "End of program\n";
return 0;
}
int randomNumber()
{
int r;
r = rand()%100;
return r;
}
/*void guess(int rNumber)
{
int input, rNumber;
cout << "Enter a number within range of 1 - 100" << endl;
cin >> input;
if(input == rNumber)
{
cout << "Congratulation! You are correct :)" << endl;
}
else
{
if(input > rNumber)
{
cout << "Too high :(\n";
}
else
{
cout << "Too low >.<\n";
}
}
}*/
voidtry(int rNumber)
{
int input;
int rNumber;
do
{
cout << "Enter a input\n";
cin >> input;
if(input == rNumber)
{
cout << "Congratulation! You are correct :)" << endl;
}
else
{
if(input > rNumber)
{
cout << "Too high :(\n";
}
else
{
cout << "Too low >.<\n";
}
}
}while(input != rNumber);
}
Error is solved, Thank You. but that is another problem, why the random number generated by the function always is the same? What should i change in order to make the number generated different every time?
why the random number generated by the function always is the same?
Add srand(time(0)); before line 20. This will seed the random number generator with the current time, which will cause different numbers each time you play it (unless you play multiple times in one second).
//Generate a random number within the given range
#include<iostream>
usingnamespace std;
int main()
{
int r;
char ans;
do
{
r = rand()%100;
cout << r << endl;
cout << "Again(Y/N)";
cin >> ans;
} while ((ans == 'y')|| (ans == 'Y'));
cout << "End of program";
return 0;
}
why this code can generate different number to me while it is still in loop but the code for guessing game can't? i will be so much appreciate for your help.
It is because if you do not supply a seed, the RNG is seeded with the value 1. This means it will generate the same sequence every time you run the program; which for me happened to always start with 41 (mod 100). If you run you guessing game multiple times, you will get the start of the sequence (41) each time you run. Your second example also started with that same number for me.