int main(int argc, const char * argv[])
{
char userInput;
int centimeter;
int meter;
float centimeterFloat = 0.0;
float meterFloat = 0.0;
printf("Enter c for centimeter to meter OR m for meter to centimeter: ");
userInput = getchar();
//If the user types in 'm' or 'M' the progrsm will ask you to type in the length in meters and it will converts to centimeters.
if (userInput == 'M')
userInput = 'm';
if (userInput == 'm')
{
//This calculation is to convert meters to centimeters.
printf("\nEnter the length in meter: ");
scanf("%d", &meter);
centimeterFloat
= (meter * 100);
printf("\nmeter\tcentimeter\n");
printf("%d\t\t\t%4.0f\n", meter, centimeterFloat);
}
//If the user types in 'c' or 'C' the program will ask you to type in the length in centimeters and it will converts to meters.
if (userInput == 'C')
userInput = 'c';
else if (userInput == 'c')
{
printf("\nEnter the length in centimeter: ");
scanf("%d", ¢imeter);
//This calculation is to convert centimeters to meters.
meterFloat = (centimeter * 1/100);
printf("\nmeter\tcentimeter\n");
printf("%3.1f\t\t\t%d\n", meterFloat, centimeter);
}
return 0;
}
这是我的代码,但是当我将输入设为小数时,输出无法正确输出,请帮忙,如果我输入 10cm,米的结果不会以十进制显示,而是 0,还有一个问题,如何进行异常处理带有 if else 语句的程序?请帮帮我,非常感谢