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.