#### Archived

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

# Guess The Number!

This topic is 5385 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

##### Share on other sites
that''s simply amazing

##### Share on other sites
Hehe this was the first game I ever made. Great job with your personal achievement!

##### 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;
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 on other sites
Remove the gotos, and put in a while loop.

Btw. My first game was a Text Adventure in Basic ;-)

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

int main()
{
int guess;
srand(time(NULL));

do{
printf("Guess the number:");
scanf("%i", &guess);
printf("%i is too low\n", guess);
printf("%i is too high\n", guess);
printf("%i is correct!\n", guess);
}

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

##### 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:

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]

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 9
• 13
• 94
• 11
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009631
• ### Who's Online (See full list)

There are no registered users currently online

×