-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrough.cpp
More file actions
109 lines (100 loc) · 2.68 KB
/
rough.cpp
File metadata and controls
109 lines (100 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class User {
protected:
string username, password;
public:
virtual void registerUser() = 0;
virtual bool loginUser() = 0;
};
class Student : public User {
public:
void registerUser() override {
cout << "\nRegister as Student";
cout << "\nEnter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
ofstream file("student_data.txt", ios::app);
file << username << " " << password << endl;
file.close();
cout << "Registration successful!\n";
}
bool loginUser() override {
string user, pass;
cout << "\nLogin as Student";
cout << "\nEnter username: ";
cin >> user;
cout << "Enter password: ";
cin >> pass;
ifstream file("student_data.txt");
string u, p;
while (file >> u >> p) {
if (u == user && p == pass) {
cout << "Login successful!\n";
return true;
}
}
cout << "Invalid credentials!\n";
return false;
}
};
class Employee : public User {
public:
void registerUser() override {
cout << "\nRegister as Employee";
cout << "\nEnter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
ofstream file("employee_data.txt", ios::app);
file << username << " " << password << endl;
file.close();
cout << "Registration successful!\n";
}
bool loginUser() override {
string user, pass;
cout << "\nLogin as Employee";
cout << "\nEnter username: ";
cin >> user;
cout << "Enter password: ";
cin >> pass;
ifstream file("employee_data.txt");
string u, p;
while (file >> u >> p) {
if (u == user && p == pass) {
cout << "Login successful!\n";
return true;
}
}
cout << "Invalid credentials!\n";
return false;
}
};
int main() {
int choice, action;
cout << "Welcome!\nSelect user type:";
cout << "\n1. Student\n2. Administrative Employee\nChoice: ";
cin >> choice;
User* user = nullptr;
if (choice == 1)
user = new Student();
else if (choice == 2)
user = new Employee();
else {
cout << "Invalid choice!";
return 0;
}
cout << "\n1. Register\n2. Login\nChoice: ";
cin >> action;
if (action == 1)
user->registerUser();
else if (action == 2)
user->loginUser();
else
cout << "Invalid action!";
delete user;
return 0;
}