0

In the example below, if I enter a character in Mac OS X terminal, the program will get stuck in an infinite loop, printing Please enter a number: line after line and never allowing the user to input anything. What's wrong with this code? What is the fix? I want to change the code in a way that if a number is not entered, the user is prompted with an error message and asked to enter a number again.

#include <stdio.h>

int main(int argc, const char * argv[]) {

    int number = 0, isnumber;
    getagin: printf("Please enter a number:\n");
    isnumber = scanf("%i", &number);
    if(isnumber) {
        printf("You enterd a number and it was %i\n", number);
    } else {
        printf("You did not eneter a number.\n");
        goto getagin;
    }

    return 0;
}

Edit: I edited the code after reading the suggestions, and fixed the infinite loop problem. This is not a bad fix for the infinite loop problem, and with a simple for loop I tell C to search for any none numeric character. The code below won't allow inputs like 123abc.

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(int argc, const char * argv[]) {

    char line[10];
    int loop, arrayLength, number, nan;
    arrayLength = sizeof(line) / sizeof(char);
    do {
        nan = 0;
        printf("Please enter a number:\n");
        fgets(line, arrayLength, stdin);
        for(loop = 0; loop < arrayLength; loop++) { // search for any none numeric charcter inisde the line array
            if(line[loop] == '\n') { // stop the search if there is a carrage return
                break;
            }
            if((line[0] == '-' || line[0] == '+') && loop == 0) {
                continue;
            } // Exculude the sign charcters infront of numbers so the program can accept both negative and positive numbers
            if(!isdigit(line[loop])) { // if there is a none numeric character then add one to nan and break the loop
                nan++;
                break;
            }
        }
    } while(nan || strlen(line) == 1); // check if there is any NaN or the user has just hit enter
    sscanf(line, "%d", &number);
    printf("You enterd number %d\n", number);
    return 0;
}
ilgaar
  • 804
  • 1
  • 12
  • 31
  • 5
    The `goto` doesn't have anything with the issue here - the same would happen if he used a while loop. – Nemanja Boric Dec 29 '13 at 20:26
  • So how can I change the code that if a number is not entered, the user is prompted with an error message and asked to enter a number again? – ilgaar Dec 29 '13 at 20:26
  • The goto has everything to do with why he doesn't see the problem, imo... – Bandrami Dec 29 '13 at 20:28
  • 2
    @Bandrami read the discussion in Vaughn's comment - the same issue without `goto` keyword. – Nemanja Boric Dec 29 '13 at 20:29
  • 5
    @Bandrami He doesn't know how `scanf` works when the contents of the buffer doesn't match the format string. Using a loop won't fix that. – howard Dec 29 '13 at 20:30

1 Answers1

0

The reason that you are getting infinite loop on entering a non-digit is the non-digit character left in the buffer as it is not read by the scanf for the next read of scanf (as it doesn't matches the format specifier). on next iteration scanf again finds this character and do not read it and exit immediately. This happens repeatedly and you are getting an infinite loop. Place a while(getchar() != '\n'); statement to consume this character.

Try this

#include <stdio.h>

int main(int argc, const char * argv[]) {

    int number = 0, isnumber;
    getagin: printf("Please enter a number:\n");
    isnumber = scanf("%i", &number);
    if(isnumber) {
        printf("You enterd a number and it was %i\n", number);
    } else {
        printf("You did not eneter a number.\n");
        while(getchar() != '\n'); // T consume all non-digits
        goto getagin;
    }

    return 0;
}
haccks
  • 104,019
  • 25
  • 176
  • 264
  • 1
    Using `getchar()` only works around the problem. When I enter `asdf 1234` (one line) into your program, I get four error messages before `scanf()` recognizes the number. IMO it makes more sense to read an entire line using `fgets()`, feed the resulting string to `sscanf()`. – hillu Dec 29 '13 at 20:43