#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
    int i, n, diff;
    int x=rand() % 100;
    int bMoveHigher = 0;
    int bGuessedCorrectly = 0;

    printf("Welcome to Guess a Number Game\n");

    for(i=1; i<=6; i++)
    {
        printf("ATTEMPT %d: Enter your number: ", i);
        scanf("%d", &n);
        if(n==x)
        {
            printf("Congrats! You have guessed the number correctly\n");
            bGuessedCorrectly=1;
            break;
        }
        diff=(int)(fabs(x-n));
        if(x>n)
            bMoveHigher=1;
        if(diff>=50)
        {
            if(bMoveHigher==0)
                printf("Your guess is Very High\n");
            else
                printf("Your guess is Very Low\n");
        }
        else if(diff>=30)
        {
            if(bMoveHigher==0)
                printf("Your guess is High\n");
            else
                printf("Your guess is Low\n");
        }
        else if(diff>=15)
        {
            if(bMoveHigher==0)
                printf("Your guess is Moderately High\n");
            else
                printf("Your guess is Moderately Low\n");
        }
        else
        {
            if(bMoveHigher==0)
                printf("Your guess is Somewhat High\n");
            else
                printf("Your guess is Somewhat Low\n");
        }
    }
    if(bGuessedCorrectly==0)
    {
        printf("Unfortunately you did not guess it correctly. The correct number is: %d\n", x);
    }
    getch();
}

call srand() at the very beginnning of the program, and before the first time rand() is used. Usually srand() is called with the return value from time() so that srand() gets a different number every time it is called. That will prevent rand() from returning the same set of numbers each time you run the program.

srand((unsigned int)time(0));

Ok ... you got it working ... but ...

What about ... if the user enters non-numeric char's when an int is expected?

Try your program and see what happens?

Try your program (as it is) as a (void) function call ... and loop in 'main' as long as the user says 'more' ... and see what happens ?

The problems you will encounter, are both handled if ...

  1. you know how to loop until 'valid input' is obtained

  2. you know how to keep the stdin buffer 'flushed' after-input-when-ever-char's-may-be-left there still ...

See the below typical 'student type' way of dealing with the above two issues (and more) ...

/* guessNum.c */

#include <stdio.h>
/* #include <conio.h> */  /* don't use to keep code portable ... */
#include <stdlib.h> /* re. rand ... */
#include <ctype.h> /* re. tolower ...*/
#include <time.h> /* re. srand ... */

#define MY_BUF_SIZE 128 /* re. buf used to form prompt with sprintf */

const char* STATUS[] = { "Fantastic", "Amazing", "Excellent", "Wonderful", 
                         "Great", "Pretty Good", "Not too bad", "That's ok",
                         "You finally got it", 
                         "Maybe better ... next time."  } ;


/* a simple student way to handle numeric input ...
   so program won't crash on bad input */
int takeInInt( const char* msg, int myMin, int myMax )
{
    int good = 0, val = 0;
    while( !good )
    {
        printf( msg ); fflush( stdout );

        if( scanf( "%d", &val ) == 1 && getchar() == '\n' )
            good = 1;
        else
        {
            printf( "\nInteger input only here please ...\n" );
            while( getchar() != '\n' ) ; /* flush stdin ... */
        }

        if( good && ( val < myMin || val > myMax ) )
        {
            good = 0;
            printf( "\nNote! Valid input range is %d..%d\n", myMin, myMax );
        }
    }
    return val;
}

/* a handy utility for many C student coding problems ... */
char takeInChar( const char* msg )
{
    char chr;
    printf( msg ); fflush( stdout );
    chr = getchar();
    if( chr != '\n' )
        while( getchar() != '\n' ) ; /* flush stdin ... */
    return chr;
}

int more() /* defaults to 'true'/'yes'/'1' ... unless 'n' or 'N' entered */
{
    if( tolower( takeInChar( "\nMore (y/n) ? " )) == 'n' )
        return 0;
    /* else ... if reach here ... */
    return 1;
}

void playGame()
{
    char buf[MY_BUF_SIZE];

    int i, guess, diff, maxNumTrys = 0;

    int x = rand() % 100;

    maxNumTrys = takeInInt( "\nHow many chances, (1..10), "
                            "would you like to have\n"
                            "to guess the 'mystery number' ?  ", 
                             1, 10 );

    for( i = 0; i < maxNumTrys; /* i is updated below ... */ ) 
    {
        /* form prompt for this 'try' ... */
        sprintf( buf, "\n< ATTEMPT %d > Enter your number:  ", ++i ) ;

        guess = takeInInt( buf, 0, 99 ); /* prompt is in C string 'buf' */

        if( guess == x )
        {
            printf( "Congrats! You guessed the "
                    "number correctly after %d trys.\n", i );

            printf( "\n%s ...\n", STATUS[i-1] ) ;
            break;
        }


        diff = abs( guess - x );

        if( diff > 32 )
        {
            if( guess - x > 0 )
                printf( "Your guess is Very Very High\n" );
            else
                printf( "Your guess is Very Very Low\n" );
        }
        else if( diff > 16 )
        {
            if( guess - x > 0 )
                printf( "Your guess is Very High\n" );
            else
                printf( "Your guess is Very Low\n" );
        }
        else if( diff > 8 )
        {
            if( guess - x > 0 )
                printf( "Your guess is Moderately High\n" );
            else
                printf( "Your guess is Moderately Low\n" );
        }
        else
        {
            if( guess - x > 0 )
                printf( "Your guess is Somewhat High\n" );
            else
                printf( "Your guess is Somewhat Low\n" );
        }
    }


    if( guess != x )
    {
        printf( "\nUnfortunately you did not guess it correctly "
                "after %d guesses ... \n"
                "The correct number was: %d\n", i, x );
    }   
}



int main()
{
    srand( time(0) ) ;

    printf( "\t*** Welcome to Game ***  \n"
                "\t*** Guess My Number *** \n"
                "\t\t(0..99)\n" ); 
    do
    {
        playGame(); 
    }
    while( more() );

    return 0;
}

Edited 2 Years Ago by David W: fixed typo

This article has been dead for over six months. Start a new discussion instead.