如何正确处理 C 语言中 char 类型的控制台操作?

How do I correctly handle console operations with char type in C?

提问人:Matheus Caetano Rocha 提问时间:9/29/2023 更新时间:9/29/2023 访问量:47

问:

我正在做一个逻辑练习(乞丐的东西),其中向用户提供了输入“A”作为其学校成绩的算术平均值和“W”作为加权平均值的选项。然后,用户继续提供三个等级,程序调用calculate_grades功能,并根据用户选择的平均值类型在终端中显示音符的平均值 - 非常基本的东西,我知道!代码如下:

#include <stdio.h>
#include <stdbool.h>

float calculate_grades(char letter, float grade1, float grade2, float grade3) {
    float average;

    if (letter == 'A' || letter == 'a')
        average = (grade1 + grade2 + grade3) / 3;
    else
        average = (grade1 * 5 + grade2 * 3 + grade3 * 2) / (5 + 3 + 2);

    return average;
}

int main() {
    int n;
    float grade1, grade2, grade3;
    char letter;

    do {
        printf("Enter the number of students:\n");
        scanf("%d", &n);

        if (n < 0)
            printf("Invalid input! The number of students should be greater than or equal to zero.\n");
    } while (n < 0);

    for (int i = 0; i < n; i++) {
        do {
            printf("Enter 'A' for arithmetic mean or 'W' for weighted mean:\n");
            scanf(" %c", &letter);
            if ((letter != 'A' && letter != 'a') && (letter != 'W' && letter != 'w'))
                printf("Invalid input! The options are 'A' for arithmetic mean and 'W' for weighted mean.\n");
        } while ((letter != 'A' && letter != 'a') && (letter != 'W' && letter != 'w'));

        do {
            printf("Enter the first grade:\n");
            scanf("%f", &grade1);
            if (grade1 < 0)
                printf("Invalid input! The grade should be greater than or equal to zero.\n");
        } while (grade1 < 0);

        do {
            printf("Enter the second grade:\n");
            scanf("%f", &grade2);
            if (grade2 < 0)
                printf("Invalid input! The grade should be greater than or equal to zero.\n");
        } while (grade2 < 0);

        do {
            printf("Enter the third grade:\n");
            scanf("%f", &grade3);
            if (grade3 < 0)
                printf("Invalid input! The grade should be greater than or equal to zero.\n");
        } while (grade3 < 0);

        printf("Student's average: %.2f pts\n", calculate_grades(letter, grade1, grade2, grade3));
    }

    return 0;
}

然而,由于某种原因,当程序到达它要求用户计算平均值类型的部分时,终端会显示多行“无效输入”——不仅当用户输入无效输入(如“blablabla”或“lorem ipsum”)时,甚至当用户输入有效输入(如“a”或“W”)时!我以前从未以这种方式在 C 中处理过字符类型,所以我不知道问题出在哪里,GPT 提供的所有解决方案都非常复杂——我敢肯定这是没有必要的,否则我的大学老师不会一开始就给我们这个练习。我在这里没有看到的简单而明显的解决方案是什么?

C 字符 逻辑 控制台 - 应用程序

评论

4赞 Weather Vane 9/29/2023
一个简单的解决方案是获取每个输入并分析字符串。如果不正确,请输入另一个字符串。 用于格式化输入,不适用于狂野的用户键盘刺伤。这是非常不友好的。fgets()scanf
0赞 Fe2O3 9/29/2023
代码对我有用。在编译之前,您是否保存了最新的编辑更改?这是构成可执行文件的代码吗?但是,如果我输入学生,则没有错误消息......也许也一样......0if( n <= 0 )while()
0赞 Barmar 9/29/2023
您可以将所有这些简化为 .这将使代码更易于阅读和编写。letter != 'X' && letter != 'x'tolower(letter) != 'x'
0赞 Barmar 9/29/2023
如果用户输入 ,则每个扫描将读取一个字符。因此,您将收到 5 条“无效输入”消息。xxxxx
0赞 Andrew Henle 9/29/2023
您必须检查返回值,以确保它确实读取了数据。例如,如果用户输入 for ,则不会读取任何内容,保留在输入缓冲区中,并且保持不变。正如目前所写的,根据未初始化的值,这将导致一个无限循环,用户在不杀死整个程序的情况下无法打破该循环,或者问题将级联到下一个循环,问题将重复替换。 不是用户不友好,而是敌对的。scanf()QWERTYscanf("%f", &grade2);scanf()QWERTYgrade2grade2scanf()grade3grade2scanf()

答: 暂无答案