-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog.cpp
153 lines (115 loc) · 3.54 KB
/
prog.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include <iostream>
#include <map>
#include <limits>
using namespace std;
//Declare user's choice validation function.
int validateChoice();
class User {
//Declare variables and users map.
string username, password, firstname, lastname, city;
map<string, string> users = {};
public:
void login();
void signUp();
void seeUsers();
};
int main() {
User user;
int choice;
//As long as the user does not quit by pressing 4, keep going.
do {
//Get user's menu selection.
cout << "1. Login " << endl << "2. Sign Up " << endl << "3. See Users " << endl << "4. Quit " << endl;
cout << "CHOOSE FROM THE SELECTION MENU (enter a number to go): " << endl;
choice = validateChoice();
switch (choice) {
case 1:
user.login();
break;
case 2:
user.signUp();
break;
case 3:
user.seeUsers();
break;
case 4:
cout << "GoodBye!" << endl;
break;
default:
break;
}
} while (choice != 4);
return 0;
}
void User::login() {
//Get username and password.
cout << "Username (NO SPACES):";
cin >> username;
cout << "Password: ";
cin >> password;
//Check if the username exists in the map or not.
if (users.count(username) != 0) {
//If it exists, then compare the password that was entered.
if (users[username] == password) {
cout << "Logged In! " << endl;
}
else {
cout << "Wrong password! " << endl;
}
} else {
//If the username did not exist, prompt the user to sign up.
cout << "Sign up First! " << endl;
}
}
void User::signUp() {
//Get username and password.
cout << "First Name: ";
cin >> firstname;
cout << "Last Name: ";
cin >> lastname;
cout << "City: ";
cin >> city;
cout << "Username(NO SPACES): ";
cin >> username;
cout << "Password: ";
cin >> password;
//Check if that username is already taken or not.
if (users.count(username) == 0) {
//If the username was not taken, sign the user up.
users.insert({username, password});
cout << "Signed up! " << endl;
} else {
//If the username was taken, display appropriate message.
cout << "Username already taken " << endl;
}
}
//Display the usernames of all the signed up users.
void User::seeUsers() {
//Check to see if the map is empty.
if (!users.empty()) {
//If map is not empty, display the usernames of the users.
cout << "Usernames: " << endl;
for (auto it = users.cbegin(); it != users.cend(); it++) {
cout << it->first << endl;
}
} else {
//If map is empty, show the appropriate message.
cout << "There are no users to display! " << endl;
}
}
//Validate user's menu selection.
int validateChoice() {
int retVal;
//Input has to be numeric.
while (!(cin >> retVal)) {
cout << "Enter numeric values 1 - 4: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
//Input has to be between 1 and 4.
if (retVal < 1 || retVal > 4) {
cout << "Enter numeric values 1 - 4: ";
retVal = validateChoice();
}
return retVal;
}