Login system in C++ not working

504 views Asked by At

I'm trying to debug my c++ login system that does not seem to be working (i.e. it says wrong user ID and password even on entering the right user ID and password).
Registering stores the login details in a binary file while login reads that file (ID.dat) and checks for a match.
This is my high school project and I really need to fix this on time. I've been trying to do that for a really, really long time. Please point out the possible errors. Thank you for your help.

Edit: I've added the new functions but it doesn't seem to work

//Class for storing IDs
char Level;
class ID
{
    public:
    char username[20],pass1[20],pass2[20],IDtype,systemID[5];
    int checkuser(char u[20])
    {
        return(strncmp(u,username,strlen(u)));
    }
    int checkpass(char p[20])
    {
        return(strncmp(p,pass1,strlen(p)));
    }
};

//Registration function
void IDregister()
{
    clrscr();
    ID i;
    ofstream fout;  
    fout.open("ID.dat",ios::app);
    gotoxy(30,6);
    cout<<"*--- Register ---*";
    gotoxy(25,9);
    cout<<" Enter User ID - ";
    fgets(i.username,21,stdin);
    gotoxy(25,11);
    cout<<" Enter Password - ";
    fgets(i.pass1,21,stdin);
    do
    {
        cout<<"\n           Re-enter Password - ";
        fgets(i.pass2,21,stdin);
    }while(strncmp(i.pass1,i.pass2,strlen(i.pass1))!=0);
    cout<<"\n       To register as Admin, type system ID below.\n            Else press Enter ";
    fgets(i.systemID,6,stdin);
    if(strncmp(i.systemID,"#123#",5)==0)
        i.IDtype='A';
    else
        i.IDtype='U';
    fout.write((char*)&i,sizeof(i));
    cout<<i.IDtype;
    getch();
    fout.close();
    IDlogin();
}

//Log-in Function
void IDlogin()
{
    clrscr();
    int flag=1;
    ID acc;
    char usercheck[20],passcheck[20];
    ifstream fin;
    fin.open("ID.dat",ios::in);
    gotoxy(30,7);
    cout<<"*--- Log-in ---*";
    gotoxy(25,10);
    cout<<"Enter User ID - ";
    fgets(usercheck,21,stdin);
    gotoxy(25,12);
    cout<<"Enter Password - ";
    fgets(passcheck,21,stdin);
    gotoxy(25,14);
    while(!fin.eof())
    {
        fin.read((char*)&acc,sizeof(acc));
        if(acc.checkuser(usercheck)==0&&acc.checkpass(passcheck)==0)
            flag=0;
    }
    if(flag==0)
    {
        cout<<"Logged in successfully!";
        Level=acc.IDtype;
        getch();
        mainscreen();
    }
    else
    {
        cout<<"Wrong User ID or Password";
        getch();
        initscreen();
    }
}
1

There are 1 answers

3
Hawk On

Try using instead of strcmp, the function strncmp. This will ensure that you are comparing at max n characters and in case there is any junk ahead in the character array, it will not be compared. Documentation of strncmp.

In addition to that, using gets is unsafe, consider using fgets instead.

 int checkuser(char u[20])
    {
        return(strncmp(u,username, strlen(username)));
    }
    int checkpass(char p[20])
    {
        return(strncmp(p,pass1, strlen(pass));
    }