#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
(fever=y)
is assignment.
You need (fever == 'y')
Note the '
(quotes) and also, the conditional check ==
instead of =
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.");
}
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.
2 - when you wanna say var equal to character ,you must write character in quotes like 'y'.
here you can see the correct way :
if ((fever == 'y') && (cough == 'y'))
printf("Your recommendation is to see a doctor.");
In order for the second scanf
to read an input you must put a space before both %c scanf
lines in the code.
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.
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' .
II - In the if condition you used the assignment operator '=' .This is wrong logic here . you must use the comparison operator '==' .
#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;
}
Always remember when using string you need to use "string"(" ") for character use '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'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.