如何检查scanf中的变量是否不是数字?

时间:2019-03-20 23:24:16

标签: c

我必须防止在程序中将字母放入scanf中。我尝试了“ isalpha”,但似乎没有用。这是我的代码:

int main() 
{
    float T[100] = {0}; 
    float *pt=T;
    float suma = 0, zmienna;
    int rozmiar;

    printf("How many numbers would you like to put in: ");
    scanf("%d", &rozmiar);
    if (isalpha((char)rozmiar) != 0)
    {
        printf("Incorrect input");
        return 1;
    }
    else if(rozmiar>100)
    {
        printf("Incorrect input data");
        return 2;
    }
    else
    printf("\n Enter the number: \n");
    for (int i = 0; i<rozmiar; i++) 
    {
        scanf("%f", &zmienna);
        *(pt+i)=zmienna;
    }
}

0 个答案:

没有答案