C语言是或否如果带有char的语句?

时间:2013-10-01 02:37:32

标签: c if-statement char

#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;
}

我得到y和n的错误

5 个答案:

答案 0 :(得分:7)

(fever=y)是作业。

您需要(fever == 'y')

注意'(引号)以及条件检查==而不是=

这需要在每次事件中得到解决。

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.");
}

答案 1 :(得分:0)

1 - 用C编程语言和许多其他等于(=)意味着赋值运算符。它意味着你给变量赋值然后如果你需要说发烧等于你必须使用==,双倍等于均值。

2 - 当你想说var等于字符时,你必须用引号来写字符,比如&#39; y&#39;。

在这里你可以看到正确的方法:

if ((fever == 'y') && (cough == 'y'))
printf("Your recommendation is to see a doctor.");

答案 2 :(得分:0)

为了让第二个arr = ["key11", "key12", key"13"]; 读取输入,您必须在代码中的两个scanf行之前放置一个空格。

看起来像这个%c scanfscanf(" %c", &fever)的原因是当你在第一次输入y或n后按Enter键时,编译器会将其作为输入本身读取。我被告知这是scanf的一个棘手的部分。

答案 3 :(得分:0)

您的代码中存在一些错误。

- 变量发烧&amp;咳嗽有字符数据类型,所以如果条件比较变量必须是单引号,如&#39; y&#39; &安培; &#39; N&#39;

II - 在if条件下,您使用了赋值运算符&#39; =&#39;这是错误的逻辑。你必须使用比较运算符&#39; ==&#39;

#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;
}

答案 4 :(得分:-1)

永远记住在使用字符串时,你需要使用“string”(“”)来表示字符'''('')。

Example,
char *s = "Hi i am fine"; //string
char *c = 'g'; //character

在代码中进行相应的更改,以检查字符“y”和“n”