我目前遇到的问题是程序没有完成第一个scanf命令之前的部分,只是跳过整个决策过程并返回到do-while循环的开头。我已经阅读了其他建议告诉人们使用fgetc(),因为输入转换类型后输入按钮(1-5)在输入缓冲区中创建一个空白区域,这样可以防止这种情况,但我真的不愿意,因为它是不是我目前的学习材料的一部分,作为最后的手段,我将使用它,甚至我不知道如何实现代替scanf。该程序看起来好像已经完成,并将其转换为c ++代码(我熟悉)允许它工作,但由于某种原因它在C代码中与我合作。我还尝试了stackoverflow的另一个建议,其中提到使用scanf:scanf("%c * c",& conversionDirection);这实际上允许它进入下一部分,但有时迫使你输入你的决定两次,第一个输入没有给你一个反应...我真的很感激,如果有人可以从这里测试这个代码的原因我不知道该怎么办,我已经尝试了一切。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int userChoice;
char conversionDirection;
float calculateNumber;
do
{
printf("Please enter one of the following: \n");
printf("Number\tConversion\n\n");
printf("1\t Kilogram/Pound\n");
printf("2\t Litre/Gallon\n");
printf("3\t Centimeter/Inch\n");
printf("4\t Celsius/Fahrenheit\n");
printf("5\t Quit\n");
scanf("%d", &userChoice);
if (userChoice == 1)
{
printf("Enter 'K' for Kilogram -> Pound or Enter 'P' for Pound -> Kilogram\n");
scanf("%c", &conversionDirection);
if (conversionDirection == 'K')
{
printf("Please enter the amount of Kilograms\n");
scanf("%f", &calculateNumber);
printf("%f Kilograms is equal to %f Pounds\n\n", calculateNumber, calculateNumber * 2.2);
}
else if (conversionDirection == 'P')
{
printf("Please enter the amount of Pounds\n");
scanf("%f", &calculateNumber);
printf("%f Pounds is equal to %f Kilograms\n\n", calculateNumber, calculateNumber / 2.2);
}
}
else if (userChoice == 2)
{
printf("Enter 'L' for Litre -> Gallon or Enter 'G' for Gallon -> Litre\n");
scanf("%c", &conversionDirection);
if (conversionDirection == 'L')
{
printf("Please enter the amount of Litres\n");
scanf("%f", &calculateNumber);
printf("%f Liters is equal to %f Gallons\n\n", calculateNumber, calculateNumber * 0.264172);
}
else if (conversionDirection == 'G')
{
printf("Please enter the amount of Gallons\n");
scanf("%f", &calculateNumber);
printf("%f Gallons is equal to %f Liters\n\n", calculateNumber, calculateNumber / 0.264172);
}
}
else if (userChoice == 3)
{
printf("Enter 'C' for Centimeter -> Inch or Enter 'I' for Inch -> Centimeter");
scanf("%c", &conversionDirection);
if (conversionDirection == 'C')
{
printf("Please enter the amount of Centimeters\n");
scanf("%f", &calculateNumber);
printf("%f Centimeters is equal to %f Inches\n\n", calculateNumber, calculateNumber * 0.393701);
}
else if (conversionDirection == 'I')
{
printf("Please enter the amount of Inches\n");
scanf("%f", &calculateNumber);
printf("%f Inches is equal to %f Centimeters\n\n", calculateNumber, calculateNumber / 0.393701);
}
}
else if (userChoice == 4)
{
printf("Enter 'C' for Celsius -> Fahrenheit or Enter 'F' for Fahrenheit -> Celsius");
scanf("%c", &conversionDirection);
if (conversionDirection == 'C')
{
printf("How many degrees Celsius?\n");
scanf("%f", &calculateNumber);
printf("%f degrees Celsius is equal to %f degrees Fahrenheit\n\n", calculateNumber, (calculateNumber * 9 / 5) + 32);
}
else if (conversionDirection == 'F')
{
printf("How many degrees Fahrenheit?\n");
scanf("%f", &calculateNumber);
printf("%f degrees Fahrenheit is equal to %f degrees Celsius\n\n", calculateNumber, (calculateNumber - 32) * 5 / 9);
}
}
else if (userChoice == 5)
{
printf("Program Exit");
return 0;
}
else
{
printf("Please enter a valid number");
}
} while (userChoice != 5);
return 0;
}
答案 0 :(得分:0)
使用scanf时的两个建议(尤其是数字):
1)在每次调用scanf之前使用fflush(stdin);
;
2)检查输出的结果 - 是否收到号码?
例如,如果没有正确的输入就无法工作:
do{
printf("Please, enter number: ");
fflush(stdin);
} while( 1 == scanf("%d", &number));