简体   繁体   English

使用 isdigit 以简单的方式检查 C++ 中的输入是否为整数

[英]Check if the input is a whole number in C++ in simple way using isdigit

This is what I have tried这是我尝试过的
It would always act as false no matter the input无论输入如何,它总是表现为假
How could I do it right in a simple way if possible and why doesn't it work?如果可能的话,我怎么能以一种简单的方式正确地做到这一点,为什么它不起作用?
I am trying to create a condition that would ask if the scanned user input is one of the numbers and if not it asks them to only write one of the shown numbers and shows what the options are.我正在尝试创建一个条件,询问扫描的用户输入是否是数字之一,如果不是,它要求他们只写一个显示的数字并显示选项是什么。

void konc()
{
    exit (0);
}


void scan()
{
    int ch = 0;
    printf("0 - \n");
    printf("1 - \n");
    printf("2 - \n");
    printf("3 - \n");
    printf("4 - \n");
    printf("5 - \n");
    printf("6 - \n");
    printf("7 - \n");
    scanf("%d", &ch);
    
    while (isdigit(int(ch)) == false || ch < 0 || ch > 7){
        printf("Must be one of the numbers\n");
        fflush(stdin);
        scan();
    }
    if (ch == 0){
        konc();
    }



    printf("%d", ch);
    scan();
}

int main()
{

    scan();

    return 0;
}

The problem is in the checking logic.问题在于检查逻辑。

The number 0 is not the same thing as the character '0' (which is mapped to a value of 48 ).数字0与字符'0'不同(映射到值48 )。

Change the 0 to '0' .0更改为'0' (and 7 also) (还有7

You have a few issues:你有几个问题:

  1.  int ch = 0; ... scanf("%d", ch);

    scanf needs to be given a pointer to the data it's reading into, so this should be scanf("%d, &ch);需要给scanf一个指向它正在读取的数据的指针,所以这应该是scanf("%d, &ch);

  2.  while (isdigit(int(ch)) == false || ch < 0 || ch > 7){

    ch is already an int, so using isdigit doesn't make sense. ch已经是一个 int,所以使用isdigit没有意义。 It's already a number.已经是一个数字了。 Just check the range ( while (ch < 0 || ch > 7) { )只需检查范围( while (ch < 0 || ch > 7) {

  3.  fflush(stdin);

    Using fflush on an input stream is undefined behavior.在输入 stream 上使用fflush是未定义的行为。 Get rid of this.摆脱这个。

  4. Instead of calling scan recursively, you should use a loop to jump back to the top:您应该使用循环跳回顶部,而不是递归调用scan

     int number; while(1) { printf("0 - \n"); printf("1 - \n"); printf("2 - \n"); printf("3 - \n"); printf("4 - \n"); printf("5 - \n"); printf("6 - \n"); printf("7 - \n"); scanf("%d", &number); if(number < 0 || number > 7) { printf("Must be one of the numbers\n"); continue; // Go back to the top of the loop } break; } // Now number is between 0 and 7

your logic is wrong.你的逻辑是错误的。 Make a loop which will continue until the user chooses a right number.做一个循环,直到用户选择正确的数字。

#include <conio.h>
#include <stdio.h>
int main(void)
{
    scan();
}

void scan()
{
  printf("Choose any of the following numbers:\n");
  do
  {
    printf("0 - \n");
    printf("1 - \n");
    printf("2 - \n");
    printf("3 - \n");
    printf("4 - \n");
    printf("5 - \n");
    printf("6 - \n");
    printf("7 - \n");
    char ch;  // now it is not undefined
    scanf("%c",&ch);
    fflush(stdin);
    if(ch<48||ch>55)  //ascii of '0' is 48 and ascii of '7' is 55.
      printf("Must be one of the numbers\n");
    else
      break;
  }while(1);
}

I have tested it.It will surely work.Also no need of isDigit()我已经测试过了。它肯定会工作。也不需要 isDigit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM