Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. // Christopher Olson, June 17th, COP 2000 Summer Semester
  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;
  23. bool fileGood;
  24. int saveRow;
  25. const int row = 5;
  26. const int cols = 7;
  27. string username;
  28. string password;
  29. string accountData[5][7] = { " " };
  30.  
  31. fileGood = readFile(accountData); // Validates if the Account Data file is opened, if so reads data into array.
  32.  
  33. {
  34. if (fileGood == true)
  35. cout << "The file was read succesfully...\n\n";
  36. else
  37. {
  38. cout << " The file was not read succesfully... exiting program.\n\n";
  39. return 0;
  40. }
  41. }
  42.  
  43. sortInput(accountData); // Sorts the array by last name alphabetically.
  44.  
  45. do
  46. {
  47.  
  48. // Prompts for Username & Password, exiting program on 0 entry in either case.
  49. cout << "Enter the following information, or press 0 to Exit anytime..." << endl;
  50. cout << "Please enter your User Name :";
  51. cin >> username;
  52.  
  53. if (username == "0")
  54. {
  55. cout << "\nThank You, have a nice day.\n\n";
  56. return 0;
  57. }
  58.  
  59. cout << "Please Enter your User Password :";
  60. cin >> password;
  61.  
  62. if (password == "0")
  63. {
  64. cout << "\nThank You, have a nice day.\n\n";
  65. return 0;
  66. }
  67.  
  68. userGood = validateUser(accountData, username, password, saveRow); // Call Uservalidation to check Username / Password Combination.
  69.  
  70. {
  71. if (userGood == true) // On true splits to Admin / User level paths.
  72. {
  73. if (accountData[saveRow][5] == "A") // Admin Level path. Prints out array table & saves to output file.
  74. {
  75. showAll(accountData);
  76. }
  77.  
  78. else
  79. {
  80. cout << "Welcome Back " << accountData[saveRow][1] << "!\n";
  81. for (int col = 1; col <= 6; col++) // User Level Path, prints User Row & Columns 1, 2, 4, 5, 6. Skips column 3.
  82. {
  83. if (col != 3)
  84. {
  85. cout << setw(8) << accountData[saveRow][col];
  86. }
  87. }
  88. cout << 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. int col = 7;
  114.  
  115. for (int index = 0; index < row; index++) // Writes array to output backup file sortedBackup.txt .
  116. {
  117. for (int index2 = 0; index2 < col; index2++)
  118. {
  119. outputFile << theAccounts[index][index2];
  120. }
  121.  
  122. }
  123.  
  124.  
  125. for (int index = 0; index < row; index++) // outputs Array to screen formatted for each column.
  126. {
  127. cout << setw(20) << right << theAccounts[index][0];
  128. cout << setw(8) << right << theAccounts[index][1];
  129. cout << setw(8) << right << theAccounts[index][2];
  130. cout << setw(10) << right << theAccounts[index][3];
  131. cout << setw(6) << right << theAccounts[index][4];
  132. cout << setw(4) << right << theAccounts[index][5];
  133. cout << setw(15) << right << theAccounts[index][6];
  134. cout << endl;
  135. }
  136.  
  137. cout << "Backup file created ....\n " << endl;
  138.  
  139. }
  140.  
  141. void sortInput(string theAccounts[5][7])
  142. {
  143. // Sort loop to sort account by last name.
  144. bool swap;
  145. string temp;
  146. int size = 5;
  147. int last = 2;
  148.  
  149. do
  150. {
  151. swap = false;
  152. for (int count = 0; count < (size - 1); count++)
  153. {
  154. if (theAccounts[count][last] > theAccounts[count + 1][last])
  155. {
  156. temp = theAccounts[count][last];
  157. theAccounts[count][last] = theAccounts[count + 1][last];
  158. theAccounts[count + 1][last] = temp;
  159. swap = true;
  160. }
  161. }
  162. } while (swap==true);
  163. cout << "stop here";
  164.  
  165. }
  166.  
  167.  
  168.  
  169. bool readFile(string theAccounts[5][7]) // Function opens the AccoutData.txt, if succesful reads data into array & returns true, otherwise returns false.
  170. {
  171. bool fileRead = false;
  172. int row = 5;
  173. int col = 7;
  174. ifstream inputFile; // inputFile file input object.
  175. inputFile.open("AccountData.txt"); // Opens AccountData.txt file
  176.  
  177. if (inputFile)
  178. {
  179. fileRead = true;
  180. // Reads file into theAccounts array.
  181. for (int index = 0; index < row; index++)
  182. for (int index2 = 0; index2 < col; index2++)
  183. inputFile >> theAccounts[index][index2];
  184. }
  185.  
  186. inputFile.close();
  187.  
  188. return fileRead;
  189. }
  190.  
  191. bool validateUser(string theAccounts[5][7], string username, string password, int &saveRow)
  192. {
  193. bool passed = false;
  194. int user = 0;
  195. int pass = 4;
  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. for (int row = 0; row <= 4; row++)
  201. {
  202. if (username == theAccounts[row][user] && password == theAccounts[row][pass])
  203. {
  204. passed = true;
  205. saveRow = row;
  206. }
  207. }
  208.  
  209. return passed;
  210. }
  211.  
  212.  
  213.  
  214. /*
  215. Algorithm Used
  216.  
  217. A. Decalre Necessary variables and 2D array to store data from input file.
  218. B. Call the readFile function and capture the returned value.
  219. C. Test if file was succesffully read if so continue wiuth program else exit with error message.
  220. D. Do While...
  221. a. Ask User to Enter user name or Zero to exit.
  222. b. Read in User Name
  223. c. if USer Name read in is "zero" Exit program
  224. d. Ask the user to Enter their password or Zero to Exit
  225. e. Read in User Password
  226. f. If User PAssword read in is "zero" exit program.
  227. g. Call the Validate User function and capture the returned value
  228. h. IF returned value from the Validate User function is ture, continue
  229. program to check access code if U or A
  230. i. If U - code appropriatly
  231. ii. If A - Code appropriatly
  232. i. Else if returned value Not Valid from the Validate User function, FALSE,
  233. output message usernamd and password invalid.
  234.  
  235. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement