我有一个简短的问题:
如何扩展程序,以便检查是否输入了正确的格式?...如果不正确,则程序应重复scanf。

这是我走了多远:

#include <stdio.h>

int main()
{
    float zahlen[2];
    int i = 0;

        while (i < 2 && zahlen != EOF) {
            printf("%d. Zahl", i + 1);
            scanf_s("%f", &zahlen[i]);
        }


        printf("Division: %f\n", zahlen[0] / zahlen[1]);
        printf("Produkt: %f\n", zahlen[0] * zahlen[1]);
        printf("Summe: %f\n", zahlen[0] + zahlen[1]);
        printf("Diffenrenz: %f\n", zahlen[0] - zahlen[1]);
        printf("Mittelwert: %f\n", (zahlen[0] + zahlen[1]) / 2);

    getchar();
    return 0;
}


感谢您的帮助。祝您白天/夜晚愉快。

最佳答案

您应该定义正确的格式。但是以伪代码的形式

float input;

// First get the input
do {
    sacnf_s("%f", &input);
} while (!IsRightFormat(input));  // If the input was not correct get it again


// Do your stuff here
// ...

07-25 20:53