# Thread: Guess the number game

1. ## Guess the number game

Hey again.

Continuing with my assignment and seem to have come a long way in understanding C. However part of my assignment was to create a guess the number game which randomly generates a number and you simply have to guess it, whist it either tells you if your guess was too high or too low. Now here is my code:

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
int secret_number;
int guess=0;
int play=1;
const int LOW=-100;
const int HIGH=100;
time_t seconds;
time(&seconds);
srand((unsigned int) seconds);
while (play==1)
{
printf("I have stored a random number between -100 and 100, try and see if you can guess the number:\n");
secret_number=rand()%(HIGH-LOW+1)+LOW;
while(guess!=secret_number)
{
scanf("%d",&guess);
if(guess==secret_number)
{
printf("\nYour Guess was correct! The number is %d\n", secret_number);
}
else if(guess>secret_number)
{
printf("\nGuess was too high, try again\n");
}
else
{
printf("Guess was too low, try again\n");
}
}
printf ("\nWould you like to play again? Yes=1 or No=2\n");
scanf ("%d", &play);
}
system("pause");
return;
}```
It works fine but I got a bit of help on generating random numbers from the Internet. My main trouble is that I don't think I can actually explain everything that's going on here.
If you were to explain each step in this program, what would you say?

Thank you.

-Ninestar

2. You could always click the FAQ link, so you can understand how rand and friends work.
Originally Posted by Ninestar
If you were to explain each step in this program, what would you say?

Thank you.

-Ninestar
I've got a better idea. You go through your program, line by line, comment it, telling US what it does. When you get to something you don't understand, let us know. Seriously, what good does it do for me to tell you how it works? It's better for you to tell me, and I'll tell you if you're right, and help you where you're stuck.

Quzah.

3. I'm sure it works 99.5% of the time. But what if the secret number is 0? Your while() loop will never even run because you initialized guess to 0. You're better off with a do-while loop. Or right after getting the random number add this line:
Code:
`guess = !secret_number;`
just to make sure they don't match before the game even begins.

4. Good point itsme86.

5. Code:
```int main(int argc, char *argv[]) {
/* ... */
return;
}```
Perhaps you meant
Code:
`return 0;`

6. ok cheers guys, I did have the '0' after return, dunno how it got deleted.

I've done some data sort alogorithms and found them easy, but need to re-write this guess the number crap so I can explain it to my tutor.

I usually put lines of explanation under each set of code, but as I don't understand what this code is doing I can't do that!

7. Originally Posted by itsme86
I'm sure it works 99.5% of the time. But what if the secret number is 0? Your while() loop will never even run because you initialized guess to 0. You're better off with a do-while loop. Or right after getting the random number add this line:
Code:
`guess = !secret_number;`
just to make sure they don't match before the game even begins.
I have that line in my While loop! - look!
My god I hate C code, how the hell do you guys enjoy this??

8. I have that line in my While loop! - look!
No, you have:
Code:
`      while(guess!=secret_number)`
See the difference? Yours is testing to see if they match. But guess = !secret_number; sets guess to either 0 or 1 depending on what secret_number is. If secret_number is true (anything but 0) then guess is set to 0, but if secret_number is 0 then guess is set to 1. See? They'll never be the same before your while() loop starts that way.

My god I hate C code, how the hell do you guys enjoy this??
There are few things in life that are totally controllable and logical. Programming happens to be one of them (I know it's not totally controllable, you pedantics, but it's moreso than most things). It's nice to know that if your program doesn't work it's because you screwed up. Figuring out newer, better ways of doing things is challenging and fun. I think programmers (who program as a hobby anyway) are a lot like inventors. Only, unlike inventing something tangible, it doesn't cost anything to make a program

9. Ok, gotcha. I added the lin of code in to my program and it still works, I'm guess this is a good thing?

I inserted it below my while loop

10. I managed to make your program hit the fan by typing in a decimal!

11. Don't type in the decimal then!

It works as long as you don't blow any air on it, so that's good enough for me!

12. The real question is: is it good enough for your teacher?

Quzah.

13. This is exactly my problem with many programming class assignments - seems too often just accomplishing the stated goals is good enough, but for real-life applications MUCH time is devoted to exception handling of one form or another. User input is especially troublesome!

Hmmmm...maybe try:
Code:
`printf("I have stored a random number between -100 and 100, try and see if you can guess the number (NO UNEXPECTED INPUT ALLOWED!):\n");`
Just kidding, of course...