我试图提示用户论坛号码,并扫描它以查看它是否是浮点数。即使输入没有小数的数字,也会回来说它是浮点数。如果输入是浮点数,则函数应返回1,否则返回0。我觉得我的功能逻辑存在问题。任何帮助,将不胜感激。谢谢!
#include <stdio.h> //including for the use of printf
#pragma warning(disable: 4996) // turn off warning about sscanf()
/* == FUNCTION PROTOTYPES == */
double getDouble(double *pNumber);
double *pNumber = NULL;
int main(void)
{
double returnedValue = 0;
double userInput = 0;
int runOnce = 0;
printf("Please Enter a float:");
userInput = getDouble(&returnedValue);
while (runOnce == 0)
{
if (userInput == 1)
{
printf("Your number is a valid float!\n");
printf("%lf", returnedValue);
}
else
{
printf("Your number is not a float!\n");
}
printf("Press ENTER key to Continue\n");
getchar();
}
}
#pragma warning(disable: 4996)
double getDouble(double *pNumber)
{
char record[121] = { 0 }; /* record stores the string from the user*/
double number = 0;
/* fgets() - a function that can be called in order to read user input from the keyboard */
fgets(record, 121, stdin);
if (scanf("%lf", &number) == 1)
{
*pNumber = number;
return 1;
}
else
{
return 0;
}
}
你叫fgets()
和scanf()
。使用其中一个但不是两个。如果您打算使用fgets()
,请使用sscanf()
来解析其结果。