Thread: Guessing Game - Problem

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    7

    Guessing Game - Problem

    I can't see why this wouldn't work. Either I'm completely missing something or this book is missing something.

    Code:
    #include <stdio.h>
    #include <windows.h>
    
    main()
    {
          
          int iRandom = 0;
          int iResponse = 0;
          srand(time());
          
          iRandom = (rand() % 6) + 1;
          
          printf("\nPlease enter a number netween 1 and 6: ");
          scanf("%d", &iResponse);
          
          if (iResponse == iRandom) 
             printf("\nCongratulations, you guessed correct!\n");
          else {
             printf("\nUnlucky, good luck next time!\n");
             printf("\nThe correct number was %d!\n", iRandom);
          }
             
          Sleep(5000);
          
    }
    Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    rand and srand appear in stdlib.h. time appears in time.h. I'm not quite sure from your question what exactly you're confused about (did the book do something in a different way? did it not compile? etc?) so I don't know what else to say.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    It didn't say to include stdlib.h and time.h for one, even with them included it still does not compile.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Slynet View Post
    It didn't say to include stdlib.h and time.h for one, even with them included it still does not compile.
    I'm completely confident that some day - maybe next week, maybe tomorrow, maybe next month - you'll paste up the error codes you received.

    I'm sure you'll manage that, some day.

  5. #5
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    Ha, I'll take your sarcasm as humor for now.

    Sorry, it's quite late here, bit brain dead. :P

    So, when I compile it comes up with the errors:

    Code:
     C:\Documents and Settings\User\Desktop\1.c In function `main': 
    11 C:\Documents and Settings\User\Desktop\1.c too few arguments to function `time'
    Error on line 11, srand(time());

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I double checked with my compiler, and it says (once I included time.h) that time does not take zero arguments (which is true -- it takes one argument, which is conventionally NULL in this circumstance).

  7. #7
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    Ok, so I've never used this function before and this is the example in the book. But if if use:

    Code:
    srand(time(0))
    It works fine. Can someone explain to me what exactly it's doing, please.

    Also now it's compiling without stdlib.h and time.h? So I need them, I don't need them?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    stdlib.h and time.h are headers -- so there's no way for the compiler to tell that you are missing an argument if the header is not included. If you're using some really fancy IDE, it may be including things for you.

    So how have you gotten this far without looking time() up in the index of your textbook, or typing "man time" online and getting this page?

  9. #9
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    In the post with the errors, that included the headers stdlib.h and time.h, which was advised.

    In the post after I stated "it is now compiling", as opposed to before. In the successful compilation post all I done was to change srand(time()) to srand(time(0)), which compiled, then took away stdlib.h and time.h, and it still compiled.

    Hope that makes more sense.

    And yes, I'm using Dev-C++ with Mingw/GCC 3.4.2.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Some compilers (like Turbo C/C++ of old), are smart enough to include some parts of a header, if you need it, and don't include the right header file for it.

    It will not do that for everything you need, so get in the habit of using the right headers. Other compilers will just give you some error, and not go looking for anything to help your code deal with a missing header.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Slynet View Post
    In the post with the errors, that included the headers stdlib.h and time.h, which was advised.

    In the post after I stated "it is now compiling", as opposed to before. In the successful compilation post all I done was to change srand(time()) to srand(time(0)), which compiled, then took away stdlib.h and time.h, and it still compiled.

    Hope that makes more sense.

    And yes, I'm using Dev-C++ with Mingw/GCC 3.4.2.
    Right. So the compiler cannot determine there's a problem unless you include the headers. If you didn't include the headers, then it won't know what to look for, so it will just take whatever you've got typed. If it doesn't work, you won't know until you crash your computer when you try to run the program (or, nowadays, "would you like to submit this error report to Microsoft?" when time takes some random number off the stack (since you didn't pass it one) and tries to use it as its argument).

  12. #12
    Registered User
    Join Date
    Jul 2008
    Posts
    7
    Ok, I get it, thanks. It's really annoying that this book doesn't include them two headers, if you're going to teach someone, teach them properly.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Slynet View Post
    Ok, I get it, thanks. It's really annoying that this book doesn't include them two headers, if you're going to teach someone, teach them properly.
    You'd think so, wouldn't you... Unfortunately, we do see it not so rarely that books either take short-cuts, or do not use up-to-date information...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PC Game project requires c++ programmers
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-22-2006, 12:23 AM
  2. Linking problem
    By Agent89 in forum C++ Programming
    Replies: 4
    Last Post: 03-27-2005, 03:03 PM
  3. My Memory Game
    By jazy921 in forum C Programming
    Replies: 0
    Last Post: 05-05-2003, 05:13 PM
  4. My number guessing game
    By The Gweech in forum C++ Programming
    Replies: 7
    Last Post: 06-22-2002, 09:03 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM