Menu

#152 [Bug] Console Input Loop C Template

None
fixed
2015-03-06
2015-03-05
Daimao
No

The Console Input Loop C Template does not work properly. Entering a character other than a/q will not produce the invalid input message. I quickly hacked a fix that both maintains the original behavior and keeps it as simplified as possible (for the benefit of new coders). Here it is:

#include <stdio.h>

void Foo() {
    printf("Hello World!\n");
}

int main(int argc, char **argv) {
    char input = 0;

    printf("Hello! This is a console application.\n");
    printf("Press q to quit, press a to execute foo.\n");
    while(1) {
        if(scanf("%c",&input) == 1) {
            if(input == 'a') {
                Foo();
            } else if(input == 'q') {
                break;
            } else if(input != '\n' && input != ' ') {
                printf("Invalid input! Ignoring...\n");
            }
        }
    }

    return 0;
}

Discussion

  • Daimao

    Daimao - 2015-03-06

    Alternatively the following solution will also work:

    #include <stdio.h>
    
    void Foo() {
        printf("Hello World!\n");
    }
    
    int main(int argc, char** argv) {
        char input = 0;
    
        printf("Hello! This is a console application.\n");
        printf("Press q to quit, press a to execute foo.\n");
        while(1) {
            if(scanf(" %c",&input) == 1) {
                if(input == 'a') {
                    Foo();
                } else if(input == 'q') {
                    break;
                } else {
                    printf("Invalid input! Ignoring...\n");
                }
            }
        }
    
        return 0;
    }
    

    However it is debatable if this one is easier for a novice to understand than my initial submission. However, it would potentially have them reading the scanf() documentation/references/manpage, which is a good thing.

     
  • orwelldevcpp

    orwelldevcpp - 2015-03-06
    • status: open --> fixed
    • assigned_to: orwelldevcpp
    • Milestone: -->
     
  • orwelldevcpp

    orwelldevcpp - 2015-03-06

    I have updated the input loop projects for both C and C++ to always produce some kind of output when the return key is pressed. For example, the C file now looks like this:

    #include <stdio.h>
    
    void Foo() {
        printf("Hello World!\n");
    }
    
    int main(int argc, char** argv) {
        char input = 0;
    
        printf("Hello! This is a console application.\n");
        printf("Press q to quit, press a to execute foo.\n");
        while(1) {
            if(scanf("%c",&input) == 1) {
                if(input == 'a') {
                    Foo();
                } else if(input == 'q') {
                    break;
                } else if(input != '\n') {
                    printf("Unknown command '%c'! Ignoring...\n",input);
                }
            } else {
                printf("Invalid input! Ignoring...\n");
            }
        }
    
        return 0;
    }
    
     

Log in to post a comment.