如何让fputs函数提示错误输入并要求用户重新输入...程序停止而不是请求新输入

问题描述 投票:0回答:2

我正在计算结果。用户将输入 3 种类型的结果,即通过、失败和无效。我还需要进行 1 项检查,即当用户输入整数值以外的结果时,它将被视为错误输入。通过使用 fputs 函数,我如何要求用户仅输入整数数据?程序不会停止,而是要求用户再次输入,直到用户输入正确的数据。

#include <stdio.h>

int main(void) {
    int test_result;
    int pass;
    int fail;
    int invalid;

    for (int trainee_total; trainee_total < 6; trainee_total++) {
        printf("Enter Result (Pass=1 Fail=2) :");

        if (scanf("%d", &test_result) != 1) {
            fputs("Wrong input\n", stderr);
            return 1;
        }

        if (test_result == 1) {
            pass = pass + 1;
        } else if (test_result == 2) {
            fail = fail + 1;
        } else {
            invalid = invalid + 1;
        }
    }

    printf("The number of participants that passed is :  %d\n", pass);
    printf("The number of participants that failed is :  %d\n", fail);
    printf("Invalid input is :  %d\n", invalid);
}
c function if-statement stderr fputs
2个回答
2
投票

可以使用循环反复提示用户输入,直到输入正确。以下是您如何修改代码以使用 fputs 实现此目的的方法:

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

int main(void) {
    int test_result;
    int pass = 0;
    int fail = 0;
    int invalid = 0;

    for (int trainee_total = 0; trainee_total < 6;) {
        printf("Enter Result (Pass=1 Fail=2) : ");

        int scanf_result = scanf("%d", &test_result);

        if (scanf_result == EOF) {
            fputs("Error reading input. Exiting...\n", stderr);
            return 1;
        } else if (scanf_result == 0) {
            fputs("Invalid input. Please enter an integer.\n", stderr);
            // Clear input buffer
            int c;
            while ((c = getchar()) != '\n' && c != EOF);
        } else {
            if (test_result == 1) {
                pass++;
            } else if (test_result == 2) {
                fail++;
            } else {
                invalid++;
            }
            trainee_total++;
        }
    }

    printf("The number of participants that passed is: %d\n", pass);
    printf("The number of participants that failed is: %d\n", fail);
    printf("Invalid inputs: %d\n", invalid);

    return 0;
}

在此修改版本中:

我添加了一个变量 scanf_result 来捕获 scanf 的返回值。

如果 scanf_result 为 EOF,则表示读取输入时出错,程序终止并显示错误消息。

如果scanf_result为0,则表明输入不是有效整数,程序清空输入缓冲区后再次提示用户。

如果scanf_result为1,则表示输入成功,程序继续循环处理输入。


1
投票

您可能想要这样的东西:

#include <stdio.h>

// Read an int from the user
//   prompt       Prompte message to display to the user
//   errortext    Text to display upon wrong input
//
int GetIntFromUser(const char *prompt, const char *errortext)
{
  int value;
  while (1) {
    fputs(prompt, stdout);

    // Read the input
    if (scanf("%d", &value) != 1) {
      fprintf(stderr, "%s\n", errortext);
      
      // Clear the input buffer
      int c;
      while ((c = getchar()) != '\n' && c != EOF);
    }
    else {
      break; // Exit the loop if input is valid
    }
  }

  return value;
}


int main(void) {
  int pass = 0;   // initialize to 0 (mandatory)
  int fail = 0;   // initialize to 0 (mandatory)
  int invalid = 0;  // initialize to 0 (mandatory)

  for (int trainee_total = 0; trainee_total < 20; trainee_total++) {
    
    // declare test_result here (better style)
    int test_result = GetIntFromUser("Enter Result (Pass=1 Fail=2) :", "Wrong input");

    if (test_result == 1) {
      pass = pass + 1;
    }
    else if (test_result == 2) {
      fail = fail + 1;
    }
    else {
      invalid = invalid + 1;
    }
  }

  printf("The number of participants that passed is :  %d\n", pass);
  printf("The number of participants that failed is :  %d\n", fail);
  printf("Invalid input is :  %d\n", invalid);
}
  • GetIntFromUser
    函数的灵感来自于这个答案
  • 解释在评论里。
© www.soinside.com 2019 - 2024. All rights reserved.