C语言 C语言是或否带有字符的if语句?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19107067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C language yes or no if statement with a char?
提问by J T
#include <stdio.h>
int main(void)
{
char fever, cough; /*Sets the chars.*/
printf("Are you running a fever? (y/n)\n"); /*Asks if they have a fever and saves their input.*/
scanf("%c",&fever);
printf("Do you have a runny nose/cough? (y/n)\n"); /*Asks if they have a cough and saves their input.*/
scanf(" %c",&cough);
printf("Please verify the folling information.\nFever: %c \nRunny nose/cough: %c \n",fever,cough); /*Asks if the following info is correct.*/
if ((fever=y) && (cough=y))
printf("Your recommendation is to see a doctor.");
else if ((fever=n) && (cough=y))
printf("Your recommendation is to get some rest.");
else if ((fever=y) && (cough=n)
printf("Your recommendation is to see a doctor.");
else
printf("Your are healthy.");
return 0;
}
I get errors for the y's and n's
我收到 y 和 n 的错误
回答by karthikr
(fever=y)is assignment.
(fever=y)是赋值。
You need (fever == 'y')
你需要 (fever == 'y')
Note the '(quotes) and also, the conditional check ==instead of =
注意'(quotes) 以及条件检查==而不是=
This needs to be fixed in every occurance.
这需要在每次发生时修复。
if ((fever == 'y') && (cough == 'y')) {
printf("Your recommendation is to see a doctor.");
}
else if ((fever == 'n') && (cough == 'y')) {
printf("Your recommendation is to get some rest.");
}
else if ((fever == 'y') && (cough == 'n') {
printf("Your recommendation is to see a doctor.");
}
回答by Pooya Panahandeh
1 - in C programming language and many others equal ( = ) mean assignment operator. it means you give value to variable then if you need to say fever is equal to y you have to use == ,double equal mean equal.
1 - 在 C 编程语言和许多其他语言中,等于 (=) 表示赋值运算符。这意味着你给变量赋值然后如果你需要说发烧等于 y 你必须使用 == ,double equal mean equal 。
2 - when you wanna say var equal to character ,you must write character in quotes like 'y'.
2 - 当你想说 var 等于 character 时,你必须在像'y'这样的引号中写字符。
here you can see the correct way :
在这里你可以看到正确的方法:
if ((fever == 'y') && (cough == 'y'))
printf("Your recommendation is to see a doctor.");
回答by Daut Ibisi
In order for the second scanfto read an input you must put a space before both %c scanflines in the code.
为了让第二个scanf读取输入,您必须%c scanf在代码中的两行之前放置一个空格。
Looking like this scanf(" %c", &fever)and scanf(" %c, &cough)the reason being that when you press enter after your first input of y or n the compiler reads this as an input itself. I was told this is a tricky part of scanf.
看起来像这样scanf(" %c", &fever),scanf(" %c, &cough)原因是当您在第一次输入 y 或 n 后按 Enter 时,编译器会将其读取为输入本身。有人告诉我这是 scanf 的一个棘手部分。
回答by Usman
There are some mistakes in your code .
您的代码中有一些错误。
I- The variable fever & cough have character datatypes so in if condition the comparison variable must be in single quotes like 'y' & 'n' .
I- 变量发烧和咳嗽具有字符数据类型,因此在 if 条件下,比较变量必须在单引号中,如 'y' & 'n' 。
II- In the if condition you used the assignment operator '=' .This is wrong logic here . you must use the comparison operator '==' .
II- 在 if 条件中,您使用了赋值运算符“=”。这里的逻辑是错误的。您必须使用比较运算符 '==' 。
#include <stdio.h>
int main(void)
{
char fever, cough; /*Sets the chars.*/
printf("Are you running a fever? (y/n)\n"); /*Asks if they have a fever and saves their input.*/
scanf("%c",&fever);
printf("Do you have a runny nose/cough? (y/n)\n"); /*Asks if they have a cough and saves their input.*/
scanf(" %c",&cough);
printf("Please verify the folling information.\nFever: %c \nRunny nose/cough: %c \n",fever,cough); /*Asks if the following info is correct.*/
if ((fever=='y') && (cough=='y'))
printf("Your recommendation is to see a doctor.");
else if ((fever=='n') && (cough=='y'))
printf("Your recommendation is to get some rest.");
else if ((fever=='y') && (cough=='n')
printf("Your recommendation is to see a doctor.");
else
printf("Your are healthy.");
return 0;
}
回答by Megharaj
Always remember when using string you need to use "string"(" ") for character use 'character'(' ').
永远记住,在使用字符串时,您需要使用 "string"(" ") 来表示字符使用 'character'(' ')。
Example,
char *s = "Hi i am fine"; //string
char *c = 'g'; //character
Make the changes accordingly in your code, to check for character 'y' and 'n'
在您的代码中进行相应的更改,以检查字符 'y' 和 'n'

