Search code examples
cgetchar

getchar() doesnt wait for enter press


I started C just a while ago (same as coding), so I`m a noob.

My Goal:

to state that the user hasn't entered a or b and then wait for the user to press enter to return to the calculator menu.

My problem:

getchar() doesn't wait for me to press enter. (Case 3)

#include <stdlib.h>

int main()
{
    for (int i = 0;i == 0;){
        int options=0,enteredA=0, enteredB=0;
        float *a, A, *b, B, *c, C;
        a=&A,b=&B,c=&C;
        printf ("Calculator\nAvailable options:\n[1]Enter value for A\n[2]Enter value for B\n[3]Addition\n[9]Exit\n");
        scanf ("%d",&options);
        system ("clear");
        switch (options) {
            case 1:
                printf("Enter a value for A:");
                scanf ("%f",&*a);
                enteredA++;
                break;
            case 2:
                printf("Enter a value for B:");
                scanf ("%f",&*b);
                enteredB++;
                break;
            case 3:
                if ((enteredA==0) | (enteredB== 0)){
                    printf("A and B are not initialized yet. Please enter a value in the menu.\nPress [Enter] to continue to the menu:\n");
                    fflush(stdin);
                    getchar();
                    break;
                } else{
                    printf("%f+%f=%f\n",*a,*b,*c=*a+*b);
                    fflush(stdin);
                    getchar();
                    break;
                }
                break;
            case 9:i++;break;
        }
        system("clear");
    }
    printf("Calculator Shut Down");
    return 0;
}



Solution

  • In the following line:

    scanf ("%d",&options);
    

    you actually enter a number, and a newline character. The scanf function reads only the number. It leaves the newline (\n) in the input stream.

    When you call getchar(), it will find a newline in the input stream. Hence, it will read it without waiting for user input. It only wait for user input if it didn't find anything in the input stream.

    A possible workaround for this is to call getchar two times instead of one. The first call will read the already existing newline in the stream. The second call won't find anything in the input stream. So, it will wait for user input as you expect.


    I have some small comments that aren't related to your question:

    • You use scanf ("%f",&*a);. Why not just scanf("%f", a); or scanf("%f", &A); ?
    • Why you even create a pointer a for the variable A ?
    • I don't think you really need the variable c as well.
    • You don't need the variable i in the loop as well.
    • At the beginning of the loop, you keep re-initializing enteredA and enteredB variables to zero. That way, the condition in case 3: will be always true. You need to move these variables outside of the loop.
    • Your code also missing a #include <stdio.h>. I'd simplify things like the following:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        int enteredA = 0, enteredB = 0;
        while (1)
        {
            int options;
            float A, B;
            printf ("Calculator\nAvailable options:\n[1]Enter value for A\n[2]Enter value for B\n[3]Addition\n[9]Exit\n");
            scanf("%d", &options);
            getchar(); // The extra getchar to read the newline left in the stdin.
            system ("clear");
            switch (options)
            {
                case 1:
                    printf("Enter a value for A:");
                    scanf("%f", &A);
                    enteredA++;
                    break;
                case 2:
                    printf("Enter a value for B:");
                    scanf ("%f", &B);
                    enteredB++;
                    break;
                case 3:
                    if (enteredA ==0 || enteredB == 0)
                    {
                        printf("A and B are not initialized yet. Please enter a value in the menu.\nPress [Enter] to continue to the menu:\n");
                    }
                    else
                    {
                        printf("%f + %f = %f\n", A, B, A + B);
                    }
                    getchar();
                    break;
                case 9:
                    printf("Calculator Shut Down");
                    return 0;
            }
            system("clear");
        }
    }