Search code examples
cstring-comparisonstrcmp

String compare in C for a password


void main()
{
    Password();
}


int Password()
{
    // Declare local variables//
    char cPassCode[] = "String";
    int iFlag, iComparison = 0;

    // Run the code to check the password//
    while (iFlag = 0)
    {
        printf("Please enter the password: ");
        scanf("%s", cPassCode);
        iComparison = strcmp(cPassCode, "A23bc5");
        if (iComparison = 0)
        {
            ArrayPrinter(Array);
            iFlag = 1;
        }
        else
        {
            printf("Wrong password");
            iFlag = 0;
        }
        return(iFlag);
    }
}

I edited the section of code and as far as I can tell it should run properly when the password A23bc5 is entered. However it is always returning a wrong password. Any ideas?


Solution

  • if (iComparison = 0)
    

    Is assigning 0 to the variable and then testing it, and 0 evaluates to false.

     if (iComparison == 0)
    

    Is checking if the variable is 0, which is probably what you meant