Question: please help me with my logic... so I am making a user login . . . and it works... however... lets say i login and

please help me with my logic... so I am making a user login... and it works... however... lets say i login and then close the program and a new user tries to login and they dont exist in the .txt file, the program just kind of bypasses the fact they have to register and logs in the last person who was logged in...
i want some kind of logic that only logs in the user IF they exist... and if they dont then they have to register before they continue, but i also want if they do login for it to continue to the body; my mind just cant wrap around the logic
#include
#include
#include
#include
using namespace std;
class User
{
protected:
char username[50];
char password[50];
public:
const char* getName() const
{
return username;
}
void Register()
{
ofstream fs("login.txt", ios::app);
cout <<" Enter username: ";
cin >> username;
cout <<" Enter password: ";
cin >> password;
fs << endl << username <<""<< password;
cout << "User registered successfully" << endl << endl;
fs.close();
}
void login(char un[], char pw[])
{
int k =0;
ifstream fs("login.txt");
while (!fs.eof())
{
fs >> username >> password;
if (strcmp(un, this->username)==0 && strcmp(pw, this->password)==0)
{
cout <<" Login Sucessful" << endl << endl;
k =0;
}
else
k =1;
}
if (k ==1)
cout <<" User does not exist. Please Register.
";
fs.close();
}
};
#include "User.cpp"
int main(){
User l1;
bool loggedIn = false;
while (!loggedIn)
{
char username[50], password[50];
int ch;
cout <<"******** Welcome to SMS *********";
cout <<"
1. Login
2. Create an Account" << endl << endl;
cout <<" Enter choice: ";
cin >> ch;
cout << endl;
switch (ch)
{
case 1:
cout <<" Username : "; cin >> username;
cout <<" Password : "; cin >> password;
l1.login(username, password);
loggedIn = true;
break;
case 2:
l1.Register();
loggedIn = false;
break;
default:
std::cout << "Invalid choice. Please try again.
";
}
}
//body of the program
cout << "Welcome "<< l1.getName()<<". You are now logged in. Continue with the program...
";
}
I think it has something to do in main, but it could be in the login function... please try the program out and you will see what I mean with the logic

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Accounting Questions!