尝试比较C中的字符串

时间:2016-09-01 03:39:08

标签: c string strcmp

我正在尝试使用函数 srtcmp 比较两个字符串,但是没有得到我想要的结果。该程序用C编码,只有在年龄较大且为男性时才打印用户名。任何情况下,只有执行其他指令。

#include <stdio.h>
#include <string.h>

int main() {

char name[20], sex[15];
int age;

printf("\n Your name here: "); fgets(name, 21, stdin);
printf(" Your sex here: "); fgets(sex, 16, stdin);
printf(" You age here: "); scanf("%i", &age);

if(strcmp(sex, "male")==0 && age>=18) {
    printf("\n %s you are old.\n\n", name);     
}

else {
    printf("\n You are not old.\n\n");
}

return 0;
}

0 个答案:

没有答案