Advertisement
Guest User

MARKS

a guest
Jan 23rd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.73 KB | None | 0 0
  1.  
  2.  
  3. #include "stdafx.h"
  4. #include <iostream>
  5. #include <conio.h>
  6. #include <iomanip>
  7. #include <string>
  8. #include <ctype.h>
  9. #include <fstream>
  10. #include <Windows.h>
  11.  
  12. using namespace std;
  13.  
  14. struct monthsSales
  15. {
  16. int salesmanNo;
  17. char forename[20], surname[20];
  18. int sales[12];
  19. double average;
  20. double bonus;
  21. };
  22.  
  23. //Prototypes - common functions
  24. void gotoXY(short x, short y);
  25. void pressKey(int col, int row);
  26. void message(string mess, int col, int row);
  27. char again(int col, int row);
  28. int getOption(int c, int r, int min, int max);
  29. char * toUpperCase(char s1[]);
  30. void salesHeadings(int row);
  31. void printRecord(monthsSales sales, int row);
  32. void clearLine(int col, int row);
  33.  
  34. //Prototypes - menus
  35. void mainMenu();
  36. void displayMenu();
  37. void reportMenu();
  38. void statisticsMenu();
  39. void populateSalesmanDetails();
  40. void updateMenu();
  41. void Update();
  42. void updateSalesmenDetails();
  43.  
  44. //Prototypes - menu process functions
  45. void addSales();
  46.  
  47.  
  48.  
  49.  
  50.  
  51. int main()
  52. {
  53. /*populateSalesmanDetails();*/
  54. int mOpt, dOpt, sOpt, rOpt;
  55. do
  56. {
  57. mainMenu();
  58. mOpt = getOption(45, 20, 1, 5);
  59.  
  60. switch (mOpt)
  61. {
  62. case 1:
  63. addSales();
  64. break;
  65. case 2:
  66.  
  67. break;
  68. case 3:
  69. Update();
  70. break;
  71. case 4:
  72. reportMenu();
  73. break;
  74. }
  75. } while (mOpt != 5);
  76.  
  77. return 0;
  78. }
  79.  
  80.  
  81.  
  82. //void populateSalesmanDetails()
  83. //{
  84. // ofstream out;
  85. //
  86. //
  87. // out.open("SalesDetails.dat", ios::binary | ios::app);
  88. //
  89. // for (int i = 0; i < 1; i++)
  90. // {
  91. // out.write((char *)& salesmanDetails[i], sizeof(monthsSales));
  92. // }
  93. //
  94. // if (out.good())
  95. // message("Records Added to File", 8, 18);
  96. //
  97. // out.close();
  98. //}
  99.  
  100. void message(string mess, int col, int row)
  101. {
  102. gotoXY(col, row);
  103. cout << mess;
  104. pressKey(col, row + 2);
  105. }//end of message
  106.  
  107. void pressKey(int col, int row)
  108. {
  109. gotoXY(col, row);
  110. cout << "Press any key to continue...";
  111. _getch();
  112. }
  113.  
  114. void gotoXY(short x, short y)
  115. {
  116. COORD c = { x, y };
  117. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
  118. }
  119.  
  120. char again(int col, int row)
  121. {
  122. char reply;
  123.  
  124. do
  125. {
  126. gotoXY(col, row);
  127. cin >> noskipws >> reply;
  128. reply = toupper(reply);
  129.  
  130. if ((reply != 'Y' && reply != 'N'))
  131. {
  132. message("Must Enter Y or N", 5, row + 3);
  133. clearLine(col, row);
  134.  
  135. // cin.clear();
  136. cin.ignore(150, '\n');
  137. }
  138.  
  139. cin.setf(ios::skipws);
  140.  
  141. } while (reply != 'Y' && reply != 'N');
  142.  
  143. return reply;
  144. }
  145.  
  146. int getOption(int c, int r, int min, int max)
  147. {
  148. int x;
  149.  
  150. gotoXY(c, r);
  151. cin >> x;
  152. cin.ignore(10, '\n');
  153. while ((x< min) || (x> max))
  154. {
  155. message("Invalid Option", 20, 22);
  156. gotoXY(c, r);
  157. cout << "_";
  158. gotoXY(c, r);
  159. cin >> x;
  160. cin.ignore(10, '\n');
  161. }
  162. return x;
  163. }
  164.  
  165. char* toUpperCase(char s1[]) //Change to Upper Case
  166. {
  167. for (int i = 0; i < strlen(s1); i++)
  168. {
  169. s1[i] = toupper(s1[i]);
  170. }
  171.  
  172. return s1;
  173. }
  174.  
  175. void salesHeadings(int row) //output headings
  176. {
  177. gotoXY(1, row); cout << "Salesman";
  178. gotoXY(10, row); cout << "forename";
  179. gotoXY(31, row); cout << "Surname";
  180. gotoXY(52, row); cout << "Sales";
  181. gotoXY(61, row); cout << "Average";
  182. gotoXY(70, row); cout << "Bonus";
  183.  
  184. row++;
  185.  
  186. gotoXY(1, row); cout << "No";
  187. gotoXY(52, row); cout << "Figures";
  188. gotoXY(61, row); cout << "numbers sold";
  189. gotoXY(75, row); cout << "% of profit";
  190. }
  191.  
  192. void printRecord(monthsSales sales, int row)
  193. {
  194. gotoXY(1, row); cout << sales.salesmanNo;
  195. gotoXY(10, row); cout << sales.forename;
  196. gotoXY(31, row); cout << sales.surname;
  197. gotoXY(52, row); cout << sales.sales;
  198. gotoXY(61, row); cout << sales.average;
  199. gotoXY(75, row); cout << sales.bonus;
  200. }
  201.  
  202. void clearLine(int col, int row)
  203. {
  204. //Used to clear prompts and user input
  205. gotoXY(col, row);
  206.  
  207. for (int i = col; i <= 80; i++)
  208. {
  209. cout << " ";
  210. }
  211. }
  212.  
  213. void mainMenu()
  214. {
  215. system("cls");
  216. gotoXY(25, 4); cout << "M A I N M E N U";
  217. gotoXY(20, 7); cout << "1. Add Salesman Figures";
  218. gotoXY(20, 9); cout << "2. Display Salesman Yearly Figures ";
  219. gotoXY(20, 11); cout << "3. Update Salesman Figures";
  220. gotoXY(20, 13); cout << "4. Reports";
  221. gotoXY(20, 15); cout << "5. Exit";
  222. gotoXY(20, 20); cout << "Enter Option (1 - 5) [_]";
  223. }
  224.  
  225. void displayMenu()
  226. {
  227. system("cls");
  228. gotoXY(25, 4); cout << "D I S P L A Y M E N U";
  229. gotoXY(20, 7); cout << "1. Display All Salesmen";
  230. gotoXY(20, 11); cout << "2. Exit";
  231. gotoXY(20, 14); cout << "Enter Option (1 - 2) [_]";
  232.  
  233. }
  234.  
  235. void reportMenu()
  236. {
  237. system("cls");
  238. gotoXY(25, 4); cout << "R E P O R T S M E N U";
  239. gotoXY(20, 7); cout << "1. List of salesmen with monthly sales figures, average and bonus.";
  240. gotoXY(20, 9); cout << "2. Statistics";
  241. gotoXY(20, 11); cout << "3. Exit";
  242. gotoXY(20, 14); cout << "Enter Option (1 - 3) [_]";
  243.  
  244. }
  245.  
  246. void statisticsMenu()
  247. {
  248. system("cls");
  249. 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";
  250. gotoXY(20, 7); cout << "1. Number of Salesmen";
  251. gotoXY(20, 9); cout << "2. Statistics";
  252. gotoXY(20, 11); cout << "3. Exit";
  253. gotoXY(20, 14); cout << "Enter Option (1 - 3) [_]";
  254. }
  255.  
  256. void updateMenu()
  257. {
  258. system("cls");
  259. gotoXY(8, 4); cout << "U P D A T E M E N U";
  260. gotoXY(8, 6); cout << "What Would You Like to Update? ";
  261. gotoXY(8, 8); cout << "1. Update a Salesmen's details";
  262. gotoXY(8, 10); cout << "2. Exit";
  263. gotoXY(8, 12); cout << "Enter Option (1 - 2) [_]";
  264.  
  265.  
  266. }
  267. void updateOptions()
  268. {
  269. gotoXY(8, 4); cout << "U P D A T E O P T I O N S ";
  270. gotoXY(8, 6); cout << "What Would You Like to Update? ";
  271. gotoXY(8, 7); cout << "1. forename";
  272. gotoXY(8, 9); cout << "2. Surname";
  273. gotoXY(8, 11); cout << "3. Sales Figures";
  274. gotoXY(8, 17); cout << "4. Exit";
  275. gotoXY(8, 20); cout << "Enter Option (1 - 4) [_]";
  276. }
  277.  
  278. void Update()
  279. {
  280. int mOpt;
  281. do
  282. {
  283. updateMenu();
  284. mOpt = getOption(45, 11, 1, 2);
  285. switch (mOpt)
  286. {
  287. case 1:
  288. updateSalesmenDetails();
  289. break;
  290. }
  291.  
  292. } while (mOpt != 2);
  293. }
  294.  
  295. void addSales()
  296. {
  297. ifstream in;
  298. ofstream out;
  299. monthsSales sales;
  300. bool valid = false;
  301. bool present = false;
  302. int row = 4;
  303. system("cls");
  304.  
  305. out.open("SalesDetails.dat", ios::binary | ios::app);
  306.  
  307. cout << "\n\tAdd Sales Details";
  308.  
  309. do
  310. {
  311. in.open("SalesDetails.dat", ios::binary);
  312. int salesmanNo = 0;
  313. gotoXY(3, 3);
  314. cout << "Please enter Salesman Number: ";
  315. gotoXY(36, 3);
  316. cin >> sales.salesmanNo;
  317. while (!in.eof())
  318. {
  319. in.read((char*)& sales, sizeof(monthsSales));
  320. if (salesmanNo = sales.salesmanNo)
  321. {
  322. present = true;
  323. in.close();
  324. gotoXY(3, 5);
  325. cout << "Salesman Number is present, please enter another Salesman Number.";
  326. pressKey(3, 7);
  327. clearLine(36, 3);
  328. clearLine(3, 5);
  329. clearLine(3, 7);
  330. cin.clear();
  331. break;
  332. }
  333. }
  334. if (cin.fail())
  335. {
  336. gotoXY(3, 5);
  337. cout << "Invalid number please enter another value. ";
  338. pressKey(3, 7);
  339. clearLine(3, 5);
  340. clearLine(3, 7);
  341. cin.clear();
  342. clearLine(33, 3);
  343. }
  344. else if (salesmanNo < 0)
  345. {
  346. gotoXY(3, 5);
  347. cout << "Invalid number please enter another value. ";
  348. pressKey(3, 7);
  349. clearLine(3, 5);
  350. clearLine(3, 7);
  351. cin.clear();
  352. clearLine(33, 3);
  353. }
  354. else
  355. {
  356. valid = true;
  357. sales.salesmanNo = salesmanNo;
  358.  
  359. }
  360. cin.ignore(10, '\n');
  361. } while (!valid || present);
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368. gotoXY(3, 5);
  369. cout << "Please enter details for forename ";
  370.  
  371. do
  372. {
  373. string forename;
  374. gotoXY(40, 5);
  375. cin.getline(sales.forename, 20, '\n');
  376. for (int i = 0; i < forename.length(); i++)
  377. {
  378. forename[i] = toupper(forename[i]);
  379. }
  380. if (cin.fail())
  381. {
  382. gotoXY(3, 7);
  383. cout << "Invalid input please re-enter.";
  384. pressKey(3, 9);
  385. clearLine(3, 7);
  386. clearLine(3, 9);
  387. cin.clear();
  388. clearLine(40, 5);
  389. }
  390. else if (forename.length() > 20)
  391. {
  392. gotoXY(3, 7);
  393. cout << "Invalid input please re-enter. (Input must be less than 20 characters)";
  394. pressKey(3, 9);
  395. clearLine(3, 7);
  396. clearLine(3, 9);
  397. cin.clear();
  398. clearLine(40, 5);
  399. }
  400. else
  401. {
  402. valid = true;
  403. forename = sales.forename;
  404. }
  405. } while (!valid);
  406.  
  407.  
  408.  
  409. gotoXY(3, 7);
  410. cout << "Please enter details for surname";
  411. do
  412. {
  413. string surname;
  414. gotoXY(39, 7);
  415. cin.getline(sales.surname, 20, '\n');
  416. for (int i = 0; i < surname.length(); i++)
  417. {
  418. surname[i] = toupper(surname[i]);
  419. }
  420. if (cin.fail())
  421. {
  422. gotoXY(3, 9);
  423. cout << "Invalid input please re-enter.";
  424. pressKey(3, 11);
  425. clearLine(3, 9);
  426. clearLine(3, 11);
  427. cin.clear();
  428. clearLine(39, 7);
  429. }
  430. else if (surname.length() > 20)
  431. {
  432. gotoXY(3, 9);
  433. cout << "Invalid input please re-enter. (Input must be less than 20 characters)";
  434. pressKey(3, 11);
  435. clearLine(3, 9);
  436. clearLine(3, 11);
  437. cin.clear();
  438. clearLine(39, 7);
  439. }
  440. else
  441. {
  442. valid = true;
  443. surname = sales.surname;
  444. }
  445.  
  446. } while (!valid);
  447.  
  448.  
  449. int sale[12] = { 0 };
  450. double totalSales = 0.0;
  451. string month[12] = { "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  452.  
  453. for (int i = 0; i < 12; i++)
  454. {
  455. do
  456. {
  457. gotoXY(3, 9);
  458. cout << "Please enter sales made in " << month[i] << ":";
  459. gotoXY(41, 9);
  460. cin >> sale[i];
  461. if (cin.fail())
  462. {
  463. gotoXY(3, 11);
  464. cout << "Invalid input please re-enter.";
  465. pressKey(3, 13);
  466. clearLine(3, 11);
  467. clearLine(3, 13);
  468. cin.clear();
  469. clearLine(41, 9);
  470. cin.ignore(150, '\n');
  471. }
  472. else if (sale[i] < 0)
  473. {
  474. bool valid = false;
  475. gotoXY(3, 11);
  476. cout << "Invalid input please enter Sales greater than 0. ";
  477. pressKey(3, 13);
  478. clearLine(3, 11);
  479. clearLine(3, 13);
  480. clearLine(41, 9);
  481. cin.clear();
  482. cin.ignore(150, '\n');
  483. }
  484. else
  485. {
  486. bool valid = true;
  487. clearLine(41, 9);
  488.  
  489. }
  490.  
  491. } while (valid = false);
  492.  
  493. totalSales += sale[i];
  494. sales.average = totalSales / 12;
  495. }
  496.  
  497. if (sales.average < 200)
  498. {
  499. sales.bonus = totalSales * 0;
  500. }
  501. if (sales.average >= 200 && sales.average < 600)
  502. {
  503. sales.bonus = totalSales * 0.01;
  504. }
  505. if (sales.average >= 600 && sales.average < 1000)
  506. {
  507. sales.bonus = totalSales * 0.04;
  508. }
  509. if (sales.average >= 1000)
  510. {
  511. sales.bonus = totalSales * 0.075;
  512. }
  513.  
  514. out.write((char*)& sales, sizeof(monthsSales));
  515.  
  516. if (out.good())
  517. {
  518. message("Records added to file", 3, 18);
  519. }
  520. out.close();
  521. }
  522.  
  523.  
  524. void updateSalesmenDetails()
  525. {
  526. monthsSales sales;
  527. fstream update;
  528. int salesmanNo, uOpt = 0, position = 0;
  529. bool present = false;
  530. bool valid = false;
  531. char ans;
  532.  
  533. update.open("SalesDetails.dat", ios::in | ios::out | ios::binary);
  534.  
  535. system("cls");
  536. gotoXY(3, 3);
  537. cout << "Please enter Salesman No: ";
  538. cin >> salesmanNo;
  539.  
  540. //Condition Output stream for floating point values
  541. cout << fixed << setprecision(2);
  542.  
  543. while (!present && !update.eof())
  544. {
  545. update.read((char *)& sales, sizeof(monthsSales));
  546.  
  547. if (update.good())
  548. {
  549. if (sales.salesmanNo = salesmanNo)
  550. {
  551. system("cls");
  552. present = true;
  553. salesHeadings(10);
  554. printRecord(sales, 13);
  555.  
  556.  
  557. pressKey(3, 24);
  558.  
  559. do
  560. {
  561. updateOptions();
  562. uOpt = getOption(33, 20, 1, 4);
  563. switch (uOpt)
  564. {
  565. case 1:
  566. system("cls");
  567. salesHeadings(10);
  568. printRecord(sales, 13);
  569. gotoXY(3, 26); cout << "Please enter the updated forename: ";
  570.  
  571. do
  572. {
  573.  
  574. string forename;
  575.  
  576. gotoXY(37, 26);
  577. cin.getline(sales.forename, 20, '\n');
  578. for (int i = 0; i < forename.length(); i++)
  579. {
  580. forename[i] = toupper(forename[i]);
  581. }
  582. if (cin.fail())
  583. {
  584. gotoXY(3, 28);
  585. cout << "Invalid input please re-enter.";
  586. pressKey(3, 29);
  587. clearLine(3, 28);
  588. clearLine(3, 29);
  589. cin.clear();
  590. clearLine(37, 26);
  591. }
  592. else if (forename.length() > 20)
  593. {
  594. gotoXY(3, 24);
  595. cout << "Invalid input please re-enter. (Input must be less than 20 characters)";
  596. pressKey(3, 26);
  597. clearLine(3, 24);
  598. clearLine(3, 26);
  599. cin.clear();
  600. clearLine(37, 22);
  601. }
  602. else
  603. {
  604. valid = true;
  605. forename == sales.forename;
  606. }
  607. } while (!valid);
  608.  
  609. break;
  610.  
  611.  
  612.  
  613.  
  614. case 2:
  615. system("cls");
  616. salesHeadings(10);
  617. printRecord(sales, 13);
  618. gotoXY(8, 22); cout << "Please enter updated surname: ";
  619.  
  620. do
  621. {
  622.  
  623. string surname;
  624.  
  625. gotoXY(37, 22);
  626. cin.getline(sales.surname, 20, '\n');
  627. for (int i = 0; i < surname.length(); i++)
  628. {
  629. surname[i] = toupper(surname[i]);
  630. }
  631. if (cin.fail())
  632. {
  633. gotoXY(3, 24);
  634. cout << "Invalid input please re-enter.";
  635. pressKey(3, 26);
  636. clearLine(3, 24);
  637. clearLine(3, 26);
  638. cin.clear();
  639. clearLine(37, 22);
  640. }
  641. else if (surname.length() > 20)
  642. {
  643. gotoXY(3, 24);
  644. cout << "Invalid input please re-enter. (Input must be less than 20 characters)";
  645. pressKey(3, 26);
  646. clearLine(3, 24);
  647. clearLine(3, 26);
  648. cin.clear();
  649. clearLine(37, 22);
  650. }
  651. else
  652. {
  653. valid = true;
  654. surname == sales.surname;
  655. }
  656. } while (!valid);
  657. break;
  658.  
  659.  
  660.  
  661.  
  662. case 3:
  663. int sale[12] = { 0 };
  664. double newTotalSales = 0.0;
  665. string month[12] = { "January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
  666.  
  667. for (int i = 0; i < 12; i++)
  668. {
  669. do
  670. {
  671. system("cls");
  672. salesHeadings(10);
  673. printRecord(sales, 13);
  674. gotoXY(3, 22);
  675. cout << "Please enter sales made in " << month[i] << ":";
  676. gotoXY(41, 22);
  677. cin >> sale[i];
  678. if (cin.fail())
  679. {
  680. gotoXY(3, 24);
  681. cout << "Invalid input please re-enter.";
  682. pressKey(3, 26);
  683. clearLine(3, 24);
  684. clearLine(3, 26);
  685. cin.clear();
  686. clearLine(41, 22);
  687. i--;
  688. newTotalSales -= sale[i];
  689. cin.ignore(150, '\n');
  690. }
  691. else if (sale[i] < 0)
  692. {
  693. bool valid = false;
  694. gotoXY(3, 24);
  695. cout << "Invalid input please enter Sales greater than 0. ";
  696. pressKey(3, 26);
  697. clearLine(3, 24);
  698. clearLine(3, 26);
  699. clearLine(41, 22);
  700. cin.clear();
  701. i--;
  702. newTotalSales -= sale[i];
  703. cin.ignore(150, '\n');
  704. }
  705. else
  706. {
  707. bool valid = true;
  708. clearLine(41, 22);
  709.  
  710. }
  711.  
  712. } while (valid = false);
  713.  
  714. newTotalSales += sale[i];
  715. sales.average = newTotalSales / 12;
  716. }
  717.  
  718. if (sales.average < 200)
  719. {
  720. sales.bonus = newTotalSales * 0;
  721. }
  722. if (sales.average >= 200 && sales.average < 600)
  723. {
  724. sales.bonus = newTotalSales * 0.01;
  725. }
  726. if (sales.average >= 600 && sales.average < 1000)
  727. {
  728. sales.bonus = newTotalSales * 0.04;
  729. }
  730. if (sales.average >= 1000)
  731. {
  732. sales.bonus = newTotalSales * 0.075;
  733. }
  734. break;
  735. };
  736. } while (uOpt != 4);
  737.  
  738. update.seekg((long double)-1 * sizeof(monthsSales), ios::cur);
  739. update.write((char *)& sales, sizeof(monthsSales));
  740.  
  741. if (update.good())
  742. {
  743. system("cls");
  744. message("Sales Details have been updated", 8, 4);
  745. }
  746. }
  747. }
  748. }
  749. update.clear();
  750. update.seekg((long double)0, ios::beg);
  751. if (!present)
  752. {
  753. system("cls");
  754. message("Invalid Salesman No!", 8, 4);
  755. }
  756. update.close();
  757. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement