Guest User

Untitled

a guest
Mar 17th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7. const int MAXSIZE = 80;
  8. char * getfilename(char *);
  9. int countrecords(char *);
  10. void printsummary(const int,const struct Roster *, int &pc, int &cc);
  11. void readsurvey(const int,const char *,struct Roster *, int &pc, int &cc);
  12. void bubble(struct Roster * sv, int rc);
  13.  
  14. struct Roster
  15. {
  16. string LName;
  17. string FName;
  18. char Cheesecake;
  19. };
  20.  
  21. int main(void)
  22. {
  23. struct Roster * MySurvey;
  24. char * filename;
  25. filename = new char[MAXSIZE];
  26. int recordCount = 0, pieCount = 0, cakeCount = 0;
  27.  
  28. filename = getfilename(filename); //step 1: Get a valid filename
  29. recordCount = countrecords(filename); //step 2: Count the number of records
  30. MySurvey = new struct Roster[recordCount]; //step 3: Allocate space for the records
  31. readsurvey(recordCount,filename,MySurvey, pieCount, cakeCount); //step 4: Read the records into the structure
  32. bubble(MySurvey, recordCount); //step 5: Bubblesort the records
  33. printsummary(recordCount, MySurvey, pieCount, cakeCount); //step 6: Print report
  34. return 0;
  35. }
  36.  
  37. char * getfilename(char * fn)
  38. {
  39. //Get file to read
  40. ifstream myfile;
  41. cout << "Please enter a file name: ";
  42. fn = "myfile.txt";
  43. cout << endl;
  44. myfile.open(fn);
  45. while(!myfile.is_open())
  46. {
  47. cout << "Filename rejected. Try again:";
  48. cin >> fn;
  49. myfile.open(fn);
  50. }
  51. myfile.close();
  52. return fn;
  53. }
  54.  
  55. int countrecords(char * fn)
  56. {
  57. int count = 0;
  58. char inputline[MAXSIZE];
  59. ifstream myfile;
  60. myfile.open(fn);
  61. myfile.getline(inputline,80);
  62. //Count records recursively
  63. while(!myfile.eof())
  64. {
  65. count++;
  66. myfile.getline(inputline,80);
  67. }
  68. myfile.close();
  69. return count;
  70. }
  71.  
  72. void readsurvey(const int rc, const char * fn, struct Roster * sv, int &pc, int &cc)
  73. {
  74.  
  75. ifstream myfile;
  76. myfile.open(fn);
  77. for (int index = 0; index <rc; index ++)
  78. {
  79. myfile >> sv[index].LName;
  80. myfile >> sv[index].FName;
  81. myfile >> sv[index].Cheesecake;
  82. //Tally votes
  83. if (sv[index].Cheesecake == 'P')
  84. pc++;
  85. if (sv[index].Cheesecake == 'C')
  86. cc++;
  87. }
  88. myfile.close();
  89. }
  90.  
  91. void printsummary (const int rc, const struct Roster * sv, int &pc, int &cc)
  92. {
  93. string pref;
  94. for (int index = 0; index < rc; index ++) {
  95.  
  96. if(sv[index].Cheesecake == 'P')
  97. pref = "Pie";
  98. else if(sv[index].Cheesecake == 'C')
  99. pref = "Cake";
  100. //Print left justified last and first names, and preference
  101. cout << left << setw(12) << sv[index].LName << " " <<
  102.  
  103. setw(12) << sv[index].FName << " " <<
  104.  
  105. setw(4) <<" "<< pref << endl;
  106. }
  107. //Print final statistics
  108. cout << "Number of records is: " << rc << endl;
  109. cout << "Number of pie-thusiasts is: " << pc << endl;
  110. cout << "Number of cake-cultists is: " << cc << endl;
  111. }
  112.  
  113. //Standard Bubble Sort
  114. void bubble(struct Roster * sv,int rc)
  115. {
  116. //Temporary Roster container object
  117. struct Roster hold;
  118. for(int x = 0; x < rc; x++)
  119. {
  120. for(int y = 0; y < rc-1; y++)
  121. {
  122. //Compare LNames
  123. if(sv[y].LName > sv[y+1].LName)
  124. {
  125. //But manipulate the containers themselves,
  126. //otherwise a bug will occur where the last names
  127. //would be sorted but not the rest of the data.
  128. hold = sv[y+1];
  129. sv[y+1] = sv[y];
  130. sv[y] = hold;
  131. }
  132. }
  133. }
  134. }
  135. /* GOTTA SORT LIKA NIGGA. YESA.
  136. use 'rc' for total recordCount
  137.  
  138. to change 'P' and 'C' to Pie and Cake;
  139. before previous functions of cout simply add an if statement saying something
  140. like 'if P then =Pie, if C then = Cake or whatever' */
Add Comment
Please, Sign In to add comment