Guest User

Untitled

a guest
Mar 16th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. /******************************************
  2. *Program Name: Cheesecake Survey *
  3. *Class: COP3014 *
  4. *Description: Inputs and sorts a file *
  5. *alphabetically, prints a summary of the *
  6. *survey. *
  7. *Date: 11/18/2011 *
  8. *Author: Ashley Suh *
  9. *Sources: Dr. Gaitros *
  10. ******************************************/
  11. #include <iostream>
  12. #include <iomanip>
  13. #include <fstream>
  14. #include <string>
  15.  
  16. using namespace std;
  17.  
  18. //Prototypes
  19. const int MAXSIZE=80;
  20. struct Roster
  21. {
  22. string Lname;
  23. string Fname;
  24. char Cheesecake;
  25. };
  26. string getfilename();
  27. int countrecords (string);
  28. void readsurvey (const int, const string, struct Roster *);
  29. void sort (struct Roster *, const int);
  30. void printsummary (const int, struct Roster *);
  31.  
  32.  
  33. int main (void)
  34. {
  35. //1. Get a valid file name
  36. string filename;
  37. filename = new char[MAXSIZE];
  38. filename = getfilename();
  39.  
  40. //2. Count the # of records
  41. int recordcount = countrecords(filename);
  42. cout << "Got " << recordcount << " records" << endl;
  43.  
  44. //3. Allocate the space for the records
  45. struct Roster * MySurvey = new struct Roster[recordcount];
  46.  
  47. //4. Read the records into the structure
  48. readsurvey (recordcount, filename, MySurvey);
  49.  
  50. //5. Sort the records
  51. sort (MySurvey, recordcount);
  52.  
  53. //6. Print report
  54. printsummary (recordcount, MySurvey);
  55.  
  56. system("pause");
  57.  
  58. return 0;
  59.  
  60. }
  61.  
  62. /******************************************
  63. *Description: Function opening the file *
  64. *and checks whether or not the file exists*
  65. *Author: Ashley Suh *
  66. ******************************************/
  67. string getfilename ()
  68. {
  69. string filename;
  70. ifstream myfile;
  71.  
  72. //Get file name
  73. cout << "Please enter a file name: ";
  74. cin >> filename;
  75. myfile.open(filename.c_str());
  76.  
  77. //loop to check if file exists
  78. while (!myfile.is_open())
  79. {
  80. cout << "Enter it again: ";
  81. cin >> filename;
  82. myfile.open(filename.c_str());
  83. }
  84.  
  85. myfile.close(); //close file
  86. return filename;
  87. }
  88.  
  89. /******************************************
  90. *Description: counts the amount of records*
  91. *in the file. *
  92. *Author: Ashley Suh *
  93. ******************************************/
  94. int countrecords (const string fn)
  95. {
  96. //counts amount of records in file
  97. int count = 0;
  98. char inputline [MAXSIZE];
  99. ifstream myfile;
  100. myfile.open(fn);
  101. myfile.getline(inputline,80);
  102.  
  103. //loop to count lines when file is not complete
  104. while (!myfile.eof())
  105. {
  106. count++;
  107. myfile.getline(inputline,80);
  108. }
  109. myfile.close();
  110.  
  111. return count;
  112. }
  113.  
  114. /******************************************
  115. *Description: reads in the file and *
  116. *creates a copy. *
  117. *Author: Ashley Suh *
  118. ******************************************/
  119. void readsurvey (const int rc, const string fn, struct Roster * ms)
  120. {
  121. //opens the file
  122. ifstream myfile;
  123. myfile.open(fn.c_str());
  124.  
  125. //copies the file in MySurvey
  126. for (int index = 0; index < rc; index++)
  127. {
  128. myfile >> ms[index].Lname;
  129. myfile >> ms[index].Fname;
  130. myfile >> ms[index].Cheesecake;
  131. }
  132.  
  133. //closes the file
  134. myfile.close();
  135. }
  136.  
  137. /******************************************
  138. *Description: Sorts the file for names *
  139. *in alphabetical order. *
  140. *Author: Ashley Suh *
  141. ******************************************/
  142. void sort (struct Roster * ms, const int rc)
  143. {
  144. bool swapped = true;
  145.  
  146. //while loop where sorting will continue
  147. while(swapped)
  148. {
  149. //if swapping is unnecesary, sets to false
  150. swapped = false;
  151.  
  152. //swap places in file if not in alphabetical order
  153. for(int i = 0; i < rc - 1; i++)
  154. if (ms[i].Lname > ms[i+1].Lname)
  155. {
  156. //swaps
  157. Roster temp = ms[i];
  158. ms[i] = ms[i+1];
  159. ms[i+1] = temp;
  160.  
  161. swapped = true;
  162. }
  163. }
  164. }
  165.  
  166. /*******************************************
  167. *Description: Prints the records of the *
  168. *file, including the number of participants*
  169. *and amount of people who chose cake or pie*
  170. *Author: Ashley Suh *
  171. *******************************************/
  172. void printsummary (const int rc, struct Roster * ms)
  173. {
  174. int cake = 0;
  175. int pie = 0;
  176.  
  177. //survey header
  178. cout << setw(15) << left << "Cheesecake Survey" << endl;
  179. cout << setw(15) << left << "Last Name" ;
  180. cout << setw(15) << left << "First Name" ;
  181. cout << setw(15) << left << "Cake or Pie" << endl;
  182. cout << "-----------------------------------------" << endl;
  183.  
  184. //displays sorted file
  185. for(int i = 0; i < rc; i++)
  186. {
  187. cout << setw(15) << left << ms[i].Lname;
  188. cout << setw(15) << left << ms[i].Fname;
  189.  
  190. if (ms[i].Cheesecake == 'C')
  191. {
  192. cake++; //counter for cake choices
  193. cout << setw(10) << left << "Cake" << endl;
  194. }
  195.  
  196. else
  197. {
  198. pie++; //counter for pie choices
  199. cout << setw(10) << left << "Pie" << endl;
  200. }
  201. }
  202.  
  203. cout << "Number of people who believe Cheesecake is a cake: " << cake << endl;
  204. cout << "Number of people who believe Cheesecake is a pie: " << pie << endl;
  205. cout << "Total number of survey participants: " << rc << endl;
  206. }
Add Comment
Please, Sign In to add comment