I have to make a program that handles user register and login using text file as the place to store the user's data.
I finished the register and login function and the register is working just fine but the login function only works when I register in the same run , if I run it one more time and doesn't register and login with the data already in the text file , it shows the output of the username or password being incorrect.
There's also admin and status thing that is also needed but it's working fine.
Here's the register and login code :
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>
#include <string>
using namespace std;
bool administrator = false;
bool login = false;
string username;
char admin;
string password;
char status;
string inputusername;
string inputpassword;
void createAcc()
{
cout << "\n\n";
cout << "=================================" << endl;
cout << setw(25) << "USER REGISTRATION" << endl;
cout << "=================================" << endl;
cout << "Enter Username :" << endl;
cin >> username;
cout << "Enter User Type : " << endl << "Administrator = [a] Buyer = [b]" << endl << endl;
cin >> admin;
while(admin != 'a' && admin != 'b')
{
cout << "Enter a valid choice : " ;
cin >> admin;
}
cout << "Enter Password : " << endl;
cin >> password;
status = 'a';
ofstream userData("userdata.txt", ios::out | ios::app);
if (!userData.is_open())
{
userData.open("userdata.txt");
}
userData << username << " " << admin << " " << password << " " << status << endl;
userData.close();
cout << endl << "Successfully Registered!" << endl;
}
void loginAcc()
{
cout << "\n\n";
cout << "=================================" << endl;
cout << setw(25) << "USER LOGIN" << endl;
cout << "=================================" << endl;
cout << "Enter Username : " << endl;
cin >> inputusername;
cout << "Enter Password : " << endl;
cin >> inputpassword;
string userPass = inputusername + " " + admin + " " + inputpassword + " " + status;
int offset;
string line;
ifstream userData;
userData.open("userdata.txt");
bool found = false;
if(userData.is_open())
{
while(getline(userData,line) && !found)
{
if (line.compare(userPass) == 0)
{
found = true;
}
}
userData.close();
if(found){
login = true;
cout << "Successfully Logged In!" << endl;
}
else if(status == 'b')
{
cout << endl << "User deleted by admin" << endl;
}
else{
cout << endl << "Username or Password is Incorrect" << endl;
}
}
if (admin == 'a'){
administrator = true;
}
}
And the data in the text file is still there and nothing changed.
I'm having trouble debugging this and would appreciate any help.