Thread: Trouble with strcmp

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Trouble with strcmp

    I'm not sure what the problem is but strcmp in line 114 returns -10 even if the strings are the same.

    Code:
    ////  main.c
    //  projectpt1
    //
    //  Created by Justin Howell on 11/17/11.
    //  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
    //
    
    
    //Preprocessor Directives
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #define buffer_size 512
    
    
    
    
    typedef struct {
        char * first[20];
        char * last[20];
        char * id[20];
        char * password[26];
        char * admin[4];
    }user;
    
    
    int displayMenu(user * userBase);
    int adminLogin(user * userBase);
    void adminMenu(user * userBase);
    void userLogin(user * userBase);
    void userCreate(user * userBase);
    
    
    
    
    
    
    
    
    
    
    int main (int argc, const char * argv[])
    {
        user userBase[15];
        int i = 0;
        strcpy(userBase[0].first, "Justin");
        strcpy(userBase[0].last, "Howell");
        strcpy(userBase[0].id, "ho");
        strcpy(userBase[0].password, "ha");
        strcpy(userBase[0].admin, "yes");
        while (i != 1){
            i = (displayMenu(&userBase[0]));
        }
    
    
        return 0;
    }
    
    
    
    
    int displayMenu(user * userBase) {
        char selection;
    	char buffer[buffer_size];
        int j = 0;
        printf("\nWelcome to the SYSTEM\n\n");
        printf("---------------------\n");
        printf("Please select an option\n");
        printf("1. Admin Login\n");
        printf("2. User Login\n");
        printf("3. User Creation\n");
        printf("4. Exit\n\n");
        printf("Selection: ");
    	fgets(buffer, buffer_size, stdin);
        selection = atoi(buffer);
        
        switch (selection) {
            case 1:
                while(j <= 2){
                    j += (adminLogin(userBase));
                    
                }
                if (j == 3) {
                    printf("\nTOO MANY INVALID ATTEMPTS!\n\n");
                    printf("Press any key to continue");
                    getchar();
                }
                selection = NULL;
                break;
                        case 2:
                            userLogin(userBase);
                            break;
                        case 3:
                            userCreate(userBase);
                            break;
                
            case 4:
                printf("Have a nice day\nEOL");
                return 1;
            default:
                printf("\nINVALID OPTION!\n\n");
                return 0;
                
        }
    }
    
    
    int adminLogin(user * userBase){
        char buffer[buffer_size];
        char userid[20];
        char pass[20];
        int i = 0;
        int j = 0;
        printf("********Admin Login********\n");
        printf("ID: ");
        fgets(buffer, buffer_size, stdin);
        strcpy(userid, buffer);
        printf("\nPassword: ");
        fgets(buffer, buffer_size, stdin);
        strcpy(pass, buffer);
        printf("%s", userBase->id);
        printf("%s", userid);
        printf("%d", strcmp((userBase->id), userid));
        
        while (i <= 15) {
            if((strcmp(userBase->id, userid)) == 0){
                if ((strcmp(*userBase->admin, "yes") == 0)) {
                    printf("User Authenticated!\n");                return 4;
                }else {
                    printf("You do not have elevated Privilages");
                }
            }else{
                j++;
                i++;
                *userBase++;
            }
        }
        return 1;
    }
    
    
    void userLogin(user * userBase){
        printf("\nOption not yet implemented!\n\n");
        printf("Press any key to continue");
        getchar();
    }
    void userCreate(user * userBase){
        printf("Option not yet implemented!\n\n");
        printf("Press any key to continue");
        getchar();
    }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I think you are trying a little bit of running before walking here, what compiler are you using? Make sure you enable .. or take notice of the warnings you .. should ... be getting, There are some very good C programmers on these pages that I am sure will help you out on this one, you have taken time to make your output menu nice in the console, but it is better to spend the time on the background functionality first, in my humble opinion... is that what INMHO means? anyway, good luck.
    Last edited by rogster001; 11-24-2011 at 06:23 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    I've tried LLVM GCC 4.2 and Apple LLVM 3.0. I'm running xcode on mac os x 10.7.2

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    so what are the warnings you see?

    C:\Documents and Settings\Administrator\My Documents\testC\main.c||In function 'main':|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|46|warning: passing argument 1 of 'strcpy' from incompatible pointer type|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4. 1\..\..\..\..\include\string.h|45|note: expected 'char *' but argument is of type 'char **'|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|47|warning: passing argument 1 of 'strcpy' from incompatible pointer type|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4. 1\..\..\..\..\include\string.h|45|note: expected 'char *' but argument is of type 'char **'|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|48|warning: passing argument 1 of 'strcpy' from incompatible pointer type|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4. 1\..\..\..\..\include\string.h|45|note: expected 'char *' but argument is of type 'char **'|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|49|warning: passing argument 1 of 'strcpy' from incompatible pointer type|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4. 1\..\..\..\..\include\string.h|45|note: expected 'char *' but argument is of type 'char **'|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|50|warning: passing argument 1 of 'strcpy' from incompatible pointer type|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4. 1\..\..\..\..\include\string.h|45|note: expected 'char *' but argument is of type 'char **'|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c||In function 'displayMenu':|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|88|warning: assignment makes integer from pointer without a cast|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c||In function 'adminLogin':|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|121|warning: format '%s' expects type 'char *', but argument 2 has type 'char **'|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|123|warning: passing argument 1 of 'strcmp' from incompatible pointer type|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4. 1\..\..\..\..\include\string.h|43|note: expected 'const char *' but argument is of type 'char **'|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|126|warning: passing argument 1 of 'strcmp' from incompatible pointer type|
    c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4. 1\..\..\..\..\include\string.h|43|note: expected 'const char *' but argument is of type 'char **'|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|135|warning: value computed is not used|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c||In function 'displayMenu':|
    C:\Documents and Settings\Administrator\My Documents\testC\main.c|105|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 11 warnings ===|
    Last edited by rogster001; 11-24-2011 at 06:51 PM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should redefine the elements in your type user with as array of char array and not array of char pointers.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #define buffer_size 512
    
    
    
    
    typedef struct {
        char first[20][buffer_size];
        char last[20][buffer_size];
        char id[20][buffer_size];
        char password[26][buffer_size];
        char admin[4][buffer_size];
    }user;
    
    
    int displayMenu(user * userBase);
    int adminLogin(user * userBase);
    void adminMenu(user * userBase);
    void userLogin(user * userBase);
    void userCreate(user * userBase);
    
    
    
    
    
    
    
    
    
    
    int main (int argc, const char * argv[])
    {
        user userBase[15];
        int i = 0;
        strcpy(userBase[0].first[0], "Justin");
        strcpy(userBase[0].last[0], "Howell");
        strcpy(userBase[0].id[0], "ho");
        strcpy(userBase[0].password[0], "ha");
        strcpy(userBase[0].admin[0], "yes");
        while (i != 1){
            i = (displayMenu(&userBase[0]));
        }
    
    
        return 0;
    }
    
    
    
    
    int displayMenu(user * userBase) {
        char selection;
        char buffer[buffer_size];
        int j = 0;
        printf("\nWelcome to the SYSTEM\n\n");
        printf("---------------------\n");
        printf("Please select an option\n");
        printf("1. Admin Login\n");
        printf("2. User Login\n");
        printf("3. User Creation\n");
        printf("4. Exit\n\n");
        printf("Selection: ");
        fgets(buffer, buffer_size, stdin);
        selection = atoi(buffer);
    
        switch (selection) {
            case 1:
                while(j <= 2){
                    j += (adminLogin(userBase));
    
                }
                if (j == 3) {
                    printf("\nTOO MANY INVALID ATTEMPTS!\n\n");
                    printf("Press any key to continue");
                    getchar();
                }
                break;
                        case 2:
                            userLogin(userBase);
                            break;
                        case 3:
                            userCreate(userBase);
                            break;
    
            case 4:
                printf("Have a nice day\nEOL");
                return 1;
            default:
                printf("\nINVALID OPTION!\n\n");
    
    
        }
                return 0;
    }
    
    
    int adminLogin(user * userBase){
        char buffer[buffer_size];
        char userid[20];
        char pass[20];
        int i = 0;
        int j = 0;
        printf("********Admin Login********\n");
        printf("ID: ");
        fgets(buffer, buffer_size, stdin); if(buffer[strlen(buffer)-1]=='\n')buffer[strlen(buffer)-1]=0;
        strcpy(userid, buffer);
        printf("\nPassword: ");
        fgets(buffer, buffer_size, stdin); if(buffer[strlen(buffer)-1]=='\n')buffer[strlen(buffer)-1]=0;
        strcpy(pass, buffer);
        printf("%s", userBase->id[0]);
        printf("%s", userid);
        printf("%d", strcmp((userBase->id[0]), userid));
    
        while (i <= 15) {
            if((strcmp(userBase->id[0], userid)) == 0){
                if ((strcmp(*userBase->admin, "yes") == 0)) {
                    printf("User Authenticated!\n");                return 4;
                }else {
                    printf("You do not have elevated Privilages");
                }
            }else{
                j++;
                i++;
            }
        }
        return 1;
    }
    
    
    void userLogin(user * userBase){
        printf("\nOption not yet implemented!\n\n");
        printf("Press any key to continue");
        getchar();
    }
    void userCreate(user * userBase){
        printf("Option not yet implemented!\n\n");
        printf("Press any key to continue");
        getchar();
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp
    By Soulzityr in forum C Programming
    Replies: 9
    Last Post: 03-23-2010, 04:36 PM
  2. Help: strcmp
    By LuizCPFL in forum C Programming
    Replies: 5
    Last Post: 12-11-2008, 03:09 AM
  3. trouble w/ strcmp
    By lord in forum C++ Programming
    Replies: 2
    Last Post: 03-30-2008, 12:18 PM
  4. strcmp();
    By xddxogm3 in forum C Programming
    Replies: 20
    Last Post: 05-03-2004, 01:10 PM
  5. strcmp
    By stanleyw in forum C++ Programming
    Replies: 2
    Last Post: 06-03-2002, 06:36 PM