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

MNY*_*MNY 1 c

我需要询问用户的输入并且他/她应该能够写一个浮点数,我需要对这两个数字进行一些计算,但我在isdigit测试后遇到问题...即使我输入一个整数continue;

这是我的代码:

#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;
}
Run Code Online (Sandbox Code Playgroud)

为上传这些功能而道歉,但由于我是新手,问题可能就在那里.干杯.

Luk*_*lsh 7

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

if(scanf("%f", &userNum) != 1) {
    // Handle item not float here
}
Run Code Online (Sandbox Code Playgroud)

由于scanf()将返回正确扫描的项目数.