1
#include <stdio.h>
#include <stdlib.h>

main()
{
    int Q = 1;
    while(Q==1)
    { 
        system("clear");
        printf("MAIN MENU\n");
        printf("--------------------------------------\n");
        printf("1 - See all files\n");
        printf("2 - See all files with permission\n");
        printf("3 - VIM Editor\n");
        printf("4 - EXIT\n");

        fputs("Enter Choice : ",stdout);
        char ch = getchar();

        switch(ch)
        {
            case '1' : system("ls"); break;
            case '2' : system("ls -l"); break;
            case '3' : system("vi"); break;
            case '4' : Q=0; break;
            default  : puts("Wrong Choice.."); break;
        }

        fflush(stdin);

        fputs("PRESS ENTER TO CONTINUE...",stdout);
        getchar();
    }
}

The getchar() does not pause rather it just clears the screen and starts the menu again.

What is the cause of such problems? I am using tutorialspoint codingground online compiler.

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • 6
    The second getchar reads the new line character that is generated by pressing the Enter key. – Vlad from Moscow May 04 '17 at 21:49
  • Note that `main` requires a prototype. `int main(void)` or `int main(int argc, char *argv[])`. Anything else is an implementation quirk of your compiler. Also note that `getchar` returns an `int` in order to be able to represent EOF. Though it can be safely cast to a char, it's a bad habit to directly store the result in a `char` as EOF will be cast oddly. – Schwern May 04 '17 at 21:56
  • `fflush(stdin)` invokes undefined behaviour. And `getchar` returns an `int`, not a `char` intentionally. – too honest for this site May 04 '17 at 22:09
  • More on `getchar` returning an int: http://stackoverflow.com/questions/35356322/difference-between-int-and-char-in-getchar-fgetc-and-putchar-fputc – Ilja Everilä May 05 '17 at 05:14

2 Answers2

6

You could identify the problem yourself if you displayed what the unexpected character is:

default:   printf ("Unrecognized choice:  '%c' (%d)", ch, ch);  break;

That is not a bad technique to use in any similar situation. If the code somehow gets unexpected input, say so and show what is known.

wallyk
  • 56,922
  • 16
  • 83
  • 148
-1

This happens because fflush() doesn't always flush the stdin and it's not the safest method to clear the buffer. Try using scanf(" %c", &yourcharvariable) instead or else you should use another getchar() to consume the '\n' left by your first input.

Try this code instead:

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

main()
{
    int Q = 1;
    while(Q==1)
    { 
        system("clear");
        printf("MAIN MENU\n");
        printf("--------------------------------------\n");
        printf("1 - See all files\n");
        printf("2 - See all files with permission\n");
        printf("3 - VIM Editor\n");
        printf("4 - EXIT\n");

        fputs("Enter Choice : ",stdout);
        char ch = getchar();

        switch(ch)
        {
            case '1' : system("ls"); break;
            case '2' : system("ls -l"); break;
            case '3' : system("vi"); break;
            case '4' : Q=0; break;
            default  : puts("Wrong Choice.."); break;
        }

        fflush(stdin); // This doesn't always work. 
        getchar(); // It consumes the '\n' left by your first getchar().
        fputs("PRESS ENTER TO CONTINUE...",stdout);
        getchar();
    }
}
Miguel Ruivo
  • 16,035
  • 7
  • 57
  • 87