开发者社区> 问答> 正文

getchar()不等待回车

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

我的目标:

指出用户尚未输入a或b,然后等待用户按Enter键返回到计算器菜单。

我的问题:

getchar()不要等我按Enter键。(情况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;
}

展开
收起
几许相思几点泪 2019-12-29 20:28:33 2893 0
1 条回答
写回答
取消 提交回答
  • 在以下行中:

    scanf ("%d",&options);
    
    

    您实际上输入了一个数字和一个换行符。该scanf功能仅读取数字。它将换行符(\ n)留在输入流中。

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

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

    我有一些与您的问题无关的小意见:

    您使用scanf ("%f",&*a);。为什么不直接scanf("%f", a);或scanf("%f", &A);? 为什么还要a为变量创建指针A? 我认为您也确实不需要该变量c。 您也不需要i循环中的变量。 您的代码也缺少#include <stdio.h>。我将简化以下内容:

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        while (1)
        {
            int options = 0, enteredA = 0, enteredB = 0;
            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);
            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(); getchar();
                    }
                    else
                    {
                        printf("%f + %f = %f\n", A, B, A + B);
                        fflush(stdin);
                        getchar(); getchar();
                    }
                    break;
                case 9:
                    printf("Calculator Shut Down");
                    return 0;
            }
            system("clear");
        }
    }
    
    2019-12-29 20:29:04
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载