Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.80 KB | None | 0 0
  1. // Kaitlynn Larrivee Smells like a chicken
  2. // Techi Gadgets Account Program. Reads in User / Admin and displays
  3. // information according to account level access.
  4.  
  5. #include <iostream>
  6. #include <iomanip>
  7. #include <cstdlib>
  8. #include <ctime>
  9. #include <cmath>
  10. #include <string>
  11. #include <fstream>
  12. using namespace std;
  13.  
  14. void showAll(string theAccounts[5][7]);
  15. void sortInput(string theAccounts[5][7]);
  16. bool readFile(string theAccounts[5][7]);
  17. bool validateUser(string theAccounts[5][7], string, string, int*);
  18.  
  19. int main()
  20. {
  21. cout << setprecision(2) << fixed << showpoint; // Setting Formatting
  22. bool userGood; // Holds Bool value for User Validation
  23. bool fileGood; // Holds Bool value for File Read being good or bad
  24. int saveRow; // Holds the row Username & Password match were found in.
  25. string username; // User input username
  26. string password; // User input password
  27. string accountData[5][7] = { " " }; // Accounts Array that file is read into.
  28.  
  29. fileGood = readFile(accountData); // Validates if the Account Data file is opened, if so reads data into array.
  30.  
  31. {
  32. if (fileGood == true)
  33. cout << "The file was read succesfully...\n\n";
  34. else
  35. {
  36. cout << " The file was not read succesfully... exiting program.\n\n";
  37. return 0;
  38. }
  39. }
  40.  
  41. sortInput(accountData); // Sorts the array by last name alphabetically.
  42.  
  43. do
  44. {
  45. // Resets username & password each loop so they cannot be used by default.
  46. username = "";
  47. password = "";
  48.  
  49.  
  50. // Prompts for Username & Password, exiting program on 0 entry in either case.
  51. cout << "Enter the following information, or press 0 to Exit anytime..." << endl;
  52. cout << "Please enter your User Name :";
  53. cin >> username;
  54.  
  55. if (username == "0")
  56. {
  57. cout << "\nThank You, have a nice day.\n\n";
  58. return 0;
  59. }
  60.  
  61. cout << "Please Enter your User Password :";
  62. cin >> password;
  63.  
  64. if (password == "0")
  65. {
  66. cout << "\nThank You, have a nice day.\n\n";
  67. return 0;
  68. }
  69.  
  70. userGood = validateUser(accountData, username, password, &saveRow); // Call Uservalidation to check Username / Password Combination.
  71.  
  72. {
  73. if (userGood == true) // On true splits to Admin / User level paths.
  74. {
  75. if (accountData[saveRow][5] == "A") // Admin Level path. Prints out array table & saves to output file.
  76. {
  77. showAll(accountData);
  78. }
  79.  
  80. else
  81. {
  82. cout << "\nWelcome Back " << accountData[saveRow][1] << "!\n\n";
  83. cout << setw(8) << left << accountData[saveRow][1];
  84. cout << setw(8) << left << accountData[saveRow][2];
  85. cout << setw(6) << left << accountData[saveRow][4];
  86. cout << setw(4) << left << accountData[saveRow][5];
  87. cout << setw(15) << left << accountData[saveRow][6];
  88. cout << endl << endl;
  89. }
  90. }
  91.  
  92. else // On False outputs error message and loops back around for Username & Password.
  93. {
  94. cout << "Username and password do not match, please try again ...\n" << endl;
  95. }
  96. }
  97.  
  98.  
  99. } while (password != "0" || username != "0");
  100.  
  101.  
  102. return 0;
  103.  
  104. }
  105.  
  106. void showAll(string theAccounts[5][7])
  107. {
  108. // If user is Admin, opens + creates an outputFile sortedBackup.txt
  109. // Then outputs entire array table while saving contents to sortedBackup.txt
  110. ofstream outputFile;
  111. outputFile.open("sortedBackup.txt");
  112. int row = 5;
  113.  
  114. cout << "\n";
  115.  
  116. for (int index = 0; index < row; index++)
  117. // outputs Array to screen formatted for each column.
  118. // Also writes to backup outputFile.
  119. {
  120. cout << setw(20) << right << theAccounts[index][0];
  121. cout << setw(8) << right << theAccounts[index][1];
  122. cout << setw(8) << right << theAccounts[index][2];
  123. cout << setw(10) << right << theAccounts[index][3];
  124. cout << setw(6) << right << theAccounts[index][4];
  125. cout << setw(4) << right << theAccounts[index][5];
  126. cout << setw(15) << right << theAccounts[index][6];
  127. cout << endl;
  128.  
  129. outputFile << setw(20) << right << theAccounts[index][0];
  130. outputFile << setw(8) << right << theAccounts[index][1];
  131. outputFile << setw(8) << right << theAccounts[index][2];
  132. outputFile << setw(10) << right << theAccounts[index][3];
  133. outputFile << setw(6) << right << theAccounts[index][4];
  134. outputFile << setw(4) << right << theAccounts[index][5];
  135. outputFile << setw(15) << right << theAccounts[index][6];
  136. outputFile << endl;
  137. }
  138.  
  139. cout << "Backup file created ....\n " << endl;
  140.  
  141. outputFile.close();
  142.  
  143. }
  144.  
  145. void sortInput(string theAccounts[5][7])
  146. {
  147. // Code to sort the array by last name alphabetically.
  148. bool sorted = false;
  149. string temp;
  150.  
  151. while (!sorted) {
  152. sorted = true;
  153. for (int index = 0; index < 4; ++index) {
  154. if (theAccounts[index][2] > theAccounts[index + 1][2]) {
  155. sorted = false;
  156. for (int last = 0; last < 7; ++last) {
  157. temp = theAccounts[index][last];
  158. theAccounts[index][last] = theAccounts[index + 1][last];
  159. theAccounts[index + 1][last] = temp;
  160. }
  161. }
  162. }
  163. }
  164. }
  165.  
  166.  
  167.  
  168. bool readFile(string theAccounts[5][7]) // Function opens the AccoutData.txt, if succesful reads data into array & returns true, otherwise returns false.
  169. {
  170. bool fileRead = false;
  171. int row = 5;
  172. int col = 7;
  173. ifstream inputFile; // inputFile file input object.
  174. inputFile.open("AccountData.txt"); // Opens AccountData.txt file
  175.  
  176. if (inputFile)
  177. {
  178. fileRead = true;
  179. // Reads file into theAccounts array.
  180. for (int index = 0; index < row; index++)
  181. for (int index2 = 0; index2 < col; index2++)
  182. inputFile >> theAccounts[index][index2];
  183. }
  184.  
  185. inputFile.close();
  186.  
  187. return fileRead;
  188. }
  189.  
  190. bool validateUser(string theAccounts[5][7], string username, string password, int *saveRow)
  191. // Validates user input username & password. Passes back True / False & saveRow value.
  192. {
  193. bool passed = false;
  194. int user = 0;
  195. int pass = 3;
  196. int row = 0;
  197.  
  198. // For loop passing through theAccounts array finding matching username + password.
  199. // Returns false if no match is found.
  200.  
  201. for (int row = 0; row <= 4; row++)
  202. {
  203. if ((username == theAccounts[row][user]) && (password == theAccounts[row][pass]))
  204. {
  205. passed = true;
  206. *saveRow = row;
  207. }
  208. }
  209.  
  210. return passed; // Passed bool value brought back to Main to determine true / false password & username input.
  211. }
  212.  
  213.  
  214.  
  215. /*
  216. Algorithm Used
  217.  
  218. A. Declare Necessary variables and 2D array to store data from input file.
  219. B. Call the readFile function and capture the returned value.
  220. C. Test if file was succesffully read if so continue with program else exit with error message.
  221. D. Sorts array alphabetically by last name.
  222. E. Do While...
  223. a. Ask User to Enter user name or Zero to exit.
  224. b. Read in User Name
  225. c. if USer Name read in is "zero" Exit program
  226. d. Ask the user to Enter their password or Zero to Exit
  227. e. Read in User Password
  228. f. If User Password read in is "zero" exit program.
  229. g. Call the Validate User function and capture the returned bool value & saveRow value
  230. h. If returned value from the Validate User function is true, continue
  231. program to check access code if U or A
  232. i. If U - Display User Name, Year Joined, Access Level, and Tech Status
  233. ii. If A - Call showAll function to display array table & write contents to outputFile.
  234. i. Else if returned value Not Valid from the Validate User function, FALSE,
  235. output message usernamd and password invalid. Loops back to top.
  236.  
  237. */
  238. Homework 6 M6A2 - Christopher Olson.cpp
  239. // Christopher Olson, June 17th, COP 2000 Summer Semester
  240. // Techi Gadgets Account Program. Reads in User / Admin and displays
  241. // information according to account level access.
  242.  
  243. #include <iostream>
  244. #include <iomanip>
  245. #include <cstdlib>
  246. #include <ctime>
  247. #include <cmath>
  248. #include <string>
  249. #include <fstream>
  250. using namespace std;
  251.  
  252. void showAll(string theAccounts[5][7]);
  253. void sortInput(string theAccounts[5][7]);
  254. bool readFile(string theAccounts[5][7]);
  255. bool validateUser(string theAccounts[5][7], string, string, int*);
  256.  
  257. int main()
  258. {
  259. cout << setprecision(2) << fixed << showpoint; // Setting Formatting
  260. bool userGood; // Holds Bool value for User Validation
  261. bool fileGood; // Holds Bool value for File Read being good or bad
  262. int saveRow; // Holds the row Username & Password match were found in.
  263. string username; // User input username
  264. string password; // User input password
  265. string accountData[5][7] = { " " }; // Accounts Array that file is read into.
  266.  
  267. fileGood = readFile(accountData); // Validates if the Account Data file is opened, if so reads data into array.
  268.  
  269. {
  270. if (fileGood == true)
  271. cout << "The file was read succesfully...\n\n";
  272. else
  273. {
  274. cout << " The file was not read succesfully... exiting program.\n\n";
  275. return 0;
  276. }
  277. }
  278.  
  279. sortInput(accountData); // Sorts the array by last name alphabetically.
  280.  
  281. do
  282. {
  283. // Resets username & password each loop so they cannot be used by default.
  284. username = "";
  285. password = "";
  286.  
  287.  
  288. // Prompts for Username & Password, exiting program on 0 entry in either case.
  289. cout << "Enter the following information, or press 0 to Exit anytime..." << endl;
  290. cout << "Please enter your User Name :";
  291. cin >> username;
  292.  
  293. if (username == "0")
  294. {
  295. cout << "\nThank You, have a nice day.\n\n";
  296. return 0;
  297. }
  298.  
  299. cout << "Please Enter your User Password :";
  300. cin >> password;
  301.  
  302. if (password == "0")
  303. {
  304. cout << "\nThank You, have a nice day.\n\n";
  305. return 0;
  306. }
  307.  
  308. userGood = validateUser(accountData, username, password, &saveRow); // Call Uservalidation to check Username / Password Combination.
  309.  
  310. {
  311. if (userGood == true) // On true splits to Admin / User level paths.
  312. {
  313. if (accountData[saveRow][5] == "A") // Admin Level path. Prints out array table & saves to output file.
  314. {
  315. showAll(accountData);
  316. }
  317.  
  318. else
  319. {
  320. cout << "\nWelcome Back " << accountData[saveRow][1] << "!\n\n";
  321. cout << setw(8) << left << accountData[saveRow][1];
  322. cout << setw(8) << left << accountData[saveRow][2];
  323. cout << setw(6) << left << accountData[saveRow][4];
  324. cout << setw(4) << left << accountData[saveRow][5];
  325. cout << setw(15) << left << accountData[saveRow][6];
  326. cout << endl << endl;
  327. }
  328. }
  329.  
  330. else // On False outputs error message and loops back around for Username & Password.
  331. {
  332. cout << "Username and password do not match, please try again ...\n" << endl;
  333. }
  334. }
  335.  
  336.  
  337. } while (password != "0" || username != "0");
  338.  
  339.  
  340. return 0;
  341.  
  342. }
  343.  
  344. void showAll(string theAccounts[5][7])
  345. {
  346. // If user is Admin, opens + creates an outputFile sortedBackup.txt
  347. // Then outputs entire array table while saving contents to sortedBackup.txt
  348. ofstream outputFile;
  349. outputFile.open("sortedBackup.txt");
  350. int row = 5;
  351.  
  352. cout << "\n";
  353.  
  354. for (int index = 0; index < row; index++)
  355. // outputs Array to screen formatted for each column.
  356. // Also writes to backup outputFile.
  357. {
  358. cout << setw(20) << right << theAccounts[index][0];
  359. cout << setw(8) << right << theAccounts[index][1];
  360. cout << setw(8) << right << theAccounts[index][2];
  361. cout << setw(10) << right << theAccounts[index][3];
  362. cout << setw(6) << right << theAccounts[index][4];
  363. cout << setw(4) << right << theAccounts[index][5];
  364. cout << setw(15) << right << theAccounts[index][6];
  365. cout << endl;
  366.  
  367. outputFile << setw(20) << right << theAccounts[index][0];
  368. outputFile << setw(8) << right << theAccounts[index][1];
  369. outputFile << setw(8) << right << theAccounts[index][2];
  370. outputFile << setw(10) << right << theAccounts[index][3];
  371. outputFile << setw(6) << right << theAccounts[index][4];
  372. outputFile << setw(4) << right << theAccounts[index][5];
  373. outputFile << setw(15) << right << theAccounts[index][6];
  374. outputFile << endl;
  375. }
  376.  
  377. cout << "Backup file created ....\n " << endl;
  378.  
  379. outputFile.close();
  380.  
  381. }
  382.  
  383. void sortInput(string theAccounts[5][7])
  384. {
  385. // Code to sort the array by last name alphabetically.
  386. bool sorted = false;
  387. string temp;
  388.  
  389. while (!sorted) {
  390. sorted = true;
  391. for (int index = 0; index < 4; ++index) {
  392. if (theAccounts[index][2] > theAccounts[index + 1][2]) {
  393. sorted = false;
  394. for (int last = 0; last < 7; ++last) {
  395. temp = theAccounts[index][last];
  396. theAccounts[index][last] = theAccounts[index + 1][last];
  397. theAccounts[index + 1][last] = temp;
  398. }
  399. }
  400. }
  401. }
  402. }
  403.  
  404.  
  405.  
  406. bool readFile(string theAccounts[5][7]) // Function opens the AccoutData.txt, if succesful reads data into array & returns true, otherwise returns false.
  407. {
  408. bool fileRead = false;
  409. int row = 5;
  410. int col = 7;
  411. ifstream inputFile; // inputFile file input object.
  412. inputFile.open("AccountData.txt"); // Opens AccountData.txt file
  413.  
  414. if (inputFile)
  415. {
  416. fileRead = true;
  417. // Reads file into theAccounts array.
  418. for (int index = 0; index < row; index++)
  419. for (int index2 = 0; index2 < col; index2++)
  420. inputFile >> theAccounts[index][index2];
  421. }
  422.  
  423. inputFile.close();
  424.  
  425. return fileRead;
  426. }
  427.  
  428. bool validateUser(string theAccounts[5][7], string username, string password, int *saveRow)
  429. // Validates user input username & password. Passes back True / False & saveRow value.
  430. {
  431. bool passed = false;
  432. int user = 0;
  433. int pass = 3;
  434. int row = 0;
  435.  
  436. // For loop passing through theAccounts array finding matching username + password.
  437. // Returns false if no match is found.
  438.  
  439. for (int row = 0; row <= 4; row++)
  440. {
  441. if ((username == theAccounts[row][user]) && (password == theAccounts[row][pass]))
  442. {
  443. passed = true;
  444. *saveRow = row;
  445. }
  446. }
  447.  
  448. return passed; // Passed bool value brought back to Main to determine true / false password & username input.
  449. }
  450.  
  451.  
  452.  
  453. /*
  454. Algorithm Used
  455. A. Declare Necessary variables and 2D array to store data from input file.
  456. B. Call the readFile function and capture the returned value.
  457. C. Test if file was succesffully read if so continue with program else exit with error message.
  458. D. Sorts array alphabetically by last name.
  459. E. Do While...
  460. a. Ask User to Enter user name or Zero to exit.
  461. b. Read in User Name
  462. c. if USer Name read in is "zero" Exit program
  463. d. Ask the user to Enter their password or Zero to Exit
  464. e. Read in User Password
  465. f. If User Password read in is "zero" exit program.
  466. g. Call the Validate User function and capture the returned bool value & saveRow value
  467. h. If returned value from the Validate User function is true, continue
  468. program to check access code if U or A
  469. i. If U - Display User Name, Year Joined, Access Level, and Tech Status
  470. ii. If A - Call showAll function to display array table & write contents to outputFile.
  471. i. Else if returned value Not Valid from the Validate User function, FALSE,
  472. output message usernamd and password invalid. Loops back to top.
  473. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement