Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.20 KB | None | 0 0
  1. //Neal Poole
  2. //Homework 6 - This program reads information from a file and then asks
  3. //the user for their username and password. Depending on their account priviledges
  4. //it will either shoe their personal info or show all the accounts
  5. //(General User or Admin)
  6. //COP2000
  7. //4-20-18
  8.  
  9. #include <iostream>
  10. #include <fstream>
  11. #include <iomanip>
  12. #include <string>
  13. #include <cstring>
  14. #include <vector>
  15.  
  16. using namespace std;
  17.  
  18. void showAll(string[5][7]);
  19. void sortInput(string[5][7]);
  20. bool readFile(string[5][7]);
  21. bool validateUser(string[5][7], string, string, int &);
  22.  
  23. int main()
  24. {
  25.     string username, password;
  26.     int saveRow, position, userInput = 1;
  27.     string accountData[5][7] = { " " };
  28.     readFile(accountData);
  29.  
  30.     if (readFile(accountData))
  31.         cout << "File Read Successfully..." << endl << endl;
  32.  
  33.     else
  34.     {
  35.         cout << "Invalid file, exiting.";
  36.         return 0;
  37.  
  38.     }
  39.  
  40.     do
  41.     {
  42.         cout << endl << "Enter the following information or 0 to exit...";
  43.         cout << endl << "Please Enter Your User Name > ";
  44.         cin >> username;
  45.         if (username == "zero")
  46.             return 0;
  47.         cout << "Please Enter Your User Password > ";
  48.         cin >> password;
  49.         if (password == "zero")
  50.             return 0;
  51.         validateUser(accountData, username, password, saveRow);
  52.         if (validateUser(accountData, username, password, saveRow))
  53.         {
  54.             cout << endl << "Welcome Back" << accountData[saveRow][1];
  55.  
  56.             if (accountData[saveRow][5] == "A")
  57.             {
  58.                 sortInput(accountData);
  59.                 showAll(accountData);
  60.  
  61.                 ofstream outputFile;
  62.                 outputFile.open("sortedBackup.txt");
  63.  
  64.                 for (int a = 0; a < 5; a++)
  65.                 {
  66.                     for (int b = 0; b < 7; b++)
  67.                     {
  68.                         outputFile << accountData[a][b];
  69.                     }
  70.                     cout << endl;
  71.                 }
  72.                 if (outputFile)
  73.                 {
  74.                     cout << endl << "Backup File Completed";
  75.                     continue;
  76.                 }
  77.                 else
  78.                 {
  79.                     cout << endl << "Error Saving File, exiting....";
  80.                     return 0;
  81.                 }
  82.             }
  83.  
  84.             else
  85.             {
  86.                 cout << endl << accountData[saveRow][1] << "   " << accountData[saveRow][2] << "   " << accountData[saveRow][4] << "   " << accountData[saveRow][5] << "   " << accountData[saveRow][6];
  87.                 continue;
  88.             }
  89.         }
  90.  
  91.         else
  92.         {
  93.             cout << endl << "Username and Password do not match... Please try Again";
  94.             continue;
  95.         }
  96.  
  97.  
  98.  
  99.  
  100.     } while (userInput != 0);
  101.  
  102.  
  103.     return 0;
  104. }
  105.  
  106. void showAll(string theAccounts[5][7])
  107. {
  108.     for (int x = 0; x < 5; x++)
  109.     {
  110.         for (int y = 0; y < 7; y++)
  111.         {
  112.             cout << setw(8) << theAccounts[x][y] << " ";
  113.         }
  114.         cout << endl;
  115.     }
  116. }
  117.  
  118. void sortInput(string theAccounts[5][7])
  119. {
  120.     bool swap;
  121.     string temp;
  122.  
  123.     do
  124.     {
  125.         swap = false;
  126.         for (int x = 0; x < 4; x++)
  127.         {
  128.             if ((theAccounts[x][1]) > (theAccounts[x + 1][1]))
  129.             {
  130.                 temp = theAccounts[x][1];
  131.                 theAccounts[x][1] = theAccounts[x + 1][1];
  132.                 theAccounts[x][0] = theAccounts[x + 1][0];
  133.                 theAccounts[x][2] = theAccounts[x + 1][2];
  134.                 theAccounts[x][3] = theAccounts[x + 1][3];
  135.                 theAccounts[x][4] = theAccounts[x + 1][4];
  136.                 theAccounts[x + 1][1] = temp;
  137.                 swap = true;
  138.  
  139.             }
  140.         }
  141.  
  142.     } while (swap);
  143. }
  144.  
  145. bool readFile(string theAccounts[5][7])
  146. {
  147.     ifstream inputFile;
  148.     inputFile.open("C:/Users/nealc/Desktop/AccountData.txt");
  149.  
  150.     if (inputFile)
  151.     {
  152.         for (int x = 0; x < 5; x++)
  153.         {
  154.             for (int y = 0; y < 7; y++)
  155.             {
  156.                 inputFile >> theAccounts[x][y];
  157.             }
  158.             cout << endl;
  159.         }
  160.         return true;
  161.     }
  162.  
  163.     else
  164.     {
  165.         return false;
  166.     }
  167.  
  168. }
  169.  
  170. bool validateUser(string theAccounts[5][7], string username, string password, int &saveRow)
  171. {
  172.     int x, y;
  173.     bool found = false;
  174.     for (x = 0, y = 0; x < 5, y < 7; x++, y++)
  175.     {
  176.         for (y = 0; y < 7; y++)
  177.         {
  178.             if (theAccounts[x][y] == username)
  179.  
  180.             found = true;
  181.             saveRow = x;
  182.             break;
  183.         }
  184.         cout << endl;
  185.     }
  186.     cout << endl << x;
  187.     switch (saveRow)
  188.     {
  189.     case 0:
  190.         if (password == "squid62")
  191.         {
  192.             return true;
  193.         }
  194.         else
  195.             return false;
  196.     case 1:
  197.         if (password == "gymrat32")
  198.         {
  199.             return true;
  200.         }
  201.         else
  202.             return false;
  203.     case 2:
  204.         if (password == "flower22")
  205.         {
  206.             return true;
  207.         }
  208.         else
  209.             return false;
  210.     case 3:
  211.         if (password == "tuna20")
  212.         {
  213.             return true;
  214.         }
  215.         else
  216.             return false;
  217.     case 4:
  218.         if (password == "ahoy10")
  219.         {
  220.             return true;
  221.         }
  222.         else
  223.             return false;
  224.     }
  225.  
  226. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement