Guest User

Untitled

a guest
Apr 23rd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <iomanip>                          // Brandon Poling
  6. using namespace std;                        // Comp-1102 Project 3b
  7.  
  8. void getFile(char fileName[]);
  9. void menuDisplay();
  10. void processMenuChoice(char choice, char fileName[]);
  11. void printDetailedReport(char fileName[]);
  12. void printExceptionReport(char fileName[]);
  13. void printSummaryReport(char fileName[]);
  14. void printDescription(char fileName[]);
  15. void printColumnHeadings();
  16. void getInfoFromFile(ifstream& dataFile, char stockNumber[], char description[], float& cost, float& price, int& quantity);
  17. void trimSpaces(char str[]);
  18. void formatLines(ifstream& dataFile, char stockNumber[], char description[], float& cost, float& price, int& quantity, int& count);
  19.  
  20.  
  21. int main()
  22. {
  23.     ifstream dataFile;
  24.     char choice;
  25.     char fileName[30];
  26.     getFile(fileName);
  27.     menuDisplay();
  28.     cin >> choice;
  29.     cin.ignore();
  30.  
  31.     while ( (choice != 'E') && (choice != 'e'))
  32.     {
  33.         processMenuChoice(choice, fileName);
  34.         menuDisplay();
  35.         cin >> choice;
  36.         cin.ignore();
  37.     }
  38. }
  39.  
  40. void getFile(char fileName[])
  41. {
  42.     ifstream dataFile;
  43.     do
  44.     {
  45.         cout << "\n\nEnter name of file: ";
  46.         cin.getline(fileName, 30, '\n');
  47.  
  48.         if (strcmp(fileName, "") == 0)
  49.         {
  50.             exit(1);
  51.             //strcat(fileName, "stock.txt");
  52.         }
  53.  
  54.         dataFile.open(fileName);
  55.         if (dataFile.fail())
  56.         {
  57.             cout << "\nError in opening file.";
  58.         }
  59.     }
  60.     while (dataFile.fail());
  61. }
  62.  
  63. void menuDisplay()
  64. {
  65.         cout << "\nA. Detail Report";
  66.         cout << "\nB. Exception Report";
  67.         cout << "\nC. Summary Report";
  68.         cout << "\nD. Display Description of Item";
  69.         cout << "\nE. Exit";
  70.         cout << "\n\nEnter a choice: ";
  71. }
  72.  
  73. void processMenuChoice(char choice, char fileName[])
  74. {
  75.     if (choice == 'A' || choice == 'a')
  76.     {
  77.         printDetailedReport(fileName);
  78.     }
  79.  
  80.     else if (choice == 'B' || choice == 'b')
  81.     {
  82.         printExceptionReport(fileName);
  83.     }
  84.  
  85.     else if (choice == 'C' || choice == 'c')
  86.     {
  87.         printSummaryReport(fileName);
  88.     }
  89.  
  90.     else if (choice == 'D' || choice == 'd')
  91.     {
  92.         printDescription(fileName);
  93.     }
  94.  
  95.     else
  96.     {
  97.         cout << "\nYou entered an invalid choice." << endl;
  98.     }
  99. }
  100.  
  101. void printDetailedReport(char fileName[])
  102. {
  103.     ifstream dataFile;
  104.     char stockNumber[6];
  105.     char description[20];
  106.     float cost;
  107.     float price;
  108.     int quantity;
  109.     int count;
  110.  
  111.     dataFile.open(fileName);
  112.     if(dataFile.fail())
  113.     {
  114.         cout << "Error in opening file " << fileName << endl;
  115.         cout << "\n\nEnter name of file: ";
  116.         cin.getline(fileName, 30, '\n');
  117.     }
  118.  
  119.     system("CLS");
  120.     count = 0;
  121.  
  122.     printColumnHeadings();
  123.     while (!dataFile.eof())
  124.     {
  125.         count++;
  126.         if (count > 10)
  127.         {
  128.             count = 1;
  129.             system("PAUSE");
  130.             cout << endl;
  131.             printColumnHeadings();
  132.         }
  133.         getInfoFromFile(dataFile, stockNumber, description, cost, price, quantity);
  134.         if (strcmp(stockNumber, "") != 0)
  135.         {
  136.         formatLines(dataFile, stockNumber, description, cost, price, quantity, count);
  137.         }
  138.     }
  139.  
  140.     dataFile.close();
  141. }
  142.  
  143. void printExceptionReport(char fileName[])
  144. {
  145.     ifstream dataFile;
  146.     char stockNumber[6];
  147.     char description[20];
  148.     float cost;
  149.     float price;
  150.     int quantity;
  151.     int count;
  152.     int minimum;
  153.  
  154.     dataFile.open(fileName);
  155.     while (dataFile.fail())
  156.     {
  157.         cout << "Error in opening file " << fileName << endl;
  158.         cout << "\n\nEnter name of file: ";
  159.         cin.getline(fileName, 30, '\n');
  160.         dataFile.open(fileName);
  161.     }
  162.  
  163.     cout << "Enter minimum quantity of stock: ";
  164.     cin >> minimum;
  165.     system("CLS");
  166.  
  167.     count = 0;
  168.     printColumnHeadings();
  169.     while (!dataFile.eof())
  170.     {
  171.         getInfoFromFile(dataFile, stockNumber, description, cost, price, quantity);
  172.  
  173.         if ((quantity < minimum) && (strcmp(stockNumber, "") != 0))
  174.         {
  175.             count++;
  176.             formatLines(dataFile, stockNumber, description, cost, price, quantity, count);
  177.         }
  178.     }
  179.     if (count == 0)    // If count wasn't incremented, no stock below the user's minimum
  180.     {
  181.         cout << "\n--------------------------No Stock Below Your Minimum-----------------------\n\n";
  182.     }
  183.     dataFile.close();
  184. }
  185.  
  186. void printSummaryReport(char fileName[])
  187. {
  188.     ifstream dataFile;
  189.     char stockNumber[6];
  190.     char description[20];
  191.     float cost;
  192.     float price;
  193.     int quantity;
  194.     float temp;
  195.     float largest;
  196.     char largestStockNum[6];
  197.     char largestDesc[20];
  198.     float secondLargest;
  199.     char secLargestStockNum[6];
  200.     char secLargestDesc[20];
  201.  
  202.     dataFile.open(fileName);
  203.     while (dataFile.fail())
  204.     {
  205.         cout << "Error in opening file " << fileName << endl;
  206.         cout << "\n\nEnter name of file: ";
  207.         cin.getline(fileName, 30, '\n');
  208.         dataFile.open(fileName);
  209.     }
  210.  
  211.     system("CLS");
  212.     largest = 0;
  213.     secondLargest = 0;
  214.  
  215.     while (!dataFile.eof())
  216.     {
  217.         getInfoFromFile(dataFile, stockNumber, description, cost, price, quantity);
  218.         temp = (price - cost) * quantity;
  219.  
  220.         if (temp > largest)
  221.         {
  222.             secondLargest = largest;
  223.             strcpy(secLargestStockNum, largestStockNum);
  224.             strcpy(secLargestDesc, largestDesc);
  225.  
  226.             largest = temp;
  227.             strcpy(largestStockNum, stockNumber);
  228.             strcpy(largestDesc, description);
  229.         }
  230.  
  231.         else if ((temp > secondLargest) && (temp < largest))
  232.         {
  233.             secondLargest = temp;
  234.             strcpy(secLargestStockNum, stockNumber);
  235.             strcpy(secLargestDesc, description);
  236.         }
  237.  
  238.     }
  239.  
  240.     cout << "Largest Profit Potential" << endl;
  241.     cout << "Stock Number: " << largestStockNum << endl;
  242.     cout << "Description: " << largestDesc << endl;
  243.     cout << endl;
  244.     cout << "Second Largest Profit Potential" << endl;
  245.     cout << "Stock Number: " << secLargestStockNum << endl;
  246.     cout << "Description: " << secLargestDesc << endl;
  247.  
  248. }
  249.     ifstream dataFile;
  250.  
  251. void printDescription(char fileName[])
  252. {
  253.     ifstream dataFile;
  254.     char stockNumber[6];
  255.     char description[20];
  256.     float cost;
  257.     float price;
  258.     int quantity;
  259.     char userStockNum[6];
  260.     bool found;
  261.  
  262.     dataFile.open(fileName);
  263.     while (dataFile.fail())
  264.     {
  265.         cout << "Error in opening file " << fileName << endl;
  266.         cout << "\n\nEnter name of file: ";
  267.         cin.getline(fileName, 30, '\n');
  268.         dataFile.open(fileName);
  269.     }
  270.  
  271.     cout << "Enter a stock number: ";
  272.     cin.getline(userStockNum, 6);
  273.  
  274.     system("CLS");
  275.     found = false;
  276.     while(!dataFile.eof())
  277.     {
  278.         getInfoFromFile(dataFile, stockNumber, description, cost, price, quantity);
  279.         if ((strcmp(userStockNum, stockNumber) == 0) && (strcmp(stockNumber, "") != 0))
  280.         {
  281.             cout << "Match! Description: " << description << endl;
  282.             found = true;
  283.         }
  284.     }
  285.  
  286.     if (found == false)
  287.     {
  288.         cout << "\nCouldn't find specified stock number.\n\n";
  289.     }
  290.  
  291.  
  292. }
  293.  
  294. void printColumnHeadings()
  295. {
  296.     cout.unsetf(ios::left);
  297.     cout << setiosflags(ios::right) << setw(7) << "Line" << " ";
  298.     cout.unsetf(ios::right);
  299.     cout << setiosflags(ios::left) << setw(9) << "Stock#" << setw(22) << "Description";
  300.     cout.unsetf(ios::left);
  301.     cout << setiosflags(ios::fixed) << setiosflags(ios::right) <<
  302.     setw(12) << "Cost" << setw(12)<< "Price" << setw(12) << "Quantity" << endl;
  303. }
  304.  
  305. void getInfoFromFile(ifstream& dataFile, char stockNumber[], char description[], float& cost, float& price, int& quantity)
  306. {
  307.     dataFile.get(stockNumber, 6);
  308.     trimSpaces(stockNumber);
  309.     dataFile.get(description, 20);
  310.     trimSpaces(description);
  311.     dataFile >> cost >> price >> quantity;
  312.     dataFile.ignore(99, '\n');
  313. }
  314. void trimSpaces(char str[])
  315. {
  316.     int n;
  317.     bool done;
  318.  
  319.     n = strlen(str) - 1;
  320.     done = false;
  321.     while (!done)
  322.     {
  323.         if (n < 0)
  324.         {
  325.             done = true;
  326.         }
  327.         else if (str[n] != 32) // 32 = space character
  328.         {
  329.             done = true;
  330.         }
  331.         else
  332.         {
  333.         n--;
  334.         }
  335.     }
  336.     str[n+1] = '\0';
  337. }
  338.  
  339. void formatLines(ifstream& dataFile, char stockNumber[], char description[], float& cost, float& price, int& quantity, int& count)
  340. {
  341.     cout.unsetf(ios::left);
  342.     cout << setiosflags(ios::right) << setw(7) << count << ") ";
  343.     cout.unsetf(ios::right);
  344.     cout << setiosflags(ios::left) << setw(9) << stockNumber << setw(22) << description;
  345.     cout.unsetf(ios::left);
  346.     cout << setiosflags(ios::fixed) << setiosflags(ios::right) << setprecision(2) <<
  347.     setw(12) << cost << setw(12)<< price << setw(12) << quantity << endl;
  348. }
Add Comment
Please, Sign In to add comment