Thread: Random number + guessing game trouble

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    45

    Question 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 (=

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    
    int guess, try, correct;
    int max;
    int low;
    
    try = 10;
    max = 100;
    low = 1;
    correct = rand();
    
    /* this will attempt to give a random value to the variable "correct" */
    
     
    repeate:
    	
    	if (correct < 0) 
    	{
    	correct = -correct;
    	}
    
    	if (correct > max) 
    	{
    	correct = correct/2;
    	}
    	
    	if (correct < low) 
    	{
    	correct = correct * 2;
    	}
    	
    	if (correct < 1)
    	{
    goto repeate;
    	}
    
    	if (correct > 100) 
    	{
    goto repeate;
    	}
    
    /* 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);
    	scanf("%d", guess);
    
    	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)
    	
    			printf("You lose\n");
    	
    
    
    }
    return 0;
    }
    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.

    thanks to anyone that can help (or took the time to read any of that) (=

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Wow, well goto's and labels aren't really recommended anymore. Also try and use indenting...

    I would try a re-write, plan from the start

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    45
    Sorry about the layout of the program. I will do a rewrite but first do you see anything that would result in the program not working? At this time I'm using gotos because I lack the knowledge of using anything else )=
    Also may I ask why people have stopped using the goto function?

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because it's confusing & hard to read, loops and ifs are better.

    If you want a number between 1 and 100, use:

    Code:
    int num = rand() &#37; 100 + 1;

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    you have a broken logic

    rand never returns negative numbers
    if (correct < low) - in your case it means 0
    0*2 and 0/2 is still 0

    to get a random value in the [low,high] regeon you can use for the start the simple approach
    correct = low + rand()&#37;(high-low+1);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Registered User
    Join Date
    May 2007
    Posts
    45
    Quote Originally Posted by vart View Post
    you have a broken logic

    rand never returns negative numbers
    if (correct < low) - in your case it means 0
    0*2 and 0/2 is still 0

    to get a random value in the [low,high] regeon you can use for the start the simple approach
    correct = low + rand()%(high-low+1);
    Okay simpilar is better (= But I guess I am misunderstanding how
    Code:
     if (correct < low)
    would be the resulting of the error, if rand will never give me a negative number than wouldn't the program just pass over:

    Code:
     if (correct < 0) 
    	{
    	correct = -correct;
    	}
    And procede to the following if statements?

    I see now that if rand happened to give back 0 then in that special case the program should fail, but wouldn't at all other times it run fine?

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    in that special case the program should fail, but wouldn't at all other times it run fine?
    I said your logic is broken one place... I haven't checked if it is OK all other places...

    Use debugger to see what happening...
    Or add temporary printfs to trace your program
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  8. #8
    Registered User
    Join Date
    May 2007
    Posts
    45
    Quote Originally Posted by vart View Post
    I said your logic is broken one place... I haven't checked if it is OK all other places...

    Use debugger to see what happening...
    Or add temporary printfs to trace your program
    Ok thats a good idea, thanks for your response.

  9. #9
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Look at the following code:

    Code:
    	if (correct < low) 
    	{
    	correct = correct * 2;
    	}
    As you can see, you defined low as being 1, so any number inferior to low is definitely smaller than 1. This in turn, is a decimal number anywhere between 0 and 1. Let's take 0.5 as example:

    0.5 * 2 = 0.25

    So your multiplication doesn't help one bit but results in an infinite loop. Since you're using integers anyway, anything below 1 will be truncated to 0. So you're ending up in an endless loop, multiplying 0 all the time by 2.

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    0.5 * 2 = 1

  11. #11
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Quote Originally Posted by KONI View Post
    0.5 * 2 = 0.25
    Shouldn't that be "0.5 * 2 = 1" instead? You're dividing in this case. Integer division can cause unforseen problems that you wouldn't normally expect. 9/5 isn't 1.8 or 2 in the case of integer division, it's 1. The trash after the decimal is ignored/discarded.

    I don't understand the repeate and goto things (is that even C?) so I can't help you there.

  12. #12
    Registered User
    Join Date
    May 2007
    Posts
    45
    Quote Originally Posted by ulillillia View Post
    Shouldn't that be "0.5 * 2 = 1" instead? You're dividing in this case. Integer division can cause unforseen problems that you wouldn't normally expect. 9/5 isn't 1.8 or 2 in the case of integer division, it's 1. The trash after the decimal is ignored/discarded.

    I don't understand the repeate and goto things (is that even C?) so I can't help you there.
    I suppose multiplying anything less than 1 (zero in this case) wouldn't make sense (=
    I guess I was thinking that the integers where getting rounded up, but that obviously isn't the case here.

    As for the goto function, it is part of the C language (according to my book... My old book). from what I have been told it is now not used very often. The only reason I use it is because I have found it easier to deal with then the normal loops and I don't have a better way of doing things at this time (=.



    By the way I have got my program to tell me if I'm higher or lower when I guess a number, but for some reason rand() has only been giving me the number 53 every time )=

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    By the way I have got my program to tell me if I'm higher or lower when I guess a number, but for some reason rand() has only been giving me the number 53 every time )=
    read srand description
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    45
    Thank you this looks like exactly what I needed (=

  15. #15
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Code:
    while (value < another)
    {
    	// do stuff
    	value++;
    }
    That's a basic loop. The loop continues until the condition is false (or a "break;" is added). There's also for and do-while. I mostly prefer "while", but there are cases where "for" comes in handy. Leaving out the "value++;" item will cause an infinite loop which makes closing the program a pain (not in Windows 98 though, but in XP Pro it is).

    For fractional values, use floats or doubles (but be careful when using high precision - 7.000000001 as a float is actually 7.0, but as a double, it's different). Floats have 7 significant figures and doubles have 15. Using == for conditions will almost always make them false - use ranges and something "close enough" instead.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. Generating a random number?
    By Konspiracy in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 12:33 AM
  3. Testing Random Number Generator
    By Roaring_Tiger in forum C Programming
    Replies: 7
    Last Post: 08-12-2005, 12:48 AM
  4. Random number generator
    By Caze in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2002, 08:10 AM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM