getchar() 不等待输入键

getchar() doesnt wait for enter press

我刚刚开始使用 C(与编码相同),所以我是菜鸟。

我的目标:

声明用户没有输入 a 或 b,然后等待用户按回车键 return 进入计算器菜单。

我的问题:

getchar() 不等我按回车键。 (案例三)

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


在下一行中:

scanf ("%d",&options);

你实际上输入了一个数字和一个换行符。 scanf 函数读取 数字。它在输入流中留下换行符 (\n)。

当你调用getchar()时,它会在输入流中找到一个换行符。因此,它将在不等待用户输入的情况下读取它。如果在输入流中找不到任何内容,它只会等待用户输入。

一个可能的解决方法是调用 getchar 两次 次而不是一次。 第一次调用将读取流中已经存在的换行符。第二次调用不会在输入流中找到任何内容。因此,它将如您所愿等待用户输入。


我有一些与您的问题无关的小评论:

  • 您使用scanf ("%f",&*a);。为什么不只是 scanf("%f", a);scanf("%f", &A);
  • 为什么还要为变量 A 创建一个指针 a
  • 我认为您也不需要变量 c
  • 您也不需要在循环中使用变量 i
  • 在循环开始时,您不断将 enteredA 和 enteredB 变量重新初始化为零。这样,case 3: 中的条件将始终为真。您需要将这些变量移出循环。
  • 您的代码还缺少 #include <stdio.h>。 我会简化如下内容:
#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");
    }
}