Thread: strcmp is always returning value == 0 :( help me (CLASSES & OBJECTS)

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

    strcmp is always returning value == 0 :( help me (CLASSES & OBJECTS)

    In this program i m creating a login system

    but when i compare two variables(password) i.e member char array and password_chck that i have declared in a function, it is always returning value 0. plz help me..

    Line no 110-116

    Code:
    #include <iostream>
    #include <conio.h>
    #include <string.h>
    
    using namespace std;
    
    class Account
    {
    
    
        char password[25];
    public:
        string uname;
        void pswd_in(char pw[25]);
        void create();
    };
    
    class Login
    {
    public:
        char password_chck[25], password_login[25];
        string uname_login;
    
    
    };
    
    
    int main()
    {
        char choice;
        int i=0;
        Account regist[10];
        Login login[10];
    
        do
        {
            cout << "Enter your Choice:\n1 to create account\n2 to login into your account\n3 to exit\n";
            cin >> choice;
    
            if ( choice == '1')
            {
                regist[i].create();
                i++;
            }
    
        }
        while( choice != '3');
    
        return 0;
    }
    
    void Account::pswd_in(char pw[25])
    {
    
        char  c ;
        int i=0,j=0;
        while(j==0)
        {
            while((c=getch())!= '\r')
            {
                if (c == 8 )
                {
                    i--;
                    cout << "\b \b";
                }
                else
                {
                    pw[i]=c;
    
                    putch('*');
                    i++;
                }
            }
            if(i > 24 || i < 3)
            {
                cout << "\nYour Password should contain minimum 3 charachters and maximum 25 charachters.\nYour typed password is " << i << " charachters long\nType your password again\n";
                i=0;
            }
            else
                j=1;
        }
        pw[i]='\0';
        i--;
        password[i+1]='\0';
        while (i >= 0)
        {
    
            password[i]=pw[i];
            i--;
        }
        cout << endl;
    
    }
    
    void Account::create()
    {
    char password_chck[25];
        cout << "\n**Create Your Account**\nEnter your username\n";
        cin.ignore(1000,'\n');
        getline(cin,uname);
        do
        {
            cout << "Enter your Password\n";
            pswd_in(password);
             cout << password;
            cout << "\nPlease type it again for confirmation\n";
            pswd_in(password_chck);
            cout << password_chck;
    
            cout << endl << strcmp(password_chck,password);
            if((strcmp(password_chck,password)) != 0)
            {
                cout << "Password does not match\n";
            }
        }
        while(strcmp(password_chck,password));
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It's because you're writing directly to password in your pswd_in routine:
    Code:
        password[i+1]='\0';
        while (i >= 0)
        {
     
            password[i]=pw[i];
            i--;
        }
    So password is set to password_chck in this call:
    Code:
    pswd_in(password_chck);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    That's because you're copying the function's argument into the member variable within the pswd_in function. On a side note, you really shouldn't be using fixed-size char arrays at all; std::strings are much safer, not to mention much easier to use...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. More returning STL Objects
    By uderman in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2010, 10:36 PM
  2. returning STL objects
    By MK27 in forum C++ Programming
    Replies: 4
    Last Post: 03-11-2010, 11:20 AM
  3. strcmp() not returning 0 when its suppose to
    By TaiL in forum C Programming
    Replies: 22
    Last Post: 12-11-2009, 08:32 PM
  4. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  5. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM