Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include <iomanip>
- #include <string>
- #include <ctype.h>
- #include <fstream>
- #include <Windows.h>
- using namespace std;
- struct monthsSales
- {
- int salesmanNo;
- char forename[20], surname[20];
- int sales[12];
- double average;
- double bonus;
- };
- //Prototypes - common functions
- void gotoXY(short x, short y);
- void pressKey(int col, int row);
- void message(string mess, int col, int row);
- char again(int col, int row);
- int getOption(int c, int r, int min, int max);
- char * toUpperCase(char s1[]);
- void salesHeadings(int row);
- void printRecord(monthsSales sales, int row);
- void clearLine(int col, int row);
- //Prototypes - menus
- void mainMenu();
- void displayMenu();
- void reportMenu();
- void statisticsMenu();
- void populateSalesmanDetails();
- void updateMenu();
- void Update();
- void updateSalesmenDetails();
- //Prototypes - menu process functions
- void addSales();
- int main()
- {
- /*populateSalesmanDetails();*/
- int mOpt, dOpt, sOpt, rOpt;
- do
- {
- mainMenu();
- mOpt = getOption(45, 20, 1, 5);
- switch (mOpt)
- {
- case 1:
- addSales();
- break;
- case 2:
- break;
- case 3:
- Update();
- break;
- case 4:
- reportMenu();
- break;
- }
- } while (mOpt != 5);
- return 0;
- }
- //void populateSalesmanDetails()
- //{
- // ofstream out;
- //
- //
- // out.open("SalesDetails.dat", ios::binary | ios::app);
- //
- // for (int i = 0; i < 1; i++)
- // {
- // out.write((char *)& salesmanDetails[i], sizeof(monthsSales));
- // }
- //
- // if (out.good())
- // message("Records Added to File", 8, 18);
- //
- // out.close();
- //}
- void message(string mess, int col, int row)
- {
- gotoXY(col, row);
- cout << mess;
- pressKey(col, row + 2);
- }//end of message
- void pressKey(int col, int row)
- {
- gotoXY(col, row);
- cout << "Press any key to continue...";
- _getch();
- }
- void gotoXY(short x, short y)
- {
- COORD c = { x, y };
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
- }
- char again(int col, int row)
- {
- char reply;
- do
- {
- gotoXY(col, row);
- cin >> noskipws >> reply;
- reply = toupper(reply);
- if ((reply != 'Y' && reply != 'N'))
- {
- message("Must Enter Y or N", 5, row + 3);
- clearLine(col, row);
- // cin.clear();
- cin.ignore(150, '\n');
- }
- cin.setf(ios::skipws);
- } while (reply != 'Y' && reply != 'N');
- return reply;
- }
- int getOption(int c, int r, int min, int max)
- {
- int x;
- gotoXY(c, r);
- cin >> x;
- cin.ignore(10, '\n');
- while ((x< min) || (x> max))
- {
- message("Invalid Option", 20, 22);
- gotoXY(c, r);
- cout << "_";
- gotoXY(c, r);
- cin >> x;
- cin.ignore(10, '\n');
- }
- return x;
- }
- char* toUpperCase(char s1[]) //Change to Upper Case
- {
- for (int i = 0; i < strlen(s1); i++)
- {
- s1[i] = toupper(s1[i]);
- }
- return s1;
- }
- void salesHeadings(int row) //output headings
- {
- gotoXY(1, row); cout << "Salesman";
- gotoXY(10, row); cout << "forename";
- gotoXY(31, row); cout << "Surname";
- gotoXY(52, row); cout << "Sales";
- gotoXY(61, row); cout << "Average";
- gotoXY(70, row); cout << "Bonus";
- row++;
- gotoXY(1, row); cout << "No";
- gotoXY(52, row); cout << "Figures";
- gotoXY(61, row); cout << "numbers sold";
- gotoXY(75, row); cout << "% of profit";
- }
- void printRecord(monthsSales sales, int row)
- {
- gotoXY(1, row); cout << sales.salesmanNo;
- gotoXY(10, row); cout << sales.forename;
- gotoXY(31, row); cout << sales.surname;
- gotoXY(52, row); cout << sales.sales;
- gotoXY(61, row); cout << sales.average;
- gotoXY(75, row); cout << sales.bonus;
- }
- void clearLine(int col, int row)
- {
- //Used to clear prompts and user input
- gotoXY(col, row);
- for (int i = col; i <= 80; i++)
- {
- cout << " ";
- }
- }
- void mainMenu()
- {
- system("cls");
- gotoXY(25, 4); cout << "M A I N M E N U";
- gotoXY(20, 7); cout << "1. Add Salesman Figures";
- gotoXY(20, 9); cout << "2. Display Salesman Yearly Figures ";
- gotoXY(20, 11); cout << "3. Update Salesman Figures";
- gotoXY(20, 13); cout << "4. Reports";
- gotoXY(20, 15); cout << "5. Exit";
- gotoXY(20, 20); cout << "Enter Option (1 - 5) [_]";
- }
- void displayMenu()
- {
- system("cls");
- gotoXY(25, 4); cout << "D I S P L A Y M E N U";
- gotoXY(20, 7); cout << "1. Display All Salesmen";
- gotoXY(20, 11); cout << "2. Exit";
- gotoXY(20, 14); cout << "Enter Option (1 - 2) [_]";
- }
- void reportMenu()
- {
- system("cls");
- gotoXY(25, 4); cout << "R E P O R T S M E N U";
- gotoXY(20, 7); cout << "1. List of salesmen with monthly sales figures, average and bonus.";
- gotoXY(20, 9); cout << "2. Statistics";
- gotoXY(20, 11); cout << "3. Exit";
- gotoXY(20, 14); cout << "Enter Option (1 - 3) [_]";
- }
- void statisticsMenu()
- {
- system("cls");
- gotoXY(25, 4); cout << "S T A T I S T I C S R E P O R T S M E N U";
- gotoXY(20, 7); cout << "1. Number of Salesmen";
- gotoXY(20, 9); cout << "2. Statistics";
- gotoXY(20, 11); cout << "3. Exit";
- gotoXY(20, 14); cout << "Enter Option (1 - 3) [_]";
- }
- void updateMenu()
- {
- system("cls");
- gotoXY(8, 4); cout << "U P D A T E M E N U";
- gotoXY(8, 6); cout << "What Would You Like to Update? ";
- gotoXY(8, 8); cout << "1. Update a Salesmen's details";
- gotoXY(8, 10); cout << "2. Exit";
- gotoXY(8, 12); cout << "Enter Option (1 - 2) [_]";
- }
- void updateOptions()
- {
- gotoXY(8, 4); cout << "U P D A T E O P T I O N S ";
- gotoXY(8, 6); cout << "What Would You Like to Update? ";
- gotoXY(8, 7); cout << "1. forename";
- gotoXY(8, 9); cout << "2. Surname";
- gotoXY(8, 11); cout << "3. Sales Figures";
- gotoXY(8, 17); cout << "4. Exit";
- gotoXY(8, 20); cout << "Enter Option (1 - 4) [_]";
- }
- void Update()
- {
- int mOpt;
- do
- {
- updateMenu();
- mOpt = getOption(45, 11, 1, 2);
- switch (mOpt)
- {
- case 1:
- updateSalesmenDetails();
- break;
- }
- } while (mOpt != 2);
- }
- void addSales()
- {
- ifstream in;
- ofstream out;
- monthsSales sales;
- bool valid = false;
- bool present = false;
- int row = 4;
- system("cls");
- out.open("SalesDetails.dat", ios::binary | ios::app);
- cout << "\n\tAdd Sales Details";
- do
- {
- in.open("SalesDetails.dat", ios::binary);
- int salesmanNo = 0;
- gotoXY(3, 3);
- cout << "Please enter Salesman Number: ";
- gotoXY(36, 3);
- cin >> sales.salesmanNo;
- while (!in.eof())
- {
- in.read((char*)& sales, sizeof(monthsSales));
- if (salesmanNo = sales.salesmanNo)
- {
- present = true;
- in.close();
- gotoXY(3, 5);
- cout << "Salesman Number is present, please enter another Salesman Number.";
- pressKey(3, 7);
- clearLine(36, 3);
- clearLine(3, 5);
- clearLine(3, 7);
- cin.clear();
- break;
- }
- }
- if (cin.fail())
- {
- gotoXY(3, 5);
- cout << "Invalid number please enter another value. ";
- pressKey(3, 7);
- clearLine(3, 5);
- clearLine(3, 7);
- cin.clear();
- clearLine(33, 3);
- }
- else if (salesmanNo < 0)
- {
- gotoXY(3, 5);
- cout << "Invalid number please enter another value. ";
- pressKey(3, 7);
- clearLine(3, 5);
- clearLine(3, 7);
- cin.clear();
- clearLine(33, 3);
- }
- else
- {
- valid = true;
- sales.salesmanNo = salesmanNo;
- }
- cin.ignore(10, '\n');
- } while (!valid || present);
- gotoXY(3, 5);
- cout << "Please enter details for forename ";
- do
- {
- string forename;
- gotoXY(40, 5);
- cin.getline(sales.forename, 20, '\n');
- for (int i = 0; i < forename.length(); i++)
- {
- forename[i] = toupper(forename[i]);
- }
- if (cin.fail())
- {
- gotoXY(3, 7);
- cout << "Invalid input please re-enter.";
- pressKey(3, 9);
- clearLine(3, 7);
- clearLine(3, 9);
- cin.clear();
- clearLine(40, 5);
- }
- else if (forename.length() > 20)
- {
- gotoXY(3, 7);
- cout << "Invalid input please re-enter. (Input must be less than 20 characters)";
- pressKey(3, 9);
- clearLine(3, 7);
- clearLine(3, 9);
- cin.clear();
- clearLine(40, 5);
- }
- else
- {
- valid = true;
- forename = sales.forename;
- }
- } while (!valid);
- gotoXY(3, 7);
- cout << "Please enter details for surname";
- do
- {
- string surname;
- gotoXY(39, 7);
- cin.getline(sales.surname, 20, '\n');
- for (int i = 0; i < surname.length(); i++)
- {
- surname[i] = toupper(surname[i]);
- }
- if (cin.fail())
- {
- gotoXY(3, 9);
- cout << "Invalid input please re-enter.";
- pressKey(3, 11);
- clearLine(3, 9);
- clearLine(3, 11);
- cin.clear();
- clearLine(39, 7);
- }
- else if (surname.length() > 20)
- {
- gotoXY(3, 9);
- cout << "Invalid input please re-enter. (Input must be less than 20 characters)";
- pressKey(3, 11);
- clearLine(3, 9);
- clearLine(3, 11);
- cin.clear();
- clearLine(39, 7);
- }
- else
- {
- valid = true;
- surname = sales.surname;
- }
- } while (!valid);
- int sale[12] = { 0 };
- double totalSales = 0.0;
- string month[12] = { "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
- for (int i = 0; i < 12; i++)
- {
- do
- {
- gotoXY(3, 9);
- cout << "Please enter sales made in " << month[i] << ":";
- gotoXY(41, 9);
- cin >> sale[i];
- if (cin.fail())
- {
- gotoXY(3, 11);
- cout << "Invalid input please re-enter.";
- pressKey(3, 13);
- clearLine(3, 11);
- clearLine(3, 13);
- cin.clear();
- clearLine(41, 9);
- cin.ignore(150, '\n');
- }
- else if (sale[i] < 0)
- {
- bool valid = false;
- gotoXY(3, 11);
- cout << "Invalid input please enter Sales greater than 0. ";
- pressKey(3, 13);
- clearLine(3, 11);
- clearLine(3, 13);
- clearLine(41, 9);
- cin.clear();
- cin.ignore(150, '\n');
- }
- else
- {
- bool valid = true;
- clearLine(41, 9);
- }
- } while (valid = false);
- totalSales += sale[i];
- sales.average = totalSales / 12;
- }
- if (sales.average < 200)
- {
- sales.bonus = totalSales * 0;
- }
- if (sales.average >= 200 && sales.average < 600)
- {
- sales.bonus = totalSales * 0.01;
- }
- if (sales.average >= 600 && sales.average < 1000)
- {
- sales.bonus = totalSales * 0.04;
- }
- if (sales.average >= 1000)
- {
- sales.bonus = totalSales * 0.075;
- }
- out.write((char*)& sales, sizeof(monthsSales));
- if (out.good())
- {
- message("Records added to file", 3, 18);
- }
- out.close();
- }
- void updateSalesmenDetails()
- {
- monthsSales sales;
- fstream update;
- int salesmanNo, uOpt = 0, position = 0;
- bool present = false;
- bool valid = false;
- char ans;
- update.open("SalesDetails.dat", ios::in | ios::out | ios::binary);
- system("cls");
- gotoXY(3, 3);
- cout << "Please enter Salesman No: ";
- cin >> salesmanNo;
- //Condition Output stream for floating point values
- cout << fixed << setprecision(2);
- while (!present && !update.eof())
- {
- update.read((char *)& sales, sizeof(monthsSales));
- if (update.good())
- {
- if (sales.salesmanNo = salesmanNo)
- {
- system("cls");
- present = true;
- salesHeadings(10);
- printRecord(sales, 13);
- pressKey(3, 24);
- do
- {
- updateOptions();
- uOpt = getOption(33, 20, 1, 4);
- switch (uOpt)
- {
- case 1:
- system("cls");
- salesHeadings(10);
- printRecord(sales, 13);
- gotoXY(3, 26); cout << "Please enter the updated forename: ";
- do
- {
- string forename;
- gotoXY(37, 26);
- cin.getline(sales.forename, 20, '\n');
- for (int i = 0; i < forename.length(); i++)
- {
- forename[i] = toupper(forename[i]);
- }
- if (cin.fail())
- {
- gotoXY(3, 28);
- cout << "Invalid input please re-enter.";
- pressKey(3, 29);
- clearLine(3, 28);
- clearLine(3, 29);
- cin.clear();
- clearLine(37, 26);
- }
- else if (forename.length() > 20)
- {
- gotoXY(3, 24);
- cout << "Invalid input please re-enter. (Input must be less than 20 characters)";
- pressKey(3, 26);
- clearLine(3, 24);
- clearLine(3, 26);
- cin.clear();
- clearLine(37, 22);
- }
- else
- {
- valid = true;
- forename == sales.forename;
- }
- } while (!valid);
- break;
- case 2:
- system("cls");
- salesHeadings(10);
- printRecord(sales, 13);
- gotoXY(8, 22); cout << "Please enter updated surname: ";
- do
- {
- string surname;
- gotoXY(37, 22);
- cin.getline(sales.surname, 20, '\n');
- for (int i = 0; i < surname.length(); i++)
- {
- surname[i] = toupper(surname[i]);
- }
- if (cin.fail())
- {
- gotoXY(3, 24);
- cout << "Invalid input please re-enter.";
- pressKey(3, 26);
- clearLine(3, 24);
- clearLine(3, 26);
- cin.clear();
- clearLine(37, 22);
- }
- else if (surname.length() > 20)
- {
- gotoXY(3, 24);
- cout << "Invalid input please re-enter. (Input must be less than 20 characters)";
- pressKey(3, 26);
- clearLine(3, 24);
- clearLine(3, 26);
- cin.clear();
- clearLine(37, 22);
- }
- else
- {
- valid = true;
- surname == sales.surname;
- }
- } while (!valid);
- break;
- case 3:
- int sale[12] = { 0 };
- double newTotalSales = 0.0;
- string month[12] = { "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
- for (int i = 0; i < 12; i++)
- {
- do
- {
- system("cls");
- salesHeadings(10);
- printRecord(sales, 13);
- gotoXY(3, 22);
- cout << "Please enter sales made in " << month[i] << ":";
- gotoXY(41, 22);
- cin >> sale[i];
- if (cin.fail())
- {
- gotoXY(3, 24);
- cout << "Invalid input please re-enter.";
- pressKey(3, 26);
- clearLine(3, 24);
- clearLine(3, 26);
- cin.clear();
- clearLine(41, 22);
- i--;
- newTotalSales -= sale[i];
- cin.ignore(150, '\n');
- }
- else if (sale[i] < 0)
- {
- bool valid = false;
- gotoXY(3, 24);
- cout << "Invalid input please enter Sales greater than 0. ";
- pressKey(3, 26);
- clearLine(3, 24);
- clearLine(3, 26);
- clearLine(41, 22);
- cin.clear();
- i--;
- newTotalSales -= sale[i];
- cin.ignore(150, '\n');
- }
- else
- {
- bool valid = true;
- clearLine(41, 22);
- }
- } while (valid = false);
- newTotalSales += sale[i];
- sales.average = newTotalSales / 12;
- }
- if (sales.average < 200)
- {
- sales.bonus = newTotalSales * 0;
- }
- if (sales.average >= 200 && sales.average < 600)
- {
- sales.bonus = newTotalSales * 0.01;
- }
- if (sales.average >= 600 && sales.average < 1000)
- {
- sales.bonus = newTotalSales * 0.04;
- }
- if (sales.average >= 1000)
- {
- sales.bonus = newTotalSales * 0.075;
- }
- break;
- };
- } while (uOpt != 4);
- update.seekg((long double)-1 * sizeof(monthsSales), ios::cur);
- update.write((char *)& sales, sizeof(monthsSales));
- if (update.good())
- {
- system("cls");
- message("Sales Details have been updated", 8, 4);
- }
- }
- }
- }
- update.clear();
- update.seekg((long double)0, ios::beg);
- if (!present)
- {
- system("cls");
- message("Invalid Salesman No!", 8, 4);
- }
- update.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement