Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Christopher Olson, June 17th, COP 2000 Summer Semester
- // Techi Gadgets Account Program. Reads in User / Admin and displays
- // information according to account level access.
- #include <iostream>
- #include <iomanip>
- #include <cstdlib>
- #include <ctime>
- #include <cmath>
- #include <string>
- #include <fstream>
- using namespace std;
- void showAll(string theAccounts[5][7]);
- void sortInput(string theAccounts[5][7]);
- bool readFile(string theAccounts[5][7]);
- bool validateUser(string theAccounts[5][7], string, string, int&);
- int main()
- {
- cout << setprecision(2) << fixed << showpoint; // Setting Formatting
- bool userGood;
- bool fileGood;
- int saveRow;
- const int row = 5;
- const int cols = 7;
- string username;
- string password;
- string accountData[5][7] = { " " };
- fileGood = readFile(accountData); // Validates if the Account Data file is opened, if so reads data into array.
- {
- if (fileGood == true)
- cout << "The file was read succesfully...\n\n";
- else
- {
- cout << " The file was not read succesfully... exiting program.\n\n";
- return 0;
- }
- }
- sortInput(accountData); // Sorts the array by last name alphabetically.
- do
- {
- // Prompts for Username & Password, exiting program on 0 entry in either case.
- cout << "Enter the following information, or press 0 to Exit anytime..." << endl;
- cout << "Please enter your User Name :";
- cin >> username;
- if (username == "0")
- {
- cout << "\nThank You, have a nice day.\n\n";
- return 0;
- }
- cout << "Please Enter your User Password :";
- cin >> password;
- if (password == "0")
- {
- cout << "\nThank You, have a nice day.\n\n";
- return 0;
- }
- userGood = validateUser(accountData, username, password, saveRow); // Call Uservalidation to check Username / Password Combination.
- {
- if (userGood == true) // On true splits to Admin / User level paths.
- {
- if (accountData[saveRow][5] == "A") // Admin Level path. Prints out array table & saves to output file.
- {
- showAll(accountData);
- }
- else
- {
- cout << "Welcome Back " << accountData[saveRow][1] << "!\n";
- for (int col = 1; col <= 6; col++) // User Level Path, prints User Row & Columns 1, 2, 4, 5, 6. Skips column 3.
- {
- if (col != 3)
- {
- cout << setw(8) << accountData[saveRow][col];
- }
- }
- cout << endl;
- }
- }
- else // On False outputs error message and loops back around for Username & Password.
- {
- cout << "Username and password do not match, please try again ...\n" << endl;
- }
- }
- } while (password != "0" || username != "0");
- return 0;
- }
- void showAll(string theAccounts[5][7])
- {
- // If user is Admin, opens + creates an outputFile sortedBackup.txt
- // Then outputs entire array table while saving contents to sortedBackup.txt
- ofstream outputFile;
- outputFile.open("sortedBackup.txt");
- int row = 5;
- int col = 7;
- for (int index = 0; index < row; index++) // Writes array to output backup file sortedBackup.txt .
- {
- for (int index2 = 0; index2 < col; index2++)
- {
- outputFile << theAccounts[index][index2];
- }
- }
- for (int index = 0; index < row; index++) // outputs Array to screen formatted for each column.
- {
- cout << setw(20) << right << theAccounts[index][0];
- cout << setw(8) << right << theAccounts[index][1];
- cout << setw(8) << right << theAccounts[index][2];
- cout << setw(10) << right << theAccounts[index][3];
- cout << setw(6) << right << theAccounts[index][4];
- cout << setw(4) << right << theAccounts[index][5];
- cout << setw(15) << right << theAccounts[index][6];
- cout << endl;
- }
- cout << "Backup file created ....\n " << endl;
- }
- void sortInput(string theAccounts[5][7])
- {
- // Sort loop to sort account by last name.
- bool swap;
- string temp;
- int size = 5;
- int last = 2;
- do
- {
- swap = false;
- for (int count = 0; count < (size - 1); count++)
- {
- if (theAccounts[count][last] > theAccounts[count + 1][last])
- {
- temp = theAccounts[count][last];
- theAccounts[count][last] = theAccounts[count + 1][last];
- theAccounts[count + 1][last] = temp;
- swap = true;
- }
- }
- } while (swap==true);
- cout << "stop here";
- }
- bool readFile(string theAccounts[5][7]) // Function opens the AccoutData.txt, if succesful reads data into array & returns true, otherwise returns false.
- {
- bool fileRead = false;
- int row = 5;
- int col = 7;
- ifstream inputFile; // inputFile file input object.
- inputFile.open("AccountData.txt"); // Opens AccountData.txt file
- if (inputFile)
- {
- fileRead = true;
- // Reads file into theAccounts array.
- for (int index = 0; index < row; index++)
- for (int index2 = 0; index2 < col; index2++)
- inputFile >> theAccounts[index][index2];
- }
- inputFile.close();
- return fileRead;
- }
- bool validateUser(string theAccounts[5][7], string username, string password, int &saveRow)
- {
- bool passed = false;
- int user = 0;
- int pass = 4;
- int row = 0;
- // For loop passing through theAccounts array finding matching username + password.
- // Returns false if no match is found.
- for (int row = 0; row <= 4; row++)
- {
- if (username == theAccounts[row][user] && password == theAccounts[row][pass])
- {
- passed = true;
- saveRow = row;
- }
- }
- return passed;
- }
- /*
- Algorithm Used
- A. Decalre Necessary variables and 2D array to store data from input file.
- B. Call the readFile function and capture the returned value.
- C. Test if file was succesffully read if so continue wiuth program else exit with error message.
- D. Do While...
- a. Ask User to Enter user name or Zero to exit.
- b. Read in User Name
- c. if USer Name read in is "zero" Exit program
- d. Ask the user to Enter their password or Zero to Exit
- e. Read in User Password
- f. If User PAssword read in is "zero" exit program.
- g. Call the Validate User function and capture the returned value
- h. IF returned value from the Validate User function is ture, continue
- program to check access code if U or A
- i. If U - code appropriatly
- ii. If A - Code appropriatly
- i. Else if returned value Not Valid from the Validate User function, FALSE,
- output message usernamd and password invalid.
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement