【问题标题】:How to check using isdigit() if there more than one character如果有多个字符,如何使用 isdigit() 检查
【发布时间】:2017-06-16 22:20:41
【问题描述】:

所以,我还是个编码新手,我的朋友很好地教了我很多东西,我想快速学习它,然后他给了我这个练习,让我在家里学习 他给了我这个关于预订桌号的问题,所以我试着去做.. 但是我在这部分检查表号是否是数字时遇到了麻烦......我在谷歌上搜索如何,但他们只举例说明如何用单个字符来做......所以,我做了一个帐户然后在这里问xD 顺便说一句,表的最大数量是 25。

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>

bool checkDigit(char tablenum){
    char digit[3];
    int n;
    int value = tablenum;
    if(tablenum>0 && tablenum<10)
        n=1;
    if(tablenum>9 && tablenum < 26)
        n=2;
    for(int i=0;i<n;i++){
        digit[i] = value%10;
        value /= 10;
    }
    if(n==1){
        printf("digit 0 = %d\nisdigit = %d\n",tablenum,isdigit(tablenum)); //just checking the isdigit value
        if(isdigit(digit[0])!=0){
            puts("It's digit");
            return true;
        }
        else{
            puts("It's not digit");
            return false;
        }
    }
    if(n==2){
        printf("digit 0 =  %d isdigit 0 = %d \ndigit 1 = %d isdigit 1 = %d\n",digit[0],isdigit(digit[0]),digit[1],isdigit(digit[1])); //checking the values too
        if(isdigit(digit[0]) != 0 && isdigit(digit[1]) != 0){
            puts("It's digits!");
            return true;
        }
        else{
            puts("It's not digits!");
            return false;
        }
    }
    else{
    printf("It's not digit!")
    return false;
    }
}

int main(){
    int num;
    bool itsdigit;
    do{
    scanf("%d",&num);fflush(stdin);

    itsdigit = checkDigit(num);

    }   while(itsdigit != 1 );
    getchar();
    return 0;
}

输入=“25”“1”“0”“测试”

输出 = "这是一个数字" "这是一个数字" "这不是一个数字" "

由于表只有1到25

当我输入“1”时,它显示正确的数字,但当它输入 isdigit() 时,我一直得到 0。

我想知道什么时候错了,请给出解释。 之前谢谢!

【问题讨论】:

  • 与您的问题无关,但您真的不应该使用stdin 之类的输入流调用fflush。它在 C 规范中明确标记为 未定义的行为
  • 这段代码看起来非常复杂。
  • 我认为你应该先从一些更简单的问题开始。
  • 那不是 C 或者它不完整。如果你编译为 C++,请更改标签!
  • num 是一个int(一个数字),因此它不可能包含"test"。你需要读一个 string 但那是你水平的另一个故事....

标签: c++


【解决方案1】:

你可能想要这个:

bool IsOnlyDigits(const char text[])
{
  int length = strlen(text);

  for (int i = 0; i < length; i++)
  {
    if (!isdigit(text[i]))
      return false;
  }

  return true;
}


int main() {
  char num[50];

  scanf("%s", num);

  if (IsOnlyDigits(num))
    puts("It's digits!");
  else
    puts("It's not digits!");

  fflush(stdout);
  getchar();
  return 0;
}

免责声明: 非错误检查和非边界检查代码。为简洁起见,省略了#includes。已在 Visual Studio 2015 下进行了测试,似乎在这里运行良好。

输入输出示例:

123
It's digits!

12a
It's not digits!

abc
It's not digits!    

要理解这一点,您需要了解字符串和数组。只需阅读 C 教科书中的相应章节即可。

【讨论】:

  • 感谢您的回复,很抱歉搞砸了..无论如何,当我在您的代码中输入任何数字时,它突然停止工作..
  • “停止工作” 你是什么意思?您收到任何错误消息吗?您的输入究竟是什么?
  • 我刚刚更改了程序的结尾(在getchar(); 之前添加了fflush(stdout);)。试试这个效果是否更好。
  • .exe文件崩溃,当我输入“1”“12”或“123”时,新的答案还是一样的情况
  • 因什么消息而崩溃??你用什么IDE,编程环境,操作系统?你确定你执行了答案中的程序吗?
【解决方案2】:

您可以改为创建一个 char 或 int 类型的数组,遍历该数组并使用 isdigit() 函数检查任何字符实例是否为数字。

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
  char arr[10];
  cin>>arr;
  for(int i=0;i<strlen(arr);i++){
    if(isdigit(arr[i])){
        cout<<arr[i]<<" is a digit\n";
    }
    else {
        cout<<arr[i]<<" is not a digit\n";
    }
  }
  return 0;
}

【讨论】:

  • 兄弟,对标签感到抱歉,我认为我是标签错误的人,事实证明我是对的.. 感谢您尝试回答!
  • 没问题,原来我的答案和答对的人一模一样,想知道为什么我没有注意到。
【解决方案3】:

你有多个问题。

让我们先来仔细看看这两行:

checkDigit(num);
itsdigit = checkDigit;

第一行调用 checkDigit,但丢弃了结果。

第二行调用函数,编译器将让它衰减到函数的指针并将该指针分配给itsdigit。你的编译器应该抱怨它。

此外,isdigit必须在字符为数字时返回 1,它只需要返回非零值。

最后,在digits 数组中,您不存储字符,而是存储整数值。 isdigit(1) 为假,isdigit('1') 为真。仅仅因为您使用类型 char 不会自动将字符存储在变量中。考虑到您读取一个整数,并将其数字存储在digits 数组中,因此无需检查数字是否实际上是数字。数字当然是数字。

【讨论】:

  • 修复了它,改变了 if,和 itsdigit = checkDigit(num);但仍然得到相同的结果
  • 我不明白这部分Lastly, in the digits array you don't store characters, you store integer values. isdigit(1) will be false while isdigit('1') will be true. Just because you use the type char doesn't automatically store characters in the variables. And considering that you read an integer, and store its digits in the digits array, there's no need to check if the digits are actually digits. Of course the digits will be digits.我实际上知道我有这个问题,问题是我如何让它像 isdigit('digit[0]') 或类似的东西。 .
  • @IrfandyJip 我还告诉你,你不需要需要 isdigitdigits 数组中存储的值您读取的数字中的数字。如果您输入15 作为数字,那么digits[0] 将是数字 5digits[1] 将是数字 1。它们是数字而不是字符,作为个位数的数字,您不需要确保它们是数字。您一直在处理 数字 而不是字符串。至少在您想检查数字是否为数字之前(当然是单个数字)。
猜你喜欢
  • 1970-01-01
  • 2021-12-30
  • 2018-02-22
  • 2018-08-08
  • 1970-01-01
  • 1970-01-01
  • 2014-01-02
  • 2021-03-06
  • 1970-01-01
相关资源
最近更新 更多