简单 IF Else,但当我尝试生成错误时命令框崩溃

Simple IF Else, but crashes the Command Box when I try to generate an error

是的,标题说明了一切。这很奇怪,因为在代码的另一部分,我可以正确执行它,它只是搞砸了,我完全糊涂了。

int main(int argc, char *argv[]) 
{
char EMPname[50], EMPgender[5], EMPposition[5];
int EMPage;
printf ("Midterm Examination, BSCS (ERCA)\n");
printf ("Welcome, employee!\n");
printf ("Please Input your name: ");
scanf ("%[^\n]s", &EMPname);
printf ("Age: ");
scanf ("%d", &EMPage);
printf ("Gender(M|F): ");
scanf ("%s", &EMPgender);
printf ("Position(P|PA|SP): ");
scanf ("%s", &EMPposition);
printf ("\n\n--------------------\n\n");
if(strcmp(EMPgender, "M")== 0|| strcmp(EMPgender, "m")== 0){
    if (strcmp(EMPposition, "P")== 0 || strcmp(EMPposition, "p")== 0){
        printf ("%s is a Programmer.\n", EMPname);
    }else if (strcmp(EMPposition, "PA")== 0 || strcmp(EMPposition, "pa")== 0 || strcmp(EMPposition, "pA")== 0 || strcmp(EMPposition, "Pa")== 0){
        printf ("%s is a Program Analyst.\n", EMPname);
    }else if (strcmp(EMPposition, "SP")== 0 || strcmp(EMPposition, "sp")== 0 || strcmp(EMPposition, "sP")== 0 || strcmp(EMPposition, "Sp"== 0)){
        printf ("%s is a Senior Programmer.\n", EMPname);
    }else{
        printf ("%s has no position.\n", EMPname);
    }
    printf ("Age: %d, Gender: Male\n", EMPage);
}else if (strcmp(EMPgender, "F")== 0|| strcmp(EMPgender, "f")== 0){
        if (strcmp(EMPposition, "P")== 0 || strcmp(EMPposition, "p")== 0){
        printf ("%s is a Programmer.\n", EMPname);
    }else if (strcmp(EMPposition, "PA")== 0 || strcmp(EMPposition, "pa")== 0 || strcmp(EMPposition, "pA")== 0 || strcmp(EMPposition, "Pa")== 0){
        printf ("%s is a Program Analyst.\n", EMPname);
    }else if (strcmp(EMPposition, "SP")== 0 || strcmp(EMPposition, "sp")== 0 || strcmp(EMPposition, "sP")== 0 || strcmp(EMPposition, "Sp"== 0)){
        printf ("%s is a Senior Programmer.\n", EMPname);
    }else{
        printf ("%s has no position.\n", EMPname);
    }
    printf ("Age: %d, Gender: Female\n", EMPage);
}else{
    if (strcmp(EMPposition, "P")== 0 || strcmp(EMPposition, "p")== 0){
        printf ("%s is a Programmer.\n", EMPname);
    }else if (strcmp(EMPposition, "PA")== 0 || strcmp(EMPposition, "pa")== 0 || strcmp(EMPposition, "pA")== 0 || strcmp(EMPposition, "Pa")== 0){
        printf ("%s is a Program Analyst.\n", EMPname);
    }else if (strcmp(EMPposition, "SP")== 0 || strcmp(EMPposition, "sp")== 0 || strcmp(EMPposition, "sP")== 0 || strcmp(EMPposition, "Sp"== 0)){
        printf ("%s is a Senior Programmer.\n", EMPname);
    }else{
        printf ("%s has no position.\n", EMPname);
    }
    printf ("Age: %d, Gender: Unknown\n", EMPage);
system ("PAUSE");
return 0;

}

程序做的是简单的输入输出,但是IF ELSE部分需要我们比较chars。 EMPposition 和 EMPgender 部分的预期输入是此处引起最多问题的输入。

编辑:这次我展示了我的全部代码,soz。

这是一行错位的括号:

if (strcmp(EMPposition, "SP")== 0 || strcmp(EMPposition, "sp")== 0 || strcmp(EMPposition, "sP")== 0 || strcmp(EMPposition, "Sp"== 0))

要更正它替换

strcmp(EMPposition, "Sp"== 0)

strcmp(EMPposition, "Sp") == 0

您将 "Sp" == 0 的结果传递给 0 函数,然后该函数尝试取消引用它,调用未定义的行为。

你的编译器应该给你一个警告,在 gcc 上我得到:

warning: passing argument 2 of 'strcmp' makes pointer from integer without a cast [-Wint-conversion]