我需要询问用户的输入,他/她应该有能力写一个浮点数,我需要对这两个数字进行一些计算,但我在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;
}
也为上传功能道歉,但由于我是新手,问题可能就在那里。干杯。