Tasos500
Tasos500

Reputation: 117

strcmp refuses to work

I've tried everything, yet strcmp (as well as strncmp) always give a value other than 0, both with and without using pointers, as well as inside and outside functions. What should I do?

The point of the program is to create a student data system for signing up and in, as well as managing and sorting said data, the latter which I haven't implemented yet.

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


int signup(char* ET, char* OT, int* G1T, int* G2T, int* G3T, int* avg, char* status, int* LI, int* sT, int* TT, char** UT, int** PT)
{
int i;
char LU[21];
int ID;
if (strcmp (status, "Register") == 0)
{
    printf("Enter last name: ");
    scanf("%s", ET);
    printf("Enter first name: ");
    scanf("%s", OT);
    while(1)
    {

        printf("Enter C grade: ");
        scanf("%d", G1T);
        if (*G1T >= 0 && *G1T <= 10)
        {
            break;
        }
    }
    while(1)
    {

        printf("Enter Java grade: ");
        scanf("%d", G2T);
        if (*G2T >= 0 && *G2T <= 10)
        {
            break;
        }
    }
    while(1)
    {

        printf("Enter C++ grade :");
        scanf("%d", G3T);
        if (*G3T >= 0 && *G3T <= 10)
        {
            break;
        }
    }
    *avg = (*G1T + *G2T + *G3T)/3;
    *UT[*TT] = *ET;
    *sT = 1;
}
    else
{
    printf("Enter username: ");
    scanf("%s", &LU);
    for (ID = 0; ID < 100; ID++)
    {
        if (strncmp(LU, UT[ID], 20) == 0)
        {
            break;
        }
    }
    if (ID == 100)
    {
        return 0;
    }
    else
    {
        printf("Enter password: ");
        scanf("%s", LU);

    }

}

return 0;
}




void pass(char** User, int** Pass, int* Total)
{
int cd[21];
int i, j;

for (i=0; i<21; i++)
{
    cd[i] = *User[i];
    if (i%2 == 0)
    {
        if(cd[i] >= 97 && cd[i <= 122])
        {
            cd[i] = cd[i] - 32;
        }
    }
    else
    {
        if(cd[i] >= 65 && cd[i] <= 90)
        {
            cd[i] = cd[i] + 32;
        }
    }

}


}
















int main(void) {

int i, j, z;
int succ, *sT;
char intro[9], *status;
int Total = 0;
int* TT;
int LoggedIn = 0;
int* LI;
char Ep[100][21], *ET, On[100][21], *OT;
int Age[100], *AgeT, Gr1[100], *G1T, Gr2[100], *G2T, Gr3[100], *G3T, avg[100], *avgT;
char UN[100][21], *UT[100];
int PW[100][21], *PT[100];


while(1)
{
    system("cls");
    if (succ = 0)
    {
        printf("ERROR: Last name found.");
    }
    succ = 1;
    while(1)
    {
        printf("Type your option (Login/Register): ");
        scanf("%s", intro);
        if ((strcmp (intro, "Login") == 0) || (strcmp (intro, "Register") == 0))
        {
            break;
        }
    }

    if ((strcmp (intro, "Login") == 0) || (strcmp (intro, "Register") == 0))
    {
        for(i = 0; i < 100; i++)
        {
            UT[i] = &UN[i][0];
            PT[i] = &PW[i][0];
        }
        ET = &Ep[Total][0];
        OT = &On[Total][0];
        G1T = &Gr1[Total];
        G2T = &Gr2[Total];
        G3T = &Gr3[Total];
        avgT = &avg[Total];
        LI = &LoggedIn;
        status = &intro[0];
        sT = &succ;
        TT = &Total;
        signup(ET, OT, Gr1, Gr2, Gr3, avg, status, LI, sT, TT, UT, PT);
        for(i = 0; i<Total; i++)
        {
            if(strncmp(UN[Total], UN[i], 20) == 0)
            {
                succ = 0;
            }
        }
        if (succ == 1)
        {
        pass(UT, PT, TT);
        Total++;
        }
    }
}

return 0;
}

An example for my inputs is the following:

Register LastName FirstName 4 5 6

Then:

Register LastName FN2 7 6 5

And I expect to see "ERROR: Last name found." right above the starting message. However, it never appears, suggesting strcmp failed.

Upvotes: -5

Views: 127

Answers (2)

Maniac
Maniac

Reputation: 36

There are a few issues with your code, as was pointed out, but the reason it doesn't work as you expect is the line: if(succ = 0)

I changed it to: if(succ==0) And it worked as you described.

Additionally this line here: scanf("%s", &LU); should be scanf("%s", LU); The compiler is probably generating a warning about the format.

Upvotes: 0

Andrew Cottrell
Andrew Cottrell

Reputation: 3413

First off, you're allocating a lot of arrays in local variables in main(). That could lead to a stack overflow. You should probably use malloc() to allocate them instead.

Second, this line is an assignment, not a comparison, which is a bug:

if (succ = 0)

Change it to this, if you want it to be a comparison:

if (succ == 0)

Third, you're not initializing succ at the beginning of main() which is a bug.

If I see anything else suspect I'll update my answer. But start with fixing those issues.

Upvotes: 2

Related Questions