|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <string.h> |
| 4 | +#include <vector> |
| 5 | +#include <functional> |
| 6 | +#include <cmath> |
| 7 | +#include <cstdlib> |
| 8 | +#include <ctime> |
| 9 | +#include "login.h" |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +void login::Login() |
| 13 | +{ |
| 14 | + string count; |
| 15 | + string username, password, id, recordPass, recordSecurity; |
| 16 | + system("cls"); |
| 17 | + cout << "\n\t\t\t Please enter the username and password: " << endl; |
| 18 | + cout << "\t\t\t USERNAME: "; |
| 19 | + cin >> username; |
| 20 | + cout << "\t\t\t PASSWORD: "; |
| 21 | + cin >> password; |
| 22 | + |
| 23 | + string loginHash = password; |
| 24 | + hash<string> mystdhash; |
| 25 | + int loginHashPassword = mystdhash(loginHash); |
| 26 | + |
| 27 | + ifstream input("data.txt"); |
| 28 | + |
| 29 | + while (input >> id >> recordPass >> recordSecurity) |
| 30 | + { |
| 31 | + if (id == username && stoi(recordPass) == loginHashPassword) |
| 32 | + { |
| 33 | + count = "1"; |
| 34 | + system("cls"); |
| 35 | + } |
| 36 | + } |
| 37 | + input.close(); |
| 38 | + if (count == "1") |
| 39 | + { |
| 40 | + cout << username << "\nLogin successful!\n"; |
| 41 | + string choice = "1"; |
| 42 | + while (choice != "2") |
| 43 | + { |
| 44 | + cout << "\t\t\t_____________________________________________\n\n\n"; |
| 45 | + cout << "\t\t\t Welcome to the CS 1021 Login! \n\n"; |
| 46 | + cout << "\t\t\t_______ Currently Logged In: " << username << " ________\n\n"; |
| 47 | + cout << "\t\t\t_________ Menu __________\n\n"; |
| 48 | + cout << "\t | Press 1 to PLAY GAME |" << endl; |
| 49 | + cout << "\t | Press 2 to LOGOUT |" << endl; |
| 50 | + cout << "\n\t\t\t Please Enter your choice: "; |
| 51 | + cin >> choice; |
| 52 | + cout << endl; |
| 53 | + |
| 54 | + if (choice == "1") |
| 55 | + { |
| 56 | + system("cls"); |
| 57 | + DrunkGame(); |
| 58 | + } |
| 59 | + else if (choice == "2") |
| 60 | + { |
| 61 | + system("cls"); |
| 62 | + cout << "Logging out" << endl; |
| 63 | + } |
| 64 | + else |
| 65 | + { |
| 66 | + system("cls"); |
| 67 | + cout << "Choice invalid, try again"; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + else |
| 72 | + { |
| 73 | + system("cls"); |
| 74 | + cout << "\n Username or password is incorrect, please try again or register\n"; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +void login::Registration() |
| 79 | +{ |
| 80 | + string regUser, regPassword, regId, regPass, securityQuestion, regSecure, regCount; |
| 81 | + system("cls"); |
| 82 | + cout << "\n\t\t\t Enter Username: "; |
| 83 | + cin >> regUser; |
| 84 | + cout << "\t\t\t Enter Password: "; |
| 85 | + cin >> regPassword; |
| 86 | + cout << "\t\t\t Security Question: What was your favorite childhood movie?: "; |
| 87 | + cin.ignore(); |
| 88 | + getline(cin, securityQuestion); |
| 89 | + |
| 90 | + string hashing = regPassword; |
| 91 | + hash<string> mystdhash; |
| 92 | + int hashPassword = mystdhash(hashing); |
| 93 | + |
| 94 | + string secureHashing = securityQuestion; |
| 95 | + hash<string> mystdhash2; |
| 96 | + int securityHash = mystdhash2(secureHashing); |
| 97 | + |
| 98 | + ifstream input("data.txt"); |
| 99 | + input.seekg(0, ios::end); |
| 100 | + |
| 101 | + if (input.tellg() == 0) |
| 102 | + { |
| 103 | + ofstream f1("data.txt", ios::app); |
| 104 | + f1 << regUser << ' ' << hashPassword << ' ' << securityHash << endl; |
| 105 | + system("cls"); |
| 106 | + cout << "\n\t\t\t Registration successful!\n"; |
| 107 | + return; |
| 108 | + } |
| 109 | + else |
| 110 | + { |
| 111 | + ifstream input("data.txt"); |
| 112 | + while (input >> regId >> regPass >> regSecure) |
| 113 | + { |
| 114 | + if (regUser == regId) |
| 115 | + { |
| 116 | + string decision; |
| 117 | + cout << "\n\t\tUsername already taken.\n"; |
| 118 | + cout << "\t\tEnter 1 to enter a new one\n"; |
| 119 | + cout << "\t\tEnter 2 to go back to the menu\n"; |
| 120 | + cout << "\n\t\tEnter choice: "; |
| 121 | + cin >> decision; |
| 122 | + |
| 123 | + if (decision == "1") |
| 124 | + { |
| 125 | + Registration(); |
| 126 | + } |
| 127 | + else if (decision == "2") |
| 128 | + { |
| 129 | + system("cls"); |
| 130 | + cout << "\tReturning to menu\n"; |
| 131 | + return; |
| 132 | + } |
| 133 | + else |
| 134 | + { |
| 135 | + system("cls"); |
| 136 | + cout << "\tInvalid Entry, returning to menu." << endl; |
| 137 | + return; |
| 138 | + } |
| 139 | + } |
| 140 | + else |
| 141 | + { |
| 142 | + regCount = "1"; |
| 143 | + } |
| 144 | + } |
| 145 | + if (regCount == "1") |
| 146 | + { |
| 147 | + ofstream f1("data.txt", ios::app); |
| 148 | + f1 << regUser << ' ' << hashPassword << ' ' << securityHash << endl; |
| 149 | + system("cls"); |
| 150 | + cout << "\n\t\t\t Registration successful!\n"; |
| 151 | + return; |
| 152 | + } |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +void login::DrunkGame() |
| 157 | +{ |
| 158 | + srand(time(0)); |
| 159 | + const int size = 60; |
| 160 | + cout << "Enter a letter to begin \n "; |
| 161 | + char x; |
| 162 | + cin >> x; |
| 163 | + int position = size / 2; |
| 164 | + while (true) |
| 165 | + { |
| 166 | + cout << "|START|"; |
| 167 | + for (int i = 0; i < size; i++) |
| 168 | + { |
| 169 | + if (i == position) |
| 170 | + cout << x; |
| 171 | + else |
| 172 | + cout << " "; |
| 173 | + } |
| 174 | + cout << "|END|" << endl; |
| 175 | + int move = rand() % 3 - 1; |
| 176 | + position = position + move; |
| 177 | + if (position < 1) |
| 178 | + { |
| 179 | + cout << "Guess you were too drunk to make it to the end..." << endl; |
| 180 | + break; |
| 181 | + } |
| 182 | + if (position > size - 1) |
| 183 | + { |
| 184 | + cout << "You might be drunk, but you made it to the end!" << endl; |
| 185 | + break; |
| 186 | + } |
| 187 | + for (int sleep = 0; sleep < 1000000; ++sleep) |
| 188 | + ; |
| 189 | + } |
| 190 | + system("pause"); |
| 191 | + system("cls"); |
| 192 | +} |
| 193 | + |
| 194 | +void login::ForgotPassword() |
| 195 | +{ |
| 196 | + string forgotChoice, count, secondCount; |
| 197 | + system("cls"); |
| 198 | + cout << "\n\t\t\tPress 1 to enter USERNAME\n"; |
| 199 | + cout << "\t\t\tPress 2 to go back to MENU\n"; |
| 200 | + cout << "\n\t\t\tEnter choice: "; |
| 201 | + cin >> forgotChoice; |
| 202 | + |
| 203 | + if (forgotChoice == "1") |
| 204 | + { |
| 205 | + string user, userSecurity, forgotId, forgotPass, forgotSecurity; |
| 206 | + int newHashPassword, forgotSecHash; |
| 207 | + system("cls"); |
| 208 | + cout << "\n\t\tEnter USERNAME: "; |
| 209 | + cin >> user; |
| 210 | + cout << endl; |
| 211 | + |
| 212 | + ifstream input("data.txt"); |
| 213 | + while (input >> forgotId >> forgotPass >> forgotSecurity) |
| 214 | + { |
| 215 | + if (user == forgotId) |
| 216 | + { |
| 217 | + cout << "\n\t\tUser found\n\t\tSecurity Question: What was your favorite childhood movie?: "; |
| 218 | + cin.ignore(); |
| 219 | + getline(cin, userSecurity); |
| 220 | + cout << endl; |
| 221 | + |
| 222 | + string hashing = userSecurity; |
| 223 | + hash<string> mystdhash; |
| 224 | + forgotSecHash = mystdhash(hashing); |
| 225 | + |
| 226 | + if (stoi(forgotSecurity) == forgotSecHash) |
| 227 | + { |
| 228 | + system("cls"); |
| 229 | + string newPass; |
| 230 | + cout << "\t\tSecurity Question correct\n"; |
| 231 | + cout << "\n\t\tEnter new PASSWORD: "; |
| 232 | + cin >> newPass; |
| 233 | + |
| 234 | + string newPassHash = newPass; |
| 235 | + hash<string> mystdhash2; |
| 236 | + newHashPassword = mystdhash2(newPassHash); |
| 237 | + |
| 238 | + count = "1"; |
| 239 | + break; |
| 240 | + } |
| 241 | + else |
| 242 | + { |
| 243 | + string incorrectChoice; |
| 244 | + system("cls"); |
| 245 | + cout << "\t\tSecurity Question incorrect\n"; |
| 246 | + cout << "\t\tPress 1 to Re-Enter\n\t\tPress 2 to return to MENU\n"; |
| 247 | + cout << "\n\t\tEnter choice: "; |
| 248 | + cin >> incorrectChoice; |
| 249 | + |
| 250 | + if (incorrectChoice == "1") |
| 251 | + { |
| 252 | + ForgotPassword(); |
| 253 | + } |
| 254 | + if (incorrectChoice == "2") |
| 255 | + { |
| 256 | + system("cls"); |
| 257 | + cout << "\tReturning to MENU\n"; |
| 258 | + return; |
| 259 | + } |
| 260 | + else |
| 261 | + { |
| 262 | + system("cls"); |
| 263 | + cout << "\tInvalid answer... Returning to menu\n"; |
| 264 | + return; |
| 265 | + } |
| 266 | + } |
| 267 | + } |
| 268 | + else |
| 269 | + { |
| 270 | + string newChoice; |
| 271 | + system("cls"); |
| 272 | + cout << "\t\tUser not found\n"; |
| 273 | + cout << "\t\tPress 1 to Re-Enter USERNAME\n\t\tPress 2 to return to MENU\n"; |
| 274 | + cout << "\n\t\tEnter choice: "; |
| 275 | + cin >> newChoice; |
| 276 | + if (newChoice == "1") |
| 277 | + { |
| 278 | + ForgotPassword(); |
| 279 | + } |
| 280 | + else if (newChoice == "2") |
| 281 | + { |
| 282 | + system("cls"); |
| 283 | + cout << "\tReturning to MENU\n"; |
| 284 | + return; |
| 285 | + } |
| 286 | + else |
| 287 | + { |
| 288 | + system("cls"); |
| 289 | + cout << "\tChoice invalid... Returning to MENU\n"; |
| 290 | + return; |
| 291 | + } |
| 292 | + } |
| 293 | + } |
| 294 | + input.close(); |
| 295 | + if (count == "1") |
| 296 | + { |
| 297 | + DeleteLine(user); |
| 298 | + secondCount = "1"; |
| 299 | + } |
| 300 | + if (secondCount == "1") |
| 301 | + { |
| 302 | + ofstream f1("data.txt", ios::app); |
| 303 | + f1 << user << ' ' << newHashPassword << ' ' << forgotSecHash << endl; |
| 304 | + system("cls"); |
| 305 | + cout << "\t\t\t Your password has been updated!\n"; |
| 306 | + return; |
| 307 | + } |
| 308 | + } |
| 309 | + else if (forgotChoice == "2") |
| 310 | + { |
| 311 | + system("cls"); |
| 312 | + cout << "\tReturning to MENU\n"; |
| 313 | + return; |
| 314 | + } |
| 315 | + else |
| 316 | + { |
| 317 | + system("cls"); |
| 318 | + cout << "\tChoice invalid... Try again\n"; |
| 319 | + ForgotPassword(); |
| 320 | + } |
| 321 | +} |
| 322 | + |
| 323 | +void login::DeleteLine(string userDelete) |
| 324 | +{ |
| 325 | + string line; |
| 326 | + ifstream myFile; |
| 327 | + myFile.open("data.txt"); |
| 328 | + ofstream temp; |
| 329 | + temp.open("temp.txt"); |
| 330 | + while (getline(myFile, line)) |
| 331 | + { |
| 332 | + if (line.substr(0, userDelete.size()) != userDelete) |
| 333 | + { |
| 334 | + temp << line << endl; |
| 335 | + } |
| 336 | + } |
| 337 | + myFile.close(); |
| 338 | + temp.close(); |
| 339 | + remove("data.txt"); |
| 340 | + rename("temp.txt", "data.txt"); |
| 341 | +} |
0 commit comments