Guest User

Untitled

a guest
Jul 18th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6. const int MAXSIZE='80';
  7. // ********************************************************************
  8. // * Struct Name: MCCSR                                               *
  9. // * Description: Stores the Last name, first name, and survey option *
  10. // * of each person surveyed.                                         *
  11. // * Parameter Description: string Fname stores the first name, string*
  12. // * Lname stores the last name, and char PorC stores pie or          *
  13. // * cheesecake.                                                      *
  14. // * Date: 03/30/2012                                                 *
  15. // * Author: Reid Richardson                                          *
  16. // ********************************************************************
  17. struct MCCSR
  18. {
  19.     string Fname, Lname;
  20.     char PorC;
  21. };
  22.  
  23. int records(const char * filename);
  24. char * inputfile(char * filename);
  25. void sort(struct MCCSR *, int counter);
  26. void PrintSummary(int count, struct MCCSR *);
  27.  
  28. // ********************************************************************
  29. // * Function Name: int main                                          *
  30. // * Description: The main routine gathers all of the information and *
  31. // * allocates memory.  It creates a struct to store the first name,  *
  32. // * last name, and survey selection from each person.  After doing   *
  33. // * all of that, it calls on the PrintSummary function to print.     *
  34. // * Parameter Description: none                                      *
  35. // * Date: 03/30/2012                                                 *
  36. // * Author: Reid Richardson                                          *
  37. // ********************************************************************
  38. int main()
  39. {
  40.     char * fn = new char[MAXSIZE];
  41.     fn = inputfile(fn);
  42.     int cn = 0;
  43.     cn = records(fn);
  44.     struct MCCSR * Survey;
  45.     Survey = new struct MCCSR[cn];
  46.     ifstream MyInputFile;
  47.     MyInputFile.open(fn);
  48.     for(int i=0; i < cn; i++)
  49.     {
  50.         MyInputFile >> Survey[i].Lname;
  51.         MyInputFile >> Survey[i].Fname;
  52.         MyInputFile >> Survey[i].PorC;
  53.     }
  54.     MyInputFile.close();
  55.     PrintSummary(cn, Survey);
  56.     sort(Survey, cn);
  57.     return 0;
  58. }
  59.  
  60. // ********************************************************************
  61. // * Function Name: int records                                       *
  62. // * Description: counts the number of lines and returns the value    *
  63. // * Parameter Description: const char * filename dereferences the    *
  64. // * inputted file so the lines can be counted.                       *
  65. // * Date: 03/30/2012                                                 *
  66. // * Author: Reid Richardson                                          *
  67. // ********************************************************************
  68. int records(const char * filename)
  69. {
  70.     int counter = 0;
  71.     char line[MAXSIZE];
  72.     ifstream MyInputFile;
  73.     MyInputFile.open(filename);
  74.     MyInputFile.getline(line, MAXSIZE);
  75.  
  76.     while(!MyInputFile.eof()) // keeps getting lines until the file ends
  77.     {
  78.         counter++;
  79.         MyInputFile.getline(line, MAXSIZE);
  80.     }
  81.     MyInputFile.close();
  82.     return counter; // returns the numeric value of the amount of lines
  83. }
  84. // ********************************************************************
  85. // * Function Name: char * inputfile                                  *
  86. // * Description: Prompts the user to input a valid file and then     *
  87. // * stores it for future use by other functions.                     *
  88. // * Parameter Description: char * filename is where the user inputs  *
  89. // * the file.                                                        *
  90. // * Date: 03/30/2012                                                 *
  91. // * Author: Reid Richardson                                          *
  92. // ********************************************************************
  93. char * inputfile(char * filename)
  94. {
  95.     ifstream MyInputFile;
  96.     cout << "Please enter a valid file: ";
  97.     cin >> filename;
  98.     MyInputFile.open(filename);
  99.     while(!MyInputFile.is_open())
  100.     {
  101.         cout << "Invalid File.  Please try again: ";
  102.         cin >> filename;
  103.         MyInputFile.open(filename);
  104.     }
  105.     MyInputFile.close();
  106.     return filename;
  107. }
  108.  
  109. // ********************************************************************
  110. // * Function Name: Sort                                              *
  111. // * Description: Alphabetically sorts last names                     *
  112. // * Parameter Description: struct MCCSR * sur is used with temp to   *
  113. // * move data and int counter is used as a counter                   *
  114. // * Date: 03/30/2012                                                 *
  115. // * Author: Originally written by Dr. David Gaitros, modified by     *
  116. // *         Reid Richardson
  117. // ********************************************************************
  118. void Sort(struct MCCSR * sur, int counter)
  119. {
  120.     struct MCCSR temp;
  121.     bool swapped = true;
  122.     while(swapped)
  123.     {
  124.         swapped = false;
  125.         for(int i=0; i < counter-1; i++)
  126.         {
  127.             if(sur[i].Lname > sur[i+1].Lname)
  128.             {
  129.                 temp = sur[i];
  130.                 sur[i] = sur[i+1];
  131.                 sur[i+1] = temp;
  132.                 swapped = true;
  133.             }
  134.         }
  135.     }
  136. }
  137.  
  138. // ********************************************************************
  139. // * Function Name: void PrintSummary                                 *
  140. // * Description: Prints out the last names, first names, survey      *
  141. // * choices, for each person and displays the total amount of people *
  142. // * and the total amount of cheesecake/pie options.                  *
  143. // * Parameter Description: int counter is used to tally the amount of*
  144. // * people interviewed and struct MCCSR * sur is used to dereference *
  145. // * the main struct to have access to the information.               *
  146. // * Date: 03/30/2012                                                 *
  147. // * Author: Reid Richardson                                          *
  148. // ********************************************************************
  149. void PrintSummary(int counter, struct MCCSR * sur)
  150. {
  151.     cout << endl;
  152.     cout << setw(30) << "The Cheesecake Report" << endl;
  153.     cout << endl;
  154.     cout << setw(5) << "Last Name" << setw(15) << "First Name" << setw(20) << "Cake or Pie" << endl;
  155.     cout << endl;
  156.     int cake=0;
  157.     int pie=0;
  158.     for(int count = 0; count < counter; count++)
  159.     {
  160.         cout << setw(15) << left << sur[count].Lname;
  161.         cout << setw(20) << left << sur[count].Fname;
  162.         if(sur[count].PorC == 'C')
  163.         {
  164.             cout << setw(10) << left << "Cake" << endl;
  165.             cake++;
  166.         }
  167.  
  168.         if(sur[count].PorC == 'P')
  169.         {
  170.             cout << setw(10) << left << "Pie" << endl;
  171.             pie++;
  172.         }
  173.     }
  174.     cout << "The number of records is: " << counter << endl;
  175.     cout << "The number of people who think CheeseCake is a Pie is: " << pie << endl;
  176.     cout << "The number of people who think CheeseCake is a Cake is: " << cake << endl;
  177. }
Add Comment
Please, Sign In to add comment