-1
#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

4

5 回答 5

8

(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.");
}
于 2013-10-01T02:39:46.627 回答
0

您的代码中有一些错误。

I - 变量发烧和咳嗽具有字符数据类型,因此在 if 条件下,比较变量必须用单引号括起来,例如 'y' 和 'n' 。

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;
}
于 2018-02-11T09:04:36.680 回答
0

为了让第二个scanf读取输入,您必须%c scanf在代码的两行之前放置一个空格。

看起来像这样scanf(" %c", &fever)scanf(" %c, &cough)原因是当您在第一次输入 y 或 n 后按 enter 时,编译器会将其读取为输入本身。有人告诉我这是 scanf 的一个棘手部分。

于 2018-02-11T07:15:24.260 回答
0

1 - 在 C 编程语言和许多其他语言中,等于 ( = ) 表示赋值运算符。这意味着你给变量赋值,那么如果你需要说发烧等于 y,你必须使用 == ,double equal 表示相等。

2 - 当你想说 var 等于字符时,你必须用引号写字符,如 'y'。

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

if ((fever == 'y') && (cough == 'y'))
printf("Your recommendation is to see a doctor.");
于 2017-04-03T08:08:16.503 回答
-2

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

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

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

于 2013-10-01T07:16:59.560 回答