-
guessing game
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
srand();
int main ()
{
int guess;
guess=1+rand()%100;
cout << "Guess what number I am thinking of... : ";
cout << guess;
cin >> guess;
while (guess != rand())
{
while (guess < rand())
{
cout << "The number is higher...Guess again : ";
cin >> guess;
}
while (guess > rand())
{
cout << "The number is lower...Guess again : ";
cin >> guess;
}
while (guess == rand())
{
cout << "You are correct";
break;
}
}
return 0;
}
im trying to make a guessing game that the program generates a random number between 1 and 100 is there a way to do that?
-
You want two variables
Code:
int my_guess = 1+rand()%100;
int your_guess = -1;
while ( my_guess != your_guess ) {
cout << "What's your guess";
cin >> your_guess;
if ( my_guess == your_guess ) {
// woo hoo!!
}
}
Got the idea?
-
You should initially seed the program with this line. Please note that it should only be seeded once or it will return the same number again and again.
srand( (unsigned)time( NULL ) );
int main ()
{
int guess;
guess=1+rand()%100;
cout << "Guess what number I am thinking of... : ";
cout << guess;
cin >> guess;
while (guess != rand())
The code above should be editted to follow the format of mine below.
int main()
{
int guess;
const int temp = 1 + (rand()%100);
cout<<"Guess what number I am thinking of...:\n";
cin>>guess;
while(guess != temp)//very important change read below why
{
//the rest of your code
}
Everytime rand() is called it returns a different random number. So instead of comparing your guess to the initial random number it is compared to a new one each time. Your guessing game could go on for ever this way. The way to avoid this pitfall is to store the number initially created into a const int variable and compare from that.
-
it tried it
it ask that guess be initialized
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctime>
srand();
int main ()
{
int guess = 0;
const int temp = 1 + (rand()%100);
cout << "Guess what number I am thinking of... : ";
cout << guess;
cin >> guess;
while (guess != temp)
{
while (guess < temp)
{
cout << "The number is higher...Guess again : ";
cin >> guess;
}
while (guess > temp)
{
cout << "The number is lower...Guess again : ";
cin >> guess;
}
while (guess == temp)
{
cout << "You are correct";
break;
}
}
return 0;
}
also the answer is constantly 42
i tried
srand( (unsigned)time(null) );
but i get errors
-
these are the errors
error C2065: 'null' : undeclared identifier
error C2501: 'srand' : missing storage-class or type specifiers
error C2373: 'srand' : redefinition; different type modifiers
-
Declare srand at the beginning of the main function
main()
{
srand( (unsigned)time( NULL ) );
//then the rest of your code
try using #include <time.h>
-
Here it is for you. New and improved and full functional. A few suggestions I might include are:
1) The program could use a infinite for loop to repeat until a break is called.
2) Could incorperate a mechanism to catch values higher or lower then the minimum and maximum values allowed.
3) Could incorperate a scoring system. Something along the lines of getting a higher score for having the fewest guesses.
This is how I got started into programming. I would make a shell application and slowly add new features. Currently I am working on a RPG game and this is how I evolve it into what I want it to be. I add a feature, test it for any possible errors, fix errors if found, test it again. If it looks good I save the class into a folder I use for backups and then add it into my source code. Doing this gives me a lot of control over what kinda of errors can appear and keeps the number of runningtime errors at a minimal.
Hope this helps, e-mail me if you have any questions.
Jeramy
#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main ()
{
srand( (unsigned)time( NULL ) );
int guess = 0;
const int temp = 1 + (rand()%100);
cout << "Guess what number I am thinking of... : ";
cin >> guess;
while (guess != temp)
{
while (guess < temp)
{
cout << "The number is higher...Guess again : ";
cin >> guess;
}
while (guess > temp)
{
cout << "The number is lower...Guess again : ";
cin >> guess;
}
while (guess == temp)
{
cout << "You are correct";
break;
}
}
return 0;
}
-
out of the topic
>>
>> also the answer is constantly 42
>>
this is a very good answer :-))))
-
yes
yes 42 is a good answer ñ_ñ
-
aaarggh 42 the meaning of life!
-
Coincidence I think not.:D
-
This should work.
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
main()
{
int low = 0;
int high = 101;
int guess = 50;
int trys = 0;
int previous = 0;
char answer = NULL;
srand(time(NULL));
cout << "Think of a number between 1-100 and I will guess it!\n" << endl;
while (answer != 'c' && answer != 'C')
{
cout << "My guess is " << guess << "." << endl;
cout << "Is your number [l]ower, [h]igher or [c]orrect?: ";
cin >> answer;
trys++; if (trys == 14) { cout << "\nMoron!\n"; exit(1); }
if (answer == 'l' || answer == 'L')
{
previous = guess;
while (previous == guess)
{
high = guess;
guess = guess - (rand() % (guess - low));
}
}
else if (answer == 'h' || answer == 'H')
{
previous = guess;
while (previous == guess)
{
low = guess;
guess = guess + (rand() % (high - guess));
}
}
}
cout << "\nHurray!\n";
cout << "Even though it took " << trys << " try(s) to get it." << endl;
return 0;
}