How do I print out an error message when the user enters a floating number or characters in C language?

Viewed 41

I'm new to programming.I wanted to create a game such that the user guesses a number between 0 and 10.However,I want to print an error message when the user enters a floating number such as 7.5 or a character or a number out of range such as 11 or -5.How do I do that ?

int main()
{
    int answer,randomNumber;
    int correct = 0;
    int tries = 0;
    char again ;
    srand(time(NULL));
    do{
           randomNumber = rand() % 10;
           printf("Guess a number between 0 and 10:\n");
           while(correct ==0){
                while(scanf(" %d",&answer)!=1){
                    printf("Wrong input.\n");
                    scanf(" %d",&answer);
            }
            if(answer<0 || answer>10){
                printf("Invalid input.Enter a number between 0 and 10.\n");
                continue;
            }
            if(answer==randomNumber){
                printf("Congratulations..%d is the correct number.\n",answer);
                tries ++;
                correct = 1;
            }else if(answer > randomNumber){
                printf("Choose a lower number.\n");
                tries++;
            }else if (answer<randomNumber){
                printf("Choose a bigger number.\n");
                tries ++;
            }else{
                printf("Wrong input.Try again..\n");
            }
        }printf("Do you want to play again ? (Y/N):\n");
        scanf(" %c",&again);
        again = toupper(again);
        while(again!='N' && again!='Y'){
            printf("Invalid input.Please type Y or N.\n");
            scanf(" %c",&again);
            again = toupper(again);
        }
    }while(again != 'N');



    return 0;
    }
1 Answers

Consider using fgets to capture input.
strtol can be used to parse input. If last does not point to the newline at the end of input, there are unprocessed characters.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

int main ( void) {
    char input[100] = "";
    int answer,randomNumber;
    int good = 0;
    int correct = 0;
    int tries = 0;
    char again ;
    srand ( time ( NULL));
    do{
        randomNumber = rand() % 10;
        printf ( "Guess a number between 0 and 10:\n");
        while ( correct == 0) {
            do {
                good = 1;
                char *last = NULL;
                if ( fgets ( input, sizeof input, stdin)) {
                    answer = strtol ( input, &last, 10);
                    if ( '\n' != *last || last == input) {
                        printf ( "Wrong input.\n");
                            good = 0;
                    }
                    else {
                        if ( answer < 0 || answer > 10){
                            printf ( "Invalid input.Enter a number between 0 and 10.\n");
                            good = 0;
                        }
                    }
                }
                else {
                    fprintf ( stderr, "fgets EOF\n");
                    exit ( EXIT_FAILURE);
                }
            } while( ! good);
            if (answer == randomNumber) {
                printf ( "Congratulations..%d is the correct number.\n",answer);
                tries ++;
                correct = 1;
            } else if ( answer > randomNumber) {
                printf ( "Choose a lower number.\n");
                tries++;
            } else if ( answer < randomNumber) {
                printf ( "Choose a bigger number.\n");
                tries ++;
            } else{
                printf ( "Wrong input.Try again..\n");
            }
        }
        correct = 0;
        printf ( "Do you want to play again ? (Y/N):\n");
        do {
            if ( fgets ( input, sizeof input, stdin)) {
            }
            again = toupper ( (unsigned char)input[0]);
            if ( again != 'N' && again != 'Y') {
                printf ( "Invalid input.Please type Y or N.\n");
            }
        } while ( again != 'N' && again != 'Y');
    } while ( again != 'N');

    return 0;
}
Related