Archived

This topic is now archived and is closed to further replies.

dmf_king

Guess The Number!

Recommended Posts

Hi just made a cool little game that should compile on anything!. Though it uses a C library - and some gotos... Any adivce would be great! #include <iostream> #include <cstdlib> using namespace std; int main() { int guess; int answer; answer=rand(); cout << endl << "[-----------------------------------]"; cout << endl << "[-----------Guessing Game-----------]"; cout << endl << "[-----------------------------------]"; cout << endl << endl << "Welcome to a very simple guessing game. To play, guess an integer that is equal to the answer(Which is a random number between 0 and 32,767) Guess:> "; start: cin >> guess; if ( guess < answer ) { cout << "Your guess was less than the answer! Guess again:> "; goto start; } if ( guess > answer ) { cout << "Your guess was higher than the answer! Guess again:> "; goto start; } cout << "YOU WIN!" << endl; return 0; }

Share this post


Link to post
Share on other sites
Argh!!!! Get rid of the goto''s!!!!! They have absolutely no place in a program that simple and simply complicate things.

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int guess;
int answer;
answer=rand();
cout << endl << "[-----------------------------------]";
cout << endl << "[-----------Guessing Game-----------]";
cout << endl << "[-----------------------------------]";
cout << endl << endl << "Welcome to a very simple guessing game. To play, guess an integer that is equal to the answer(Which is a random number between 0 and 32,767) Guess:> ";
while (true)
{
cin >> guess;
if ( guess < answer )
{
cout << "Your guess was less than the answer! Guess again:> ";
}
else if ( guess > answer )
{
cout << "Your guess was higher than the answer! Guess again:> ";
}
else
{
cout << "YOU WIN!" << endl;
return 0;
}
}
}


Enigma

Share this post


Link to post
Share on other sites
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int guess;
int answer;
srand(time(NULL));
answer = rand();

do{
printf("Guess the number:");
scanf("%i", &guess);
if(guess < answer)
printf("%i is too low\n", guess);
else if(guess > answer)
printf("%i is too high\n", guess);
}while(guess != answer);
printf("%i is correct!\n", guess);
}


[edited by - extrarius on January 23, 2004 11:43:51 AM]

Share this post


Link to post
Share on other sites
1) I guess it might be reasonable to assume that rand() generates numbers between 0 and 32767, but it would be more appropriate to assume that the numbers will be between 0 and RAND_MAX, since that is exactly what the function does. And technically, RAND_MAX might change on various compilers. If you want it to still be between 0 and 32767, you could change your rand() line to:

answer = rand() % 32768;   
2) I don't know what system you compile, but on most, you need to provide the random number generator with a random seed. Other wise, it will generate the same number every time. I'm using VC6, and the number is 41 every time for me. The typical way to fix this is to #include "" and then call the following once at the beginning of your program before ever calling rand():

srand(time(NULL));   
3) I agree with Enigma. Get rid of gotos. They are in most cases bad. There are arguably a few places where they're useful. But not many.

4) Lastly, congratulations! I can't remember what my early programs were like, but I wish I could go back to the days when small programs excited me. Any more, the interesting projects are the ones that take weeks to complete, at the minimum. Maybe the small ones still can. I should write something simple tonight.

[Edit - Grr... Can't make a :D smiley.]

[edited by - Agony on January 23, 2004 11:46:20 AM]

[edited by - Agony on January 23, 2004 11:48:28 AM]

Share this post


Link to post
Share on other sites