0

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;
}


alk
  • 69,737
  • 10
  • 105
  • 255
SirCoCoNut
  • 47
  • 6

1 Answers1

0

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");
    }
}
Youssef13
  • 3,836
  • 3
  • 24
  • 41
  • 2
    The C standard tells us that `fflush(stdin);` invokes undefined behaviour. `fflush()` may only be called for "out"-streams. – alk Dec 29 '19 at 12:17
  • 1
    Calling `getchar()` twice is a poor solution that assumes that the newline immediately follows the decimal digits, which may not be the case. Better to read _all_ characters until the newline or EOF. – Clifford Dec 29 '19 at 12:51
  • @alk, Thanks for pointing to that. I've removed it. I've also pointed to another problem in the OP's code. – Youssef13 Dec 29 '19 at 12:53
  • @Clifford, Reading until the newline or EOF is a possible better solution too. – Youssef13 Dec 29 '19 at 12:53