0

Username and password system that stores the account info using the signin function and logs in using function login.For some reason the program reads the binary file in login but doesnt recognize if the given first name,lastname and password match any of the data in the file.

int userno = 0;
bool check;
char firstname[10],lastname[10],password[10];


class user
{
public:
int uid;
char fname[20],lname[20],pword[20];
int age;

}u[100];

getuserno()
{

fstream f("user.dat",ios::in);


    for(int i=0 ; !f.eof() ; i++)
    {
        f.read((char*)&u[i],sizeof(u[i]));
        userno = i;
    }


}

`

int signin()
{   
ofstream entry("user.dat",ios::out|ios::app|ios::binary);
if(!entry) {
  cout << "Cannot open file!" << endl;
  return 1;
}
cout<<"Enter first name\n";
cin>>u[userno].fname;
cout<<"Enter last name\n";
cin>>u[userno].lname;
cout<<"Enter your password\n";
cin>>u[userno].pword;
cout<<"Enter your age\n";
cin>>u[userno].age;
if(u[userno].age<18)
{
    cout<<"You must be 18 or older"<<endl;
    cin>>u[userno].age;
}
u[userno].uid = userno;
entry.write((char*)&u[userno],sizeof(u[userno]));
if(!entry.good()) {
  cout << "Error occurred at writing time!" << endl;
  system("pause");
}
userno++;
entry.close();
system("cls");
}

`

int login()
{   
x:check=false;

ifstream login("user.dat",ios::in|ios::binary);
if(!login) {
  cout << "Cannot open file!" << endl;
  return 1;
}
cout<<"Enter your first name\n";
cin>>firstname;
cout<<"Enter your last name:"<<endl;
cin>>lastname;
cout<<"Enter password\n";
cin>>password;
login.seekg(0);
for(int i = 0; i < userno ;i++)
{   
    login.read((char*)&u[i],sizeof(u[i]));
    if(firstname == u[i].fname && lastname == u[i].lname && 
password == u[i].pword)
    {
        check=true; 
        cout<<"IT WORKS!!!!";
    }
    cout<<check;
}
if(check == false)
{
    cout<<"Incorrect username or password!!!!\n";
    goto x;
    system("cls");
}
login.close();
system("cls");
}

The for loop reads through each record in the file but still outputs check as false(0) even if the correct info is put in.

1 Answers1

0

Your questions lacks a Minimal-Complete and Verifiable Example (aka mcve), thus it is impossible to say if your code has more problems, though one problem is:

for(int i = 0; i < userno ;i++)
{   
    login.read((char*)&u[i],sizeof(u[i]));
    if(firstname == u[i].fname && lastname == u[i].lname && 
password == u[i].pword)
    {
        check=true; 
        cout<<"IT WORKS!!!!";
    }
    cout<<check;
}

You loop through elements of u and in each iteration you check if the names and password match. After the loop, check will only tell you if the last entry was a match, but if the match was before, check will get overwritten with false on the next iteration.

You need to break the loop once you found the match:

for(int i = 0; i < userno ;i++)
{   
    login.read((char*)&u[i],sizeof(u[i]));
    if(firstname == u[i].fname && lastname == u[i].lname && 
password == u[i].pword)
    {
        check=true;             
        cout<<"IT WORKS!!!!";
        break;                 // exit the while loop 
    }
}
cout<<check;                   // move the print out of the loop

Moreover:

  • Don't use goto when you can use a more clear while loop
  • Read about why is using namespace std considered bad practice
  • unless you need it for later, there is no need to store all entries from the file in an array. Inside the loop you only ever need a single entry.
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • Using break didnt work but I did changed the goto and array parts of the program so thanks for the tip. – Ayushmaan Kaushik Aug 30 '19 at 15:47
  • @AyushmaanKaushik without seeing the contents of the file and the input you provide and a complete example it is impossible to say what else is wrong. Without the `break` the logic is clearly broken – 463035818_is_not_an_ai Aug 30 '19 at 17:53