I have a few sets of username and password in the file but when I try to login, the program could only read the first set of username and password. The rest will be invalid input eventhough I key in correctly.
#ifndef USERLOG_H
#define USERLOG_H
class UserLog
{
public:
char newUserName[25];
char userName[25];
char newPassword[16];
char password[16];
void createUser();
void checkUser();
};
#endif
#include<iostream>
#include<fstream>
#include<string>
#include "userLog.h"
using namespace std;
string temp;
string temp1;
ofstream editUserLog("User Log.txt", ios::app);
ifstream readUserLog("User Log.txt");
void UserLog::createUser()
{
if(editUserLog.is_open())
{
cout << "Enter desired username: ";
cin >> newUserName;
editUserLog << newUserName << endl;
cout << "\nEnter desired password: ";
cin >> newPassword;
editUserLog << newPassword << endl;
editUserLog.close();
}
}
void UserLog::checkUser()
{
if(readUserLog.is_open())
{
cout << "Enter your username: ";
cin >> userName;
cout << "\nEnter your password: ";
cin >> password;
readUserLog >> temp;
readUserLog >> temp1;
if(userName == temp && password == temp1)
{
cout << "\nWelcome " << userName << endl;
}
else
cout << "\nInvalid username or password." << endl;
readUserLog.close();
}
}
#include<iostream>
#include<fstream>
#include "userLog.h"
using namespace std;
int main()
{
UserLog ul;
char choice;
while(1)
{
cout << "Welcome to the Student Grade Management System" << endl;
cout << "What would you like to do?\n"
<< "[C] Create User\n"
<< "[L] Login\n"
<< "[Q] Quit\n";
cin >> choice;
if(choice == 'c' || choice == 'C')
{
ul.createUser();
}
else if(choice =='l' || choice == 'L')
{
ul.checkUser();
}
else if(choice == 'q' || choice == 'Q')
{
break;
}
else
{
cout << "Invalid Input" << endl;
continue;
}
}
cout << "Have a nice day" << endl;
system("PAUSE");
return 0;
}