我有一个创建程序的任务,询问用户他们想要转换的温度然后转换它,但我的if语句不起作用。
这是我的代码:
#include <stdio.h>
#include <string.h>
int main(void) {
//Declare Variables
float ce; //Celcius
float fa; //Fahrenheit
char in; //Input form user
float go; //If a loop should keep running
//Ask the user what they would like to convert
printf("Would you like to convert from Celsius 'C' or Fahrenheit 'F'?\n");
//Get the input from the user
scanf("%d", &in);
//Start the Loop
go = 1;
while (go == 1) {
if (in == 'F') { //If the user wants to convert from Fahrenheit
//Stop the loop
go = 0;
//Ask the user the temperature they would like to convert
printf("What temperature F would you like to convert to C?\n");
//Get the input from the user
scanf("%d", &fa);
//Crunch the Numbers
ce = ((fa - 32)/1.8);
//Print the answer
printf("The temperature in Celcius is %lf\n", ce);
} else if (in == 'C') { //If the user wants to convert from Celcius
//Stop the loop
go = 0;
//Ask the user the temperature they would like to convert
printf("What temperature C would you like to convert to F?\n");
//Get the input from the user
scanf("%d", &ce);
//Crunch the Numbers
fa = (32 + (ce * 1.8));
//Print the answer
printf("The temperature in Fahrenheit is %lf\n", fa);
} else {
//make sure the loop will continue running
go = 1;
//Print an error message
printf("That is an invalid input, please enter either a 'C' of 'F'\n");
//Take the input again
scanf("%d", &in);
}
}
//End the program
return 0;
}
无论我输入什么,它都会继续回来“这是一个无效的输入......”
如何让我的if语句起作用?我也试过strcmp只得到一个“指针和整数之间的比较”错误。这应该是一个相当简单和基本的程序,但我无法让它工作。
答案 0 :(得分:1)
问题:
在您的代码in
中声明为char
,但您在scanf()
中使用了错误的格式说明符,即%d
,同时扫描in
.. < / p>
scanf("%d",&in)
<强>解决方案强>:
以避免使用%c
作为scanf()
scanf(" %c",&in)
注意:我已
space
%c
前面\n
消耗可能来自换行符(scanf()
)的空格以前的scanf()
陈述结束
在scanf()
中,您传递的所有参数都是指针,并且必须传递与该地址中存在的变量类型匹配的确切格式说明符。
要详细了解在printf()
或currentValue = firstValue;
while (endNotYetReached(currentValue)) {
currentValue = myFunction(currentValue);
}
中使用错误的格式说明符时会发生什么情况,请参阅:click
答案 1 :(得分:1)
在这种情况下使用Switch()语句更好。
#include <stdio.h>
main()
{
float c,f,k;
int choice;
printf("Which Scale do you want to convert\n 1.Celsius\n 2.Fahrenheit\n 3.Kelvin");
printf("\n Enter Your Choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter Celsius Temperature: ");
scanf("%f",&c);
f=(9*c+32)/5;
k=c+273;
printf("\nThe Temperature is %.2f in Fahrenheit and %.2f in Kelvin\n",f,k);
break;
case 2: printf("Enter Fahrenheit Temperature: ");
scanf("%f",&f);
c=((5*(f-32))/9);
k=((5*(f-32))/9)+273;
printf("\nThe Temperature is %.2f in Celsius and %.2f in Kelvin\n",c,k);
break;
case 3: printf("Enter Kelvin Temperature: ");
scanf("%f",&k);
c=k-273;
f=((9*(k-273))/5)+32;
printf("\nThe Temperature is %.2f in Celsius and %.2f in Fahrenheit\n",c,f);
break;
default: printf("\nError. Please Restart The Program\n");
}
}
答案 2 :(得分:0)
检查您需要更改的相应更改。
#include <stdio.h>
#include <string.h>
int main(void) {
//Declare Variables
float ce; //Celcius
float fa; //Fahrenheit
char in; //Input form user
float go; //If a loop should keep running
printf("Would you like to convert into Celsius 'C' or Fahrenheit 'F'?\n");
//scanf("%d", &in);
scanf("%c", &in);
go = 1;
while (go == 1) {
if ((in == 'F') || (in == 'f')) { //If the user wants to convert from Fahrenheit
//Stop the loop
go = 0;
//Ask the user the temperature they would like to convert
//printf("What temperature F would you like to convert to C?\n");
printf("What temperature F would you like to convert to C?\n");
//Get the input from the user
scanf("%f", &fa);
//Crunch the Numbers
ce = ((fa - 32)/1.8);
//Print the answer
printf("The temperature in Celcius is %lf\n", ce);
} else if ((in == 'C')|| (in == 'c')) { //If the user wants to convert from Celcius
//Stop the loop
go = 0;
//Ask the user the temperature they would like to convert
printf("What temperature C would you like to convert to F?\n");
//Get the input from the user
scanf("%f", &ce);
//Crunch the Numbers
fa = (32 + (ce * 1.8));
//Print the answer
printf("The temperature in Fahrenheit is %lf\n", fa);
} else {
//make sure the loop will continue running
go = 1;
//Print an error message
printf("That is an invalid input, please enter either a 'C' of 'F'\n");
//Take the input again
scanf(" %c", &in); // because of \n characters entered at the end of previous scanf() statement
}
}
//End the program
return 0;
}