我编写的以下程序在用户输入“ 1”后立即停止,并且未完成功能km.miles。我做错了什么?
#include <stdio.h>
void kilometer_mile() {
char inputChar;
double kilometers = 0.0;
double miles = 0.0;
printf("To convert from Kilometers to Miles enter 'K'\n"
"To convert from Miles to Kilometers enter 'M'\n");
scanf("%c", &inputChar);
if(inputChar == 'K') {
printf("Enter kilometers: ");
scanf("%c", &inputChar);
miles = kilometers/1.609;
printf("%lf kilometers is equal to %lf miles", kilometers, miles);
} else if(inputChar == 'M') {
printf("Enter miles: ");
scanf("%c", &inputChar);
kilometers = miles/1.609;
printf("%lf miles is equal to %lf kilometers", miles, kilometers);
}
}
int main() {
int inputValue = 0;
printf("Type 1 to convert from Kilometers to Miles\n"
"Type 2 to convert from Meters to Feet\n"
"Type 3 to convert from Centimetre to Inch\n"
"Type 4 to convert from Celsius to Fahrenheit\n"
"Type 5 to quit the program\n");
scanf("%d", &inputValue);
if(inputValue == 1) {
kilometer_mile();
}
return 0;
}
答案 0 :(得分:1)
问题是您没有读取用户在第一个scanf功能上按Enter键时也会发出的“ \ n”;因此,在您的kmometer_mile函数中,您实际上是在char上读取“ \ n”,而在给定的两个if中都不显示