Guess the number game

This is a discussion on Guess the number game within the C Programming forums, part of the General Programming Boards category; Hey again. Continuing with my assignment and seem to have come a long way in understanding C. However part of ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    16

    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. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always click the FAQ link, so you can understand how rand and friends work.
    Quote 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.
    Hope is the first step on the road to disappointment.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    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.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Dec 2005
    Location
    USA
    Posts
    29
    Good point itsme86.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    int main(int argc, char *argv[]) {
        /* ... */
        return;
    }
    Perhaps you meant
    Code:
    return 0;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    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. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    Quote 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. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    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
    Last edited by itsme86; 12-07-2005 at 09:40 AM.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    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. #10
    Registered User
    Join Date
    Jul 2005
    Posts
    10
    I managed to make your program hit the fan by typing in a decimal!

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    16
    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. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The real question is: is it good enough for your teacher?


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User KidA's Avatar
    Join Date
    Nov 2005
    Location
    Ohio, USA
    Posts
    26
    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...
    "So I was sitting in my cubicle today, and I realized, ever since I started working, every single day of my life has been worse than the day before it. So that means that every single day that you see me, that's on the worst day of my life" - Peter Gibbons

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. craps game & dice game..
    By cgurl05 in forum C Programming
    Replies: 3
    Last Post: 03-25-2006, 06:58 PM
  5. Someone help me with this game??
    By stehigs321 in forum Game Programming
    Replies: 15
    Last Post: 10-30-2003, 08:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21