Random number + guessing game trouble
I've been trying to work on my first program for the last 5 hours, this program should be very simple but even so I've had countless different problems with it )=
The program idea comes from a book called Teach yourself C, second edition, 1994 pg. 57. The problem is stated as so (I'm going to shorten it some and put it into my own words):
Create a program that generates a random number, allow it so a user has 10 chances to guess the said random number. The value of the generated number should be within 1 and 100. When a player makes a guess inform the user if his guess is to low or to high.
Unfortunately the book doesn't show to create a random number (at least up to this point it hasn't), because of this I had to look up a function that could (I'm going to try to use rand() )
So far I've encountered the following problems:
- For some reason, my compiler likes to compile things from the trash bin (things with the same name as my file i want to compile). This little problem took me quite some time to figure out, I would edit the program and i would get the same messages as before. So no one encounters this problem, when you delete a file say named guess.c and decide to make a new file (same name) close your editor and reopen it. I don't know why that works but it allowed me to actually try to compile the program I wanted to. Also emptying trash bin will work. If anyone has experienced this and knows why please tell me.
- The second problem I had was I didn't include the right .h files. This one took even longer to figure out and seems kinda a silly mistake.
- My next problem is the one I'm stuck on, I have edited and fixed and edited some more, but my program will not run (ok thats not true it does, but it won't do what I want it to). I need to generate a random number, which I have tried to do (as shown in the following code), I set it so the rand() function gets a random number, then my program is supposed to modify it so the new number is between 1 and 100. As my program runs I have found no matter what number I run I get the returned message I need to guess even higher, even when I guess higher than the limit I have set (or apparently though I set).
- My last problem is my program seems to go a little crazy when I try to put a character instead of an integer into the input )= I would think setting the variable to an int type would stop that problem.
So now here is my program, unfortunately its not what I really wanted I tried to add all the loop functions but I changed it thinking it was wrong before do to the first problem I had )= So now all it has is boring if loops. Also I have heard that goto function is very bad, well I'm not sure what I can use in its place so I had to use it (=
I'm shure theres some basic reason I can't get it to work... But I have missed it up to this point. Also if it helps I am running linux, and the compiler I use is gcc.
int guess, try, correct;
try = 10;
max = 100;
low = 1;
correct = rand();
/* this will attempt to give a random value to the variable "correct" */
if (correct < 0)
correct = -correct;
if (correct > max)
correct = correct/2;
if (correct < low)
correct = correct * 2;
if (correct < 1)
if (correct > 100)
/* This asks a user to guess a number as long as their trys have not been used up */
while (try != 0)
printf("Guess a number from 1-100, you have %d tries\n", try);
if (guess == correct)
printf("Correct, good job\n");
try = 0;
else if (guess > correct && try != 0)
printf("Sorry try agian, this time guess lower\n");
try = try - 1;
else if (guess < correct && try != 0)
printf("Wrong, Try again. This time guess higher\n");
try = try - 1;
if (try == 0 && guess != correct)
thanks to anyone that can help (or took the time to read any of that) (=