-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetro.cpp
186 lines (168 loc) · 6.16 KB
/
metro.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <iostream>
#include "./card_operations.h"
#include "graph_operations.h"
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
void userLogin(string username) {
int choice;
while (true) {
cout<<"Welcome to the metro path finder"<<endl;
cout << "\n***** Metro Rail Route Finder *****\n";
cout << "1. Find Shortest Path\n";
cout << "2. Find Most Economical Path\n";
cout << "3. Recharge Metro Card\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 0) {
cout << "Logging out. Goodbye!" << endl;
break;
}
switch (choice)
{
case 1:
loadGraph1();
break;
// case 2:
// loadGraph();
// break;
case 2:
loadGraph2();
break;
case 3:
cout << "";
recharge(username);
break;
default:
cout << "Invalid choice! Please enter a valid option." << endl;
break;
}
}
}
int main() {
int choice;
//beginning of menu driven options
while (true) {
cout << "1. Login " << endl;
cout << "2. Sign up" << endl;
cout<< "3. Exit" << endl;
cout << "Enter your choice (1-3): ";
cin >> choice;
if (choice == 3) {
cout << "Exiting the program. Goodbye!" << endl;
break;
}
switch (choice) {
case 1: {
string username, password, info;
cout << "Enter your username: ";
cin >> username;
cout << "Enter your password: ";
cin >> password;
//concatinating all the user info for easy comparison
info= username + " " + password;
//extraction of vectors inside the file
ifstream inputFile("./usernames.txt");
if (!inputFile.is_open()) {
std::cerr << "Error opening the file." << std::endl;
return 1;
}
vector<std::string> usernames;
string line;
while (std::getline(inputFile, line)) {
usernames.push_back(line);
}
inputFile.close();
//implementig binary search to find info of the user in the vector
int left = 0;
int right = usernames.size() - 1;
bool found = false;
while (left <= right) {
int mid = (left + right) / 2;
if (usernames[mid] == info) {
found = true;
break;
} else if (usernames[mid] < info) {
left = mid + 1;
} else {
right = mid - 1;
}
}
// POV: found yaaaayyyy
if (found) {
std::cout << "Login successful!" << std::endl;
cout<<"Welcome "<<username<<"!"<<endl;
cout<<"Press 0 to logout at any point" << endl;
userLogin(username);
} else {
std::cout << "Invalid username or password." << std::endl;
}
//closing file after use
inputFile.close();
break;
}
case 2: {
string newUsername, newPassword;
cout << "Enter new username: ";
cin >> newUsername;
cout << "Enter new password: ";
cin >> newPassword;
//concatinating all the user info for easy insertion and comparison
string info= newUsername + " " + newPassword;
ifstream inputFile("./usernames.txt");
if (!inputFile.is_open()) {
std::cerr << "Error opening the file." << std::endl;
return 1;
}
//extracting all the users info from the file
vector<std::string> usernames;
string line;
while (std::getline(inputFile, line)) {
usernames.push_back(line);
}
inputFile.close();
// Find the insertion point for the new user info using binary search
int left = 0;
int right = usernames.size() - 1;
int insertionPoint = -1;
while (left <= right) {
int mid = (left + right) / 2;
if (usernames[mid] == info) {
std::cout << "Username already exists." << std::endl;
return 0;
} else if (usernames[mid] < info) {
left = mid + 1;
insertionPoint = mid + 1;
} else {
right = mid - 1;
insertionPoint = mid;
}
}
// Insert the new user info at the correct position in the vector
if (insertionPoint == -1) {
usernames.insert(usernames.begin(), info);
} else {
usernames.insert(usernames.begin() + insertionPoint, info);
}
// Write the sorted list of usernames into the file
ofstream outputFile("./usernames.txt");
if (!outputFile.is_open()) {
std::cerr << "Error opening the file for writing." << std::endl;
return 1;
}
for (const auto& username : usernames) {
outputFile << username << std::endl;
}
outputFile.close();
std::cout << "New user added: " << newUsername << std::endl;
break;
}
default:
cout << "Invalid choice. Please enter a valid option (1-3)." << endl;
continue;
}
}
return 0;
}