-4

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=>");
  • this is explained at the beginning of string section in every C book. Please [find a good one and read](https://stackoverflow.com/q/562303/995714) – phuclv Jan 01 '22 at 14:44

2 Answers2

2

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

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

In your if statement

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

What you are actually comparing are the addresses of EmpCatString and the address of the literal ”CTO”. Not the contents of the two strings. You need to compare the values pointed to by EmpCatString and the values of the literal pointer ”CTO”

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

Hogstrom
  • 3,581
  • 2
  • 9
  • 25