C代码中的问题

Problems in C code

我正在尝试用 Turbo C 制作一个简单的计算器(我现在使用 Turbo C 有自己的原因)

#include <stdio.h>
#define P printf
int loop[] = {1, 1, 1, 1};
int num;
char input[64];
void main()
{
    int num1, num2;
    char x, y;
    while(loop[0] == 1)
    {
        clrscr();

        P("Hello!, This simple calculator will help you compute 2 numbers.");
        P("\nPress the corresponding key to choose the operation you will use.");
        P("\n\nA - (A)ddition");
        P("\nS - (S)ubtraction");
        P("\nM - (M)ultiplication");
        P("\nD - (D)ivision");
        P("\n\nAnswer: ");

        while(loop[1] == 1)
        {
            x = getchar();

            if(tolower(x) == 'a')
            {
                P("\nYou have chosen addition.");
                num1 = askForNumber("\n\nEnter 1st number: ");
                num2 = askForNumber("\nEnter 2nd number: ");
                P("\n\n%d + %d = %d", num1, num2, num1+num2);
            }
            else if(tolower(x) == 's')
            {
                P("\nYou have chosen subtraction.");
                num1 = askForNumber("\n\nEnter 1st number: ");
                num2 = askForNumber("\nEnter 2nd number: ");
                P("\n\n%d - %d = %d", num1, num2, num1-num2);
            }
            else if(tolower(x) == 'm')
            {
                P("\nYou have chosen multiplication.");
                num1 = askForNumber("\n\nEnter 1st number: ");
                num2 = askForNumber("\nEnter 2nd number: ");
                P("\n\n%d * %d = %d", num1, num2, num1*num2);
            }
            else if(tolower(x) == 'd')
            {
                P("\nYou have chosen division.");
                num1 = askForNumber("\n\nEnter 1st number: ");
                num2 = askForNumber("\nEnter 2nd number: ");
                P("\n\n%g* %g = %.2f", (float)num1, (float)num2, (float)(num1/num2));
            }
            else
            {
                P("\nYou have entered an invalid character!");
                P("\n\nAnswer: ");
                continue;
            }

            while(loop[2] == 1)
            {
                P("\n\nDo you want to do another calculation? Y/N: ");
                y = getchar();
                if(tolower(y) == 'y' || tolower(y) == 'n')
                {
                    loop[2] = 0;
                }
                else
                {
                    P("\nYou have entered an invalid character.");
                    continue;
                }
            }

            loop[1] = 0;
        }

        if(tolower(y) == 'y')
        {
            continue;
        }
        if(tolower(y) == 'n')
        {
            loop[0] = 0;
        }
    }
}

int askForNumber(const char *string)
{
    P("%s", string);
    while(loop[3] == 1)
    {
        fgets(input, (sizeof(input)/sizeof(input[0]))-1, stdin);
        if(sscanf(input, "%d", &num) != 1)
        {
            num = 0;
            P("Invalid number!");
            continue;
        }
        return num;
    }
}

我有这些错误:

  1. 我计算完后,按'Y',它不停地清屏。

  2. 在 "Enter 1st number: " 之后,即使我还没有输入任何内容,"Invalid number" 也会出现一次(但我仍然可以输入一个数字,它将被保存到 'num1', "Invalid number just bugs me".

  3. 在顶部我要输入'a'或's'或'm'或'd'来选择一个操作,如果我放一些除了上面那封信,我得到这个

输出:

Answer: o

You have entered an invalid character!
Answer:

You have entered an invalid character!
Answer:

错误出现了两次,但我只输入了一次。

当输入缓冲区中没有字符时,getchar 函数将阻塞,直到按下 return 键。所有键,包括 return,都存储在输入缓冲区中,然后 getchar return 是缓冲区中的第一个字符。下一次调用 getchar 将立即 return 缓冲区中的下一个字符。

所以如果你按 'y' 然后 return,第一次调用 getchar return 是 'y' 字符,下一个 returns 换行符,即 return 键。

每次使用 getchar 跳过换行符时都需要刷新输入缓冲区:

do {
    c = getchar();
} while (c == '\n');

您需要 #include ctype.h 降低是该库的一部分,因为降低会使用该库,而它在您的代码中无处可去