Home > database >  String detection on C
String detection on C

Time:01-02

Been stuck on this for ages, cant figure why the the failsafe is not detecting the string.

char EmpCatString[10];
    printf("Choose Employee Category CTO or PM or PA");
    scanf_s("%9s", &EmpCatString, sizeof(EmpCatString)) == 1;
        printf("%s\n", EmpCatString);
        if (EmpCatString == "CTO" || EmpCatString == "PM" || EmpCatString == "PA")
        {
            state = 3;
        }
        else
            printf("please enter a valid Employee Category: \nCTO:\nPM\nPA\n=>");

CodePudding user response:

use strcmp. if the result is 0, the strings are the same. == here is comparing the memory addresses.

CodePudding user response:

In your if statement

if (EmpCatString == "CTO" || EmpCatString == "PM" || EmpCatString == "PA")

What you are actually comparing are the addresses if EmpCatString and the address of the literal ”CTO”. And not the contents of the two strings. You need to compate the values pointed to by EmpCatString and the vlaues of the literal pointer ”CTO”

You one of the functions like strcmp to compare the values.

  • Related