c - 当我运行代码两次(一段时间/执行一段时间之后)时,scanf 无法按预期工作。 (我使用 Visual C++ Windows 控制台应用程序)

标签 c while-loop scanf user-input

我是编程新手,我正在尝试制作一个计算器,并且添加了一个 while 循环,这样如果您想重复,只需输入“1”,程序就会重复。问题是,如果我重复它,scanf() 就会中断,并且不再允许我在命令行中输入内容。 (我正在使用 Visual C++ Windows 控制台应用程序)

我试图使用fflush(stdin)来清除键盘缓冲区,但这也不起作用。

#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <Windows.h>


void main() {
    char v;
    int exit=1;
    while (exit == 1) {
        v = 0;
        //Read what type of calcualtion the user wants to do.
        printf("Type (+,-,*,/): ");
        fflush(stdin); 
        scanf("%c", &v);
        //system("cls");
        //show the user for 2 sec what he chose
        printf("Type you chose: %c", v);
        Sleep(2000);
        //system("cls");
        //here the calcualtion will take place. 
        switch (v) {
        case '+':
            printf("\nTBD +");
            break;
        //Here are some more cases that i have excluded.
        default:
            printf("Please only use '+,-,*,/' above\n");
            exit = 1;
            break;
        }
        printf("\n do you want to repeat (1==yes|0==no): ");
        scanf_s("%d", &v);
    }
}

该程序运行时的结果如下所示:

Type (+,-,*,/): +
Type you chose: +
TBD +
 do you want to repeat (1==yes|0==no): 1
Type (+,-,*,/): Type you chose:
Please only use '+,-,*,/' above

do you want to repeat (1==yes|0==no):

结果应该如下所示:

Type (+,-,*,/): +
Type you chose: +
TBD +
 do you want to repeat (1==yes|0==no): 1
Type (+,-,*,/): +
Type you chose: +
TBD +
 do you want to repeat (1==yes|0==no): 1

最佳答案

您的代码存在很多问题。其一,您不需要 Windows header 或需要使用 scanf_sfflush(stdin) 还会导致未定义的行为。您应该自己清除输入流。作为 scanf 的替代方案,请使用 fgetsfgetc 并自行执行转换。代码中的另一个问题是您在循环开始时重置 v 的值。然后你给 x 一个默认值 1,但检查 while(x==1) 你还尝试至少运行代码一次,无论初始条件如何,并且 do在这种情况下,while 循环是 while 更好的替代方案。另外,如果您在 exit == 1 处继续循环,这也只是为了命名约定,这是一种误导。如果exit == 1,那么您应该终止循环。这是非常复杂和困惑的代码。让我尝试为您清理它。

int main()  {
    //We only need a size of 3 , 1 for character, 1 for null terminator,1 for carriage return
    char v[32] = {0};
    int exit = 0;
    do{
        //Read what type of calcualtion the user wants to do.
        printf("Type (+,-,*,/): ");
        fgets(v, sizeof(v), stdin);
        //system("cls");
        //show the user for 2 sec what he chose
        printf("Type you chose: %c", *v);//dereference the pointer to the first character
        //system("cls");
        //here the calcualtion will take place.
        switch (*v) {//dereference the pointer to the first character
            case '+':
                printf("\nTBD +");
                break;
                //Here are some more cases that i have excluded.
            default:
                printf("Please only use '+,-,*,/' above\n");
        }
        printf("\n do you want to exit (1==yes|0==no): ");
        fgets(v, sizeof(v),stdin);
        exit = atoi(v);
    }while(exit != 1);
}

我们给 v 的大小为 32,但如果我们只输入 1 个字符,则大小 3 就足够了。主要是因为用fgets输入单个字符会消耗三个字节。但由于我们在循环末尾获取整数值,因此我们希望确保缓冲区中有足够的空间。例如,如果用户输入 123,缓冲区仍然正常,并且流中不会保留额外的字节。

关于c - 当我运行代码两次(一段时间/执行一段时间之后)时,scanf 无法按预期工作。 (我使用 Visual C++ Windows 控制台应用程序),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56203851/

相关文章:

c - 如何向 Makefile 添加调试选项

逗号分隔的文本文件到结构数组

c - 独家开卷

C程序使用scanf进入无限循环

c - 为什么此函数中使用 Char 数据类型?

c - 从数组和程序中提取整数值而不显示 printf 函数

c - (;;) 和 while(); 的作用是什么? C中的意思

php - Mysql COUNT,未按预期计数

php - 在不同的表格中显示所有服务名称及其下面的图片

c - 如何将标准输入扫描到可变数量的流中