C 是否可以使用isdigit()函数测试浮点?

C 是否可以使用isdigit()函数测试浮点?,c,C,我需要询问用户的输入,他/她应该有能力写一个浮点数,我需要对这两个数进行一些计算,但在isdigit测试后,我遇到了一个问题…即使我输入了一个整数,它也将继续测试 这是我的代码: #include <stdio.h> #include <ctype.h> char get_choice(void); float calc(float number1, float number2); int main() { float userNum1; float

我需要询问用户的输入,他/她应该有能力写一个浮点数,我需要对这两个数进行一些计算,但在
isdigit
测试后,我遇到了一个问题…即使我输入了一个整数,它也将继续
测试

这是我的代码:

#include <stdio.h>
#include <ctype.h>
char get_choice(void);
float calc(float number1, float number2);

int main()
{

    float userNum1;
    float userNum2;
    get_choice();

    printf("Please enter a number:\n");
    while ((scanf("%f", &userNum1)) == 1)
    {
        if (!isdigit(userNum1))
        {
            printf("Please enter a number:\n");
            continue;
        }


        printf("Please enter another number:\n");
        while ((scanf("%f", &userNum2) == 1))
        {
            if (!isdigit(userNum2))
            {
                printf("Please enter a number:/n");
                continue;
            }
            else if (userNum2 == '0')
            {
                printf("Please enter a numer higher than 0:\n");
                continue;
            }

        }

    }
    calc(userNum1, userNum2);
    return 0;
}

float calc(float number1, float number2)

{
    int answer;

    switch (get_choice())
        {
            case 'a':
                answer = number1 + number2;
                break;
            case 's':
                answer = number1 - number2;
                break;
            case 'm':
                answer = number1 * number2;
                break;
            case 'd':
                answer = number1 / number2;
                break;
        }
    return answer;
}

char get_choice(void)

{
    int choice;

    printf("Enter the operation of your choice:\n");
    printf("a. add        s. subtract\n");
    printf("m. multiply   d. divide\n");
    printf("q. quit\n");

    while ((choice = getchar()) == 1 && choice != 'q')
    {

        if (choice != 'a' || choice != 's' || choice != 'm' || choice != 'd')
        {
            printf("Enter the operation of your choice:\n");
            printf("a. add        s. subtract\n");
            printf("m. multiply   d. divide\n");
            printf("q. quit\n");
            continue;
        }


    }
    return choice;
}
#包括
#包括
char get_选项(void);
浮点数(浮点数1、浮点数2);
int main()
{
浮动用户num1;
浮动用户num2;
得到你的选择();
printf(“请输入一个数字:\n”);
while((scanf(“%f”,&userNum1))==1)
{
如果(!isdigit(userNum1))
{
printf(“请输入一个数字:\n”);
继续;
}
printf(“请输入另一个号码:\n”);
而((scanf(“%f”,&userNum2)==1))
{
如果(!isdigit(userNum2))
{
printf(“请输入一个数字:/n”);
继续;
}
else if(userNum2==“0”)
{
printf(“请输入一个大于0:\n的数字”);
继续;
}
}
}
计算(userNum1,userNum2);
返回0;
}
浮点数(浮点数1,浮点数2)
{
int答案;
开关(get_choice())
{
案例“a”:
答案=1号+2号;
打破
案例s:
答案=数字1-数字2;
打破
案例“m”:
答案=1号*2号;
打破
案例“d”:
答案=1号/2号;
打破
}
返回答案;
}
char get_选项(无效)
{
智力选择;
printf(“输入您选择的操作:\n”);
printf(“a.加s.减”);
printf(“m.乘d.除”\n);
printf(“q.quit\n”);
while((choice=getchar())==1&&choice!='q')
{
如果(选择!=“a”|选择!=“s”|选择!=“m”|选择!=“d”)
{
printf(“输入您选择的操作:\n”);
printf(“a.加s.减”);
printf(“m.乘d.除”\n);
printf(“q.quit\n”);
继续;
}
}
回报选择;
}
很抱歉也上传了这些函数,但因为我是新手,问题可能就在那里。 干杯。

isDigit()
只接受字符输入。 检查输入是否正确的最佳方法是使用

if(scanf("%f", &userNum) != 1) {
    // Handle item not float here
}
因为
scanf()
将返回正确扫描的项目数。

isDigit()
只接受字符输入。 检查输入是否正确的最佳方法是使用

if(scanf("%f", &userNum) != 1) {
    // Handle item not float here
}
因为
scanf()
将返回正确扫描的项目数。

IsDigit()函数仅在字符是否为十进制数字时进行检查

比如说:

int isdigit ( int c );
检查c是否为十进制数字字符

小数位数可以是0 1 2 3 4 5 6 7 8 9中的任意一位

如果c是十进制数字,isdigit()函数将返回非零;否则,仅当字符是否为十进制数字时,才会返回0。

IsDigit()函数检查

比如说:

int isdigit ( int c );
检查c是否为十进制数字字符

小数位数可以是0 1 2 3 4 5 6 7 8 9中的任意一位


如果c是十进制数字,isdigit()函数将返回非零;否则,它将返回0。

您也可以尝试以下方法:

int get_float(char *val, float *F){
    char *eptr;
    float f;
    errno = 0;
    f = strtof(val, &eptr);
    if(eptr != val && errno != ERANGE){
        *F = f;
        return 1;
    }
    return 0;

如果
val
指向float number函数return 1并将该数字置于
*F
,否则它返回0。

您也可以尝试以下操作:

int get_float(char *val, float *F){
    char *eptr;
    float f;
    errno = 0;
    f = strtof(val, &eptr);
    if(eptr != val && errno != ERANGE){
        *F = f;
        return 1;
    }
    return 0;

如果
val
指向浮点数字函数返回1,并将该数字放入
*F
,否则返回0。

isdigit
用于测试字符,而不是数字。
isdigit
用于测试字符,而不是数字。