Random Numbers Game

This is a discussion on Random Numbers Game within the C Programming forums, part of the General Programming Boards category; I started a random numbers game and got it all done. Then I realized that if you entered a letter ...

  1. #1
    Registered User
    Join Date
    Nov 2010

    Random Numbers Game

    I started a random numbers game and got it all done. Then I realized that if you entered a letter instead of a number it crashes the game.

    What is the code to help stop that from happening?

  2. #2
    Join Date
    Aug 2010
    Ontario Canada
    Post your code here (in code tags, please)... There could be several solutions.

  3. #3
    Registered User
    Join Date
    Nov 2010
    I think I posted this right lol
    void main()
    int num,guess=-1,tries=0,pass=0;
    time_t t;
    printf(“Enter the guess num b/w 0 & 100 (you have %d tries left out)\n”,(8-tries)); scanf(“%d”,&guess);
    printf(“Hurray you guessed it correctly!!!\n”);
    else if(num< guess)
    printf(“Your guess is too high\n”);
    printf(“Your guess is too low\n”);
    printf(“Sorry you lost! The correct number is %d\n”,num);

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    Let's start with the basics:

    1) void main should be int main(void) and should return 0 at the end of the function.
    2) your indentation is horrible
    3) you can read the input as a char and if it is not in the ASCII range you want to just tell the user his input is wrong.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Has it happens, I've got just the two links you need already open.
    SourceForge.net: Indentation - cpwiki
    SourceForge.net: Void main - cpwiki
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [freefps]Tactical Assault coders needed
    By Sickmind in forum Projects and Job Recruitment
    Replies: 4
    Last Post: 05-07-2010, 06:06 PM
  2. Replies: 15
    Last Post: 10-20-2009, 10:39 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 04:33 AM
  4. Random Numbers (Advanced)
    By misplaced in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2005, 03:57 AM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 10:14 PM

Tags for this Thread

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