如何防止C&中的非数字输入再次要求用户输入 [英] How to prevent non-numeric input in C & ask user input again

查看:59
本文介绍了如何防止C&中的非数字输入再次要求用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我可以轻松地在Google中找到类似的问题,但仍然无法解决我的问题.

Actually, I can easily found a similar question in Google, but it still can not solve my question.

如何防止C语言中的非数字输入?

上部链接就像一个类似的情况.

The upper-link is like a similar case.

这是我的代码

#include <stdio.h>
int main()
{
    int n;
    printf("Enter 1 or 2?\n");
    scanf("%d", &n);
    while(n != 1 && n != 2)
    {
      printf("Please do not enter other characters\n");
      printf("Enter 1 or 2?\n");
      scanf("%d", &n);
    }
}

我希望用户输入其他数字(例如1、45、656),字符(例如a,f,u,e)或字符串(例如苹果),上层程序可以打印出错误消息并询问用户再次输入.

I hope if users enter other numbers(e.g. 1, 45, 656), characters (e.g. a, f, u, e), or string(e.g. apple), the upper program can print out an error message and ask for user input again.

是的!如果用户输入其他数字,则程序可以执行我想要的操作.但!如果用户输入其他字符,字符串,程序将继续循环播放.

Yes! if users enter other numbers, the program can do what I want. But! if users enter other characters, string, the program will keep looping.

我需要在此程序中添加什么?

What should I need to add to this program?

推荐答案

如何防止在C&中输入非数字再次要求用户输入

How to prevent non-numeric input in C & ask user input again

请勿使用 scanf() ** .使用 fgets().
scanf(%d",...)不使用非数字输入.取而代之的是,对于下一个 scanf(),有问题的输入将保留在 stdio 中.

Do not use scanf()**. Use fgets().
scanf("%d", ...) does not consume non-numeric input. Instead that offending input remains in stdio for the next scanf().

除非锁定按键,否则无法阻止非数字输入.相反,请阅读所有输入,识别非数字文本,将其扔给用户,并向用户提供有关新输入的反馈.

Code cannot prevent non-numeric input unless it locks the keys from being pressed. Instead, read all input, identify the non-numeric text, toss it and present the user with feedback for new input.

提供一个辅助函数来控制对其余代码的影响.

Make a helper function to control impact on rest of code.

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>

// return -1 on EOF
int read_int_in_range(const char *prompt, const char *reprompt, int lo, int hi) {
  if (prompt) {
    fputs(prompt, stdout);
    fflush(stdout);
  }
  char buf[100];
  while (fgets(buf, sizeof buf, stdin)) {
    char *endptr;
    errno = 0;
    long val = strtol(buf, &endptr, 10);
    // no overflow, conversion occurred, in range 
    if (errno == 0 && endptr > buf && val >= lo && val <= hi) {
      // Tolerate trailing white-space.
      while (isspace((unsigned char ) *endptr)) {
        endptr++;
      }
      // No junk after the numeric text
      if (*endptr == '\0') {
        return (int) val;
      }
    }
    if (reprompt) {
      fputs(reprompt, stdout);
      fflush(stdout);
    }
  }
  return EOF; // or `INT_MIN` or TBD code to distinguish `int` from an error.
}

用法

const char *prompt = "Enter 1 or 2?\n";
const char *reprompt = "Please do not enter other characters\n" "Enter 1 or 2?\n";
int n = read_int_in_range(prompt, reprompt, 1, 2); 


** 我建议不要在任何地方都使用 scanf()来读取用户输入,除非他们了解其缺点和局限性.


**I recommend to not use scanf() anywhere to read user input until ones understands its weaknesses and limitations.

这篇关于如何防止C&amp;中的非数字输入再次要求用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆