Hello, I am trying to create a Login system, so far it writes users correctly however it doesn't seem to be reading it correctly as it always claims the username/password is incorrect even though when I check the file it is.
Could someone help, thanks!
class TestLogin
{
public:
char name[16];
char pword[10];
void sendname();
void sendpassword();
};
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void TestLogin::sendname()
{
cout << "Enter your desired username(max 16 characters): ";
cin >> name;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
};
void TestLogin::sendpassword()
{
cout << "Enter your password(max 10 characters): ";
cin >> pword;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
};
int ListMenu()
{
int option;
cout << "Welcome to the login menu..." << endl;
cout << "\tPress '1' to login." << endl;
cout << "\tPress '2' to register." << endl;
cout << "Your option: ";
cin >> option;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return option;
};
int main()
{
ifstream readFile ("list.txt");
ofstream writeFile ("list.txt", ios::app);
TestLogin charInfo;
do
{
int option = ListMenu();
cout << endl;
if(option == 1)
{
string username;
string password;
string temp;
string temp1;
if(readFile.is_open())
{
cout << "Enter Name: ";
cin >> username;
cout << "Enter Password: ";
cin >> password;
cout << endl;
readFile >> temp;
cout << "scanning for username...";
if(username == temp)
{
cout << "\nusername found, checking password" << endl;
readFile>>temp1;
if(password == temp1)
{
cout << "Welcome " << username << "!" << endl;
break;
}
}
else
{
cout << "Invalid username or password." << endl;
}
readFile.close();
}
}
cout << endl;
if(option == 2)
{
charInfo.sendname();
cout << endl;
charInfo.sendpassword();
cout << endl;
if (writeFile.is_open())
{
writeFile << charInfo.name << endl;
writeFile << charInfo.pword << endl;
writeFile.close();
}
else
{
cout << "Unable to open file";
}
}
}while(1);
return 0;
}