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
    Posts
    3

    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
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Post your code here (in code tags, please)... There could be several solutions.

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

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 05:06 PM
  2. Replies: 15
    Last Post: 10-20-2009, 09:39 AM
  3. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  4. Random Numbers (Advanced)
    By misplaced in forum C++ Programming
    Replies: 11
    Last Post: 03-25-2005, 02:57 AM
  5. random numbers
    By lil_plukyduck in forum C++ Programming
    Replies: 5
    Last Post: 01-14-2003, 09: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