询问用户是否想从 C 转换为 F 然后进行转换的 C 程序
C Program That asks If the User wants to Convert from C to F then does the conversion
我有一个任务要创建一个程序,询问用户他们想要转换什么温度然后转换它,但我的 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;
}
无论我输入什么,它都会返回 "That is an invalid input..."
如何让我的 if 语句起作用?我也试过 strcmp 只是为了得到一个 "Comparison between pointer and integer" 错误。这应该是一个相当简单和基本的程序,但我无法让它工作。
问题:
在您的代码中 in
被声明为 char
但是,您在 scanf()
中使用了错误的格式说明符,即扫描 in
时 %d
。 .
scanf("%d",&in)
解决方法:
避免在 scanf()
中使用 %c
作为格式说明符进行扫描
scanf(" %c",&in)
Note: I've given space
infront of %c
to consume white spaces that might come from newline characters (\n
) entered at the end of previous scanf()
statements
在scanf()
中,您传递的所有参数都是指针,并且必须传递与该地址处存在的变量类型匹配的确切格式说明符。
要了解有关在 scanf()
或 printf()
中使用错误格式说明符时会发生什么的更多信息,请参阅:click
检查您需要更改的相应更改。
#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;
}
在这种情况下使用 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");
}
}
我有一个任务要创建一个程序,询问用户他们想要转换什么温度然后转换它,但我的 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;
}
无论我输入什么,它都会返回 "That is an invalid input..."
如何让我的 if 语句起作用?我也试过 strcmp 只是为了得到一个 "Comparison between pointer and integer" 错误。这应该是一个相当简单和基本的程序,但我无法让它工作。
问题:
在您的代码中 in
被声明为 char
但是,您在 scanf()
中使用了错误的格式说明符,即扫描 in
时 %d
。 .
scanf("%d",&in)
解决方法:
避免在 scanf()
%c
作为格式说明符进行扫描
scanf(" %c",&in)
Note: I've given
space
infront of%c
to consume white spaces that might come from newline characters (\n
) entered at the end of previousscanf()
statements
在scanf()
中,您传递的所有参数都是指针,并且必须传递与该地址处存在的变量类型匹配的确切格式说明符。
要了解有关在 scanf()
或 printf()
中使用错误格式说明符时会发生什么的更多信息,请参阅:click
检查您需要更改的相应更改。
#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;
}
在这种情况下使用 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");
}
}