Thread: Random number guessing game

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    93

    Random number guessing game

    I'm on my way to make a Hang Man game so I thought I'd develop a random number guessing game first.

    Here's the code:

    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    
    
    int main() {
        int RandomNumber, Guess;
        time_t RandomSeeder;
        
        srand((unsigned) time(&RandomSeeder));
        
        RandomNumber = rand() % 100;
        
        printf("Enter a guess, please: ");
        
        scanf("%d", &Guess);
        
        while(Guess > RandomNumber || Guess < RandomNumber) {
            if(Guess > RandomNumber)
                printf("Too high!\n");
            
            if(Guess < RandomNumber)
                printf("Too low!\n");
            
            printf("Try again: ");
            
            scanf("%d", &Guess);
        }
        
        if(Guess == RandomNumber)
            printf("You win!\n");
        
        return 0;
    }
    I know I'm using scanf but this was a quick throw together.

    I've forgotten how to use other methods to make sure a user inputs a number instead of a character. I'll be looking into that in a bit...so yes, there's a feature where if you type a few characters, it'll go into an infinite loop.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    my two cents
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
     
     
    int main() {
        int RandomNumber, Guess;
        time_t RandomSeeder;
         
        srand((unsigned) time(&RandomSeeder));
         
       // RandomNumber = rand() % 100;
       RandomNumber = 50;
         
        printf("Enter a guess, please: ");
         
        scanf("%d", &Guess);
         
        while(Guess > RandomNumber || Guess < RandomNumber) 
        {
            if(Guess > RandomNumber)
                printf("Too high!\n");
            else // it's either one or the other yes?
            // if(Guess < RandomNumber)
                printf("Too low!\n");
             
            printf("Try again: ");
    
             // gets the value here so it does not go though the if statements frist
           // but the conditional check first. so if match it kicks it out of loop. 
          // so no real need to worry about if statements for everything. if else suffices. 
       // if you'd put that scanf on top before the if stameants then that'd be a different story. 
     
            scanf("%d", &Guess);
        }
         
        if(Guess == RandomNumber)
            printf("You win!\n");
         
        return 0;
    }
    just gave it a hard coded value to check it.
    Code:
    $ ./term2
    Enter a guess, please: 49
    Too low!
    Try again: 51
    Too high!
    Try again: 50
    You win!
    Last edited by userxbw; 12-05-2017 at 12:45 PM.

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    I've done a bit more work. This time I can accept characters (except for the looper variable, haven't done that yet). My goal is to just reject anything that's not a number. I'll get there.

    Here's the new code:

    Code:
    #include <stdio.h>#include <time.h>
    #include <stdlib.h>
    
    
    int Guess;
    char UserGuess[64];
    
    
    void NumberInput(void);
    
    
    int main() {
        int looper;
        looper = 1;
        
        while(looper) {
            int RandomNumber;
            time_t RandomSeeder;
            
            srand((unsigned) time(&RandomSeeder));
            
            RandomNumber = rand() % 100;
            
            printf("Number guess: ");
            
            NumberInput();
            
            while(Guess > RandomNumber || Guess < RandomNumber) {
                if(Guess > RandomNumber)
                    printf("Too high!\n");
                
                else
                    printf("Too low!\n");
                
                printf("Try again: ");
                
                NumberInput();
            }
            
            if(Guess == RandomNumber)
                printf("You win!\n");
            
            printf("Play another game?\n");
            printf("1. yes, 2. no\n");
            
            scanf("%d", &looper);
            
            if(looper == 2)
                looper = 0;
        }
        return 0;
    }
    
    
    void NumberInput() {
        while(1) {
            fgets(UserGuess, 64, stdin);
            
            Guess = atoi(UserGuess);
            
                if(Guess > -1 && Guess < 101)
                break;
            
            else
                printf("Enter a number between 0 to 100\n");
        }
        return;
    }

  4. #4
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Wait.. making adjustments to a function I wrote awhile back to see if I can get it to completely work in here. just cuz
    o tay look at this,
    Code:
    void NumberInput() {
        while(1) {
            fgets(UserGuess,sizeof UserGuess, stdin);
     
           // Guess = atoi(UserGuess);
                if ( ( Guess = int_or_ch(UserGuess) ) == -1)
                {
                    printf("enter a number silly %d\n"
                    "Try again ", Guess);
                     
                }
                else if(Guess > -1 && Guess < 101)
                    break;
                else
                    printf("Enter a number between 0 to 100\n");
        }
        return;
    }
    output
    Code:
    $ ./term2
    Number guess: r
    enter a number silly -1
    Try again r
    enter a number silly -1
    Try again r
    enter a number silly -1
    Try again r
    enter a number silly -1
    Try again 4
    Too low!
    Try again: e
    enter a number silly -1
    Try again
    no I didn't post the function, did want to deprive you of your fun. But if you want some hints. post for some.
    Last edited by userxbw; 12-05-2017 at 03:32 PM.

  5. #5
    Registered User
    Join Date
    Sep 2017
    Posts
    93
    Are you able to tell me some of the functions used in your function without giving away how it works?

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    is uses isdigit and a char if more than 1 , like 20 = 2 then char is looked at as an array within isdigit. to get it back to 1 to see char as an int. it is a stinky tricky thing to try and get to work. I still have issues with it. I know me and whiteflags fiddled with this issue about how to get input to see if it is a digit or not a while back, It might be in C++ side. if not here, ok found it, YOU DO NOT HAVE TO CLICK ON IT, RESIST THE TEMPTATION Mhumhahahahahaha

    help with a function that determins digit or not digit needed, apply within.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random Number Guessing Game
    By Borko Kovacev in forum C Programming
    Replies: 2
    Last Post: 02-13-2013, 03:14 AM
  2. Random Number Guessing Game
    By iMattbckr in forum C++ Programming
    Replies: 14
    Last Post: 07-26-2012, 04:00 AM
  3. random number guessing game
    By kazoo in forum C Programming
    Replies: 7
    Last Post: 05-30-2010, 11:31 AM
  4. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 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

Tags for this Thread