#include <stdio.h>
#include <stdlib.h>
int main()
{
float marks;
char sports;
char dance[10];
printf("Enter your marks:\n");
scanf(" %f",&marks);
printf("Do you play sports?? <y/n>\n");
scanf(" %c",&sports);
printf("Do you dance?? <yes/no>\n");
scanf(" %s",dance);
if((marks >=70) && (sports == 'y') &&(dance == "yes"))
{
printf("You are a Good Student\n");
} else{
printf("You are a Average student\n");
}
return 0;
}
在任何情况下我都不会得到输出,因为你是一个好学生 如果我将舞蹈作为字符,我会得到输出,但我希望它使用字符串来解决。 谁能告诉我这段代码有什么问题。
最佳答案
当你执行dance == "yes"
时,你正在比较两个指针(指向数组dance
中第一个元素的指针,以及指向第一个字母的指针在"is"
)。这两个指针永远不会相同。
要比较字符串,您需要使用 strcmp
功能:
strcmp(dance, "yes") == 0
关于C编程-3 if语句中的测试条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39386060/