Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- sales.cpp
- 2/8/2016, 3.3.3
- Steven Piper, Jordan Prokop, Jesse Winter
- */
- #include <iostream>
- #include <stdlib.h>
- #include <fstream>
- #include <iomanip>
- #include <cmath>
- #include <string>
- #include <assert.h>
- using namespace std;
- //conversion rates. Used by convertUSD function
- double const USD = 1.0;
- double const CAD = 1.39;
- double const BTC = 0.002664;
- double const GBP = 0.69;
- //this changes the amount of asterisks shown. Only supported for USD
- int const BAR_GRAPH_SCALE = 3500;
- char const CURR_SIGN_USD = '$';
- string const CURR_SIGN_BTC = "BTC";
- char const CURR_SIGN_CAD = '$';
- string const CURR_SIGN_GBP = "GBP";
- //prototypes
- void welcomeScreen();
- char getMenuSelection();
- void menuExec(char control);
- int charToInt(char var);
- int getOptionSelection();
- void options();
- void add();
- void optionMenuExec(int selection);
- void view();
- void genDefConfig();
- void loadOptions();
- void changeOptions(int transferArray);
- void convertUSD();
- void deleteAllFiles();
- void convertUSDConfig();
- int getConvertUSDConfigSelection();
- void configUSD();
- void configCAD();
- void configBTC();
- void configGBP();
- string getSign();
- int main()
- {
- //Generates config if one doesnt exist
- genDefConfig();
- //loads config
- loadOptions();
- //Greets user
- welcomeScreen();
- do
- {
- //sets selection to what the user enters, after being prompted
- char selection = getMenuSelection();
- //uses what the user enters in, to be ran through the switch statement
- menuExec(selection);
- //will continue to repeat until user exits
- } while (1>0);
- system("pause");
- return 0;
- }
- /*************************************************
- Function: welcomeScreen
- Description:Greets the user
- Input: no input
- Precondition: no precondition
- Postcondition: prints out a greeting before returning
- *************************************************/
- void welcomeScreen()
- {
- cout << "Welcome to the SaleS!\n\n";
- return;
- }
- /*************************************************
- Function:getMenuSelection
- Description: This will prompt the user with menu options
- Input: a single number char value
- Precondition: user input must be valid
- Postcondition: Returns selection, char value between 1-3, and char X
- *************************************************/
- char getMenuSelection()
- {
- char selection = ' ';
- do
- {
- //prompt
- cout.setf(ios::left);
- cout << setw(40) << "What would you like to do?" << endl;
- cout << setw(40) << "|Add a sales record" << setw(10) << "| 1" << endl;
- cout << setw(40) << "|View a sales record" << setw(10) << "| 2" << endl;
- cout << setw(40) << "|Options" << setw(10) << "| 3" << endl;
- cout << setw(40) << "|Exit" << setw(10) << "| X" << endl;
- cout << "-->|";
- cin >> selection;
- cout << endl;
- }
- //If an invalid char is entered, it will repeat. This allows for functions to not worry about values out of bounds
- while (!(selection == '1' || selection == '2' || selection == '3' || selection == 'X' || selection == 'x'));
- return selection;
- }
- /*************************************************
- Function: menuExec
- Description: This takes the char control, that the user entered, and runs it through a switch statement to carry out an action
- Input: char control
- Precondition: char control must be x, X, 1, 2, or 3
- Postcondition: Will run what the user selects
- *************************************************/
- void menuExec(char control)
- {
- //converts the char values to int values, for use in switch statement. X = 0
- int controlInt = charToInt(control);
- //This allows for the switch statement to use Titles instead of numbers
- enum menutitles{ EXIT, ADD, VIEW, OPTIONS };
- switch (controlInt)
- {
- case EXIT:
- exit(1);
- break;
- case ADD:
- add();
- break;
- case VIEW:
- view();
- break;
- case OPTIONS:
- options();
- break;
- }
- return;
- }
- /*************************************************
- Function: charToInt
- Description: Converts the user input to an int for the switch statement in menuExec
- Input: char control
- Precondition: char control must be x, X, 1, 2, or 3
- Postcondition: Converts 'x'||'X' = 0, '1' = 1, '2' = 2, '3' = 3
- *************************************************/
- int charToInt(char var)
- {
- int control = 0;
- if (var == 'X' || var == 'x')
- {
- control = 0;
- }
- else if (var == '1')
- {
- control = 1;
- }
- else if (var == '2')
- {
- control = 2;
- }
- else if (var == '3')
- {
- control = 3;
- }
- else if (var == '4')
- {
- control = 4;
- }
- return control;
- }
- /*************************************************
- Function: view
- Description: Shows a graph and currency
- Precondition:called when view is selected by user
- Postcondition: displays names, currecny and a graph that depends on the constant BAR_GRAPH_SCALE
- *************************************************/
- void view()
- {
- string sign = getSign();
- //Declare stream
- ifstream nameInput;
- ifstream salaryInput;
- nameInput.open("names.txt");
- salaryInput.open("sales.txt");
- string name[100];
- double salary[100];
- int index = 0;
- do
- {
- getline(nameInput, name[index]);
- //runs through converter. Default is USD
- salaryInput >> salary[index];
- index++;
- }
- while (!salaryInput.eof());
- nameInput.close();
- salaryInput.close();
- //This is the bar graph. You can change the scale of the graph at the top. BAR_GRAPH_SCALE
- for (int c = 0; c < index - 1; c++)
- {
- //prints out name, cash, and asterisk graph.
- cout << setw(16) << name[c] << " | " << salary[c] << sign << " ||";
- for (int j = 0; j < salary[c] / BAR_GRAPH_SCALE; j++)
- {
- cout << "*";
- }
- cout << endl;
- }
- return;
- }
- /*************************************************
- Function: add
- Description: adds names and salary to txt
- Input: 2 strings and an int
- Precondition:called when add is selected by user
- Postcondition: adds entries to text
- *************************************************/
- void add()
- {
- //Revised Jesse's function
- //Declare stream
- ofstream nameOutput;
- ofstream salaryUSDOutput;
- ofstream salaryConvertedOutput;
- //Open file SalesStats.txt
- nameOutput.open("names.txt", fstream::app);
- salaryUSDOutput.open("salesUSD.txt", fstream::app);
- salaryConvertedOutput.open("sales.txt", fstream::app);
- //defining Arrays with 100 spaces
- string name[100];
- double salary[100];
- int entryAmt = 0;
- cout << "|How many entries are to be added?\n-->|";
- cin >> entryAmt;
- cout << endl;
- //This loop will store the values in the arrays, and read them out to two seperate files. When read, it should read in the same values at the same index. IE name[6] corresponds to salary[6]
- for (int c = 0; c < entryAmt; c++)
- {
- cout << "Please input first then last name of Salesperson.\n-->|";
- cin.ignore();
- //This allows for a whole line (including whitespaces) to be stored
- getline(cin, name[c]);
- //prints out an end line character
- nameOutput << name[c] << '\n';
- cout << "Please input total sales of Salesperson.\n-->|";
- cin >> salary[c];
- //Stores in two seperate files. This is because salesUSD remains unchanged (except appened), while sales constantly gets overwritten by currency conversions, dependin on the second value in config.txt
- salaryUSDOutput << salary[c] << '\n';
- salaryConvertedOutput << salary[c] << '\n';
- cout << endl;
- }
- nameOutput.close();
- salaryUSDOutput.close();
- salaryConvertedOutput.close();
- //This is to update sales.txt, as non converted values will be added. This will rewrite the whole file
- loadOptions();
- }
- /*************************************************
- Function: Option
- Description: gets user input for options
- Input: char selection
- Precondition:must be called before optionMenuExec
- Postcondition: calls charToInt, and passes parameter to execOptionMenu
- *************************************************/
- void options()
- {
- int selection = getOptionSelection();
- charToInt(selection);
- optionMenuExec(selection);
- return;
- }
- /*************************************************
- Function: loadOptions
- Description: This reads config.cfg, to change saved options.
- Input: no input
- Precondition: reccomended to have a clean config file
- Postcondition: changes options to what is saved in config
- *************************************************/
- void loadOptions()
- {
- ifstream readConfig;
- readConfig.open("config.txt");
- //DO NOT TOUCH INDEX 0. THAT VALUE MUST STAY -1 FOR THE ENTIRE LIFE OF THIS PROJECT
- int options[4];
- for (int c = 0; c < 4; c++)
- {
- //This saves the config numbers into options array
- readConfig >> options[c];
- }
- readConfig.close();
- convertUSD();
- return;
- }
- /*************************************************
- Function: generate Default Config
- Description: This will create the default config file when loaded up. This will only create the folder on first run.
- Input: no input
- Precondition: reccomended to have a clean config file
- Postcondition: Creates a default config file, if one does not exist
- *************************************************/
- void genDefConfig()
- {
- ofstream generateDefaultCfg;
- generateDefaultCfg.open("config.txt", ios::app);
- if (generateDefaultCfg.is_open())
- {
- int check = 0;
- ifstream checkFile;
- checkFile.open("config.txt");
- checkFile >> check;
- //If the first value is -1, then nothing will be changed.
- if (check != -1)
- {
- //Generates settings
- generateDefaultCfg << "-1" << endl << "1" << endl << "1" << endl << '1';
- }
- }
- generateDefaultCfg.close();
- return;
- }
- /*************************************************
- Function: getOptionSelection
- Description: gets user input for options
- Input: char selection
- Precondition:must be called before optionMenuExec
- Postcondition: calls charToInt, and passes parameter to execOptionMenu
- *************************************************/
- int getOptionSelection()
- {
- char selection = ' ';
- do
- {
- cout.setf(ios::left);
- cout << setw(40) << "What would you like to do?" << endl;
- cout << setw(40) << "|Change Currency" << setw(10) << "| 1" << endl;
- cout << setw(40) << "|Delete All Files" << setw(10) << "| 2" << endl;
- cout << setw(40) << "|Exit" << setw(10) << "| X" << endl;
- cout << "-->|";
- cin >> selection;
- cout << endl;
- }
- //If an invalid char is entered, it will repeat. This allows for functions to not worry about values out of bounds
- while (!(selection == '1' || selection == '2' || selection == '3' || selection == 'X' || selection == 'x'));
- //converts to int before returning
- int selectionInt = charToInt(selection);
- return selectionInt;
- }
- /*************************************************
- Function: optionsMenuExec
- Description: Prompts for options
- Input: Int selection
- Precondition: Must be called
- Postcondition: Calls option stuff
- *************************************************/
- void optionMenuExec(int selection)
- {
- enum menutitles{ EXIT, CURR, DELETE };
- switch (selection)
- {
- case EXIT:
- exit(1);
- break;
- case CURR:
- convertUSDConfig();
- break;
- case DELETE:
- string prompt;
- cout << "\n|Are you sure you want to delete all files permanently? Type \"Yes\" to continue.\n-->|";
- cin >> prompt;
- if (prompt == "yes" || prompt == "Yes" || prompt == "yEs" || prompt == "yeS" || prompt == "YEs" || prompt == "YES" || prompt == "YeS")
- {
- deleteAllFiles();
- }
- else
- {
- cout << "No files have been deleted. Exiting now.\n";
- }
- system("pause");
- exit(1);
- break;
- }
- return;
- }
- /*************************************************
- Function: deleteAllFiles
- Description: This deletes all of the txt files
- Input: No input
- Precondition: Must have existing files to delete
- Postcondition: deletes sales.txt, salesUSD.txt, names.txt, and config.txt
- *************************************************/
- void deleteAllFiles()
- {
- //This deletes all created files.
- remove("sales.txt");
- remove("salesUSD.txt");
- remove("names.txt");
- remove("config.txt");
- cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n==========================================\nFiles Deleted.\n==========================================\nExiting.\n";
- system("pause");
- exit(1);
- return;
- }
- /*************************************************
- Function: convertUSD
- Description: Converts all values in salesUSD.txt, and saves them to sales.txt
- Input: all the values from sales.txt
- Precondition: Files must contain double values
- Postcondition: overwrites sales.txt with conversion numbers
- *************************************************/
- void convertUSD()
- {
- //This array obtains the value of the second number in config
- int currencyOption[2];
- //This reads in the salary amount
- double salary[100];
- //Must be -1, or else random numbers will be printed out to sales.txt
- int entries = -1;
- //this will read in the first two values of config. The first is irrelevant
- ifstream readCurrencyOption;
- readCurrencyOption.open("config.txt");
- //DO NOT TOUCH INDEX 0. THAT VALUE MUST STAY -1 FOR THE ENTIRE LIFE OF THIS PROJECT
- for (int c = 0; c < 2; c++)
- {
- readCurrencyOption >> currencyOption[c];
- }
- readCurrencyOption.close();
- ifstream readSalaries;
- readSalaries.open("salesUSD.txt", ios::app);
- int index = 0;
- //This stores the nonconverted USD data into salary[]
- do
- {
- readSalaries >> salary[index];
- entries++;
- index++;
- } while (!readSalaries.eof());
- readSalaries.close();
- //this will writeout to sales.txt. This is not appeneded.
- ofstream convertSalaries;
- convertSalaries.open("sales.txt");
- index = 0;
- if (currencyOption[1] == 1)
- {
- for (int c = 0; c < entries; c++)
- {
- convertSalaries << salary[index] * USD << endl;
- index++;
- }
- }
- //Canadian Dollar
- else if (currencyOption[1] == 2)
- {
- for (int c = 0; c < entries; c++)
- {
- convertSalaries << salary[index] * CAD << endl;
- index++;
- }
- }
- //Bitcoin
- else if (currencyOption[1] == 3)
- {
- for (int c = 0; c < entries; c++)
- {
- convertSalaries << salary[index] * BTC << endl;
- index++;
- }
- }
- //British Pound
- else if (currencyOption[1] == 4)
- {
- for (int c = 0; c < entries; c++)
- {
- convertSalaries << salary[index] * GBP << endl;
- index++;
- }
- }
- convertSalaries.close();
- return;
- }
- /*************************************************
- Function: convertUSDConfig
- Description: A menu to run the different conversions to save to config.txt
- Input: user input 1-3 and x||X
- Precondition: sales.txt must exist
- Postcondition: runs one of the conversion functions
- *************************************************/
- void convertUSDConfig()
- {
- enum currencies{EXIT, USD, CAD, BTC, GBP};
- int control = getConvertUSDConfigSelection();
- switch (control)
- {
- case EXIT:
- exit(1);
- break;
- case USD:
- configUSD();
- break;
- case CAD:
- configCAD();
- break;
- case BTC:
- configBTC();
- break;
- case GBP:
- configGBP();
- break;
- }
- return;
- }
- /*************************************************
- Function: getConvertUSDConfigSelection
- Description: This prompts the user for a selection, to be returned to convertUSDConfig
- Input: user input 1-4 and x||X
- Precondition: must be called and set to an int
- Postcondition: returns an integer that the user typed in, or 0 if the user entered in x||X
- *************************************************/
- int getConvertUSDConfigSelection()
- {
- char selection = ' ';
- do
- {
- cout.setf(ios::left);
- cout << setw(40) << "Which Currency would you like to use?" << endl;
- cout << setw(40) << "|USD (Default)" << setw(10) << "| 1" << endl;
- cout << setw(40) << "|Canadian Dollar" << setw(10) << "| 2" << endl;
- cout << setw(40) << "|Bitcoin" << setw(10) << "| 3" << endl;
- cout << setw(40) << "|British Pound" << setw(10) << "| 4" << endl;
- cout << setw(40) << "|Exit" << setw(10) << "| X" << endl;
- cout << "-->|";
- cin >> selection;
- cout << endl;
- }
- //If an invalid char is entered, it will repeat. This allows for functions to not worry about values out of bounds
- while (!(selection == '1' || selection == '2' || selection == '3' || selection == '4' || selection == 'X' || selection == 'x'));
- //converts to int before returning
- int selectionInt = charToInt(selection);
- return selectionInt;
- }
- /*************************************************
- CONFIG CONVERSIONS BELOW
- Functions: configUSD, configCAD, configBTC, and configGBP
- description: overwrites the 2nd value in config.txt, to be read by loadOptions.
- US Dollar = 1
- Canadian Dollar = 2
- Bitcoin = 3
- British Pound = 4
- *************************************************/
- void configUSD()
- {
- ofstream editConfig;
- ifstream readConfig;
- readConfig.open("config.txt");
- int options[4];
- for (int c = 0; c < 4; c++)
- {
- //This saves the config numbers into options array. Only index 1 is being touched. The rest are stored, and rewritten.
- readConfig >> options[c];
- }
- readConfig.close();
- editConfig.open("config.txt");
- for (int c = 0; c < 4; c++)
- {
- //This rewrites the config, only changing index 1
- if (c != 1)
- {
- editConfig << options[c] << endl;
- }
- else
- {
- // =USD=
- editConfig << "1" << endl;
- }
- }
- editConfig.close();
- loadOptions();
- return;
- }
- void configCAD()
- {
- ofstream editConfig;
- ifstream readConfig;
- readConfig.open("config.txt");
- int options[4];
- for (int c = 0; c < 4; c++)
- {
- //This saves the config numbers into options array. Only index 1 is being touched. The rest are stored, and rewritten.
- readConfig >> options[c];
- }
- readConfig.close();
- editConfig.open("config.txt");
- for (int c = 0; c < 4; c++)
- {
- //This rewrites the config, only changing index 1
- if (c != 1)
- {
- editConfig << options[c] << endl;
- }
- else
- {
- // =CAD=
- editConfig << "2" << endl;
- }
- }
- editConfig.close();
- loadOptions();
- return;
- }
- void configBTC()
- {
- ofstream editConfig;
- ifstream readConfig;
- readConfig.open("config.txt");
- int options[4];
- for (int c = 0; c < 4; c++)
- {
- //This saves the config numbers into options array. Only index 1 is being touched. The rest are stored, and rewritten.
- readConfig >> options[c];
- }
- readConfig.close();
- editConfig.open("config.txt");
- for (int c = 0; c < 4; c++)
- {
- //This rewrites the config, only changing index 1
- if (c != 1)
- {
- editConfig << options[c] << endl;
- }
- else
- {
- // =BTC=
- editConfig << "3" << endl;
- }
- }
- editConfig.close();
- loadOptions();
- return;
- }
- void configGBP()
- {
- ofstream editConfig;
- ifstream readConfig;
- readConfig.open("config.txt");
- int options[4];
- for (int c = 0; c < 4; c++)
- {
- //This saves the config numbers into options array. Only index 1 is being touched. The rest are stored, and rewritten.
- readConfig >> options[c];
- }
- readConfig.close();
- editConfig.open("config.txt");
- for (int c = 0; c < 4; c++)
- {
- //This rewrites the config, only changing index 1
- if (c != 1)
- {
- editConfig << options[c] << endl;
- }
- else
- {
- // =GBP=
- editConfig << "4" << endl;
- }
- }
- editConfig.close();
- loadOptions();
- return;
- }
- string getSign()
- {
- int currencyOption[2];
- ifstream readCurrencyOption;
- readCurrencyOption.open("config.txt");
- //DO NOT TOUCH INDEX 0. THAT VALUE MUST STAY -1 FOR THE ENTIRE LIFE OF THIS PROJECT
- for (int c = 0; c < 2; c++)
- {
- readCurrencyOption >> currencyOption[c];
- }
- if (currencyOption[1] == 1)
- {
- return "$";
- }
- else if (currencyOption[1] == 2)
- {
- return "$";
- }
- else if (currencyOption[1] == 3)
- {
- return "BTC";
- }
- else if (currencyOption[1] == 4)
- {
- return "GBP";
- }
- readCurrencyOption.close();
- }
- /*
- CODE REUSE
- Read currency option in config
- ****************************************************
- int currencyOption[2];
- ifstream readCurrencyOption;
- readCurrencyOption.open("config.txt");
- //DO NOT TOUCH INDEX 0. THAT VALUE MUST STAY -1 FOR THE ENTIRE LIFE OF THIS PROJECT
- for (int c = 0; c < 2; c++)
- {
- readCurrencyOption >> currencyOption[c];
- }
- readCurrencyOption.close();
- *************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement