Guest User

Untitled

a guest
Jul 18th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. // Constant and Type declarations
  9. const int MAXSIZE = 80;
  10. struct Roster {
  11. string LName;
  12. string FName;
  13. char Cheesecake;
  14. };
  15.  
  16. // Function Prototypes
  17. char * returnfilename(char * );
  18. int recordcount(const char * );
  19. void readsurvey(const int, const char *, struct Roster *);
  20. void sortrecord(struct Roster * , const int);
  21. void printsummary(const int, struct Roster * );
  22.  
  23. // Main Routine
  24. int main (void)
  25.  
  26. {
  27. char * filename = new char [MAXSIZE];
  28. int numberofrecords = 0;
  29. struct Roster * MyRoster;
  30.  
  31. filename = returnfilename(filename);
  32. numberofrecords = recordcount(filename);
  33. MyRoster = new struct Roster[numberofrecords];
  34. readsurvey(numberofrecords, filename, MyRoster);
  35. sortrecord(MyRoster, numberofrecords);
  36. printsummary(numberofrecords, MyRoster);
  37. system("pause");
  38. return 0;
  39. }
  40.  
  41. char * returnfilename (char * fn)
  42. {
  43. ifstream myfile;
  44. cout << "Please enter a valid file name: ";
  45. cin >> fn;
  46. myfile.open(fn);
  47. while(!myfile.is_open())
  48. {
  49. cout << "invalid file name, please re-enter: ";
  50. cin >> fn;
  51. myfile.open(fn);
  52. }
  53. myfile.close();
  54. return fn;
  55. }
  56.  
  57. int recordcount (const char * filename)
  58. {
  59. ifstream myfile;
  60. int count = 0;
  61. char inputline[MAXSIZE];
  62.  
  63. myfile.open(filename);
  64. myfile.getline(inputline, MAXSIZE);
  65. while(!myfile.eof())
  66. {
  67. count ++;
  68. myfile.getline(inputline,MAXSIZE);
  69. }
  70. myfile.close();
  71. return count;
  72. }
  73.  
  74. void readsurvey (const int rc, const char * fn, struct Roster * ms)
  75. {
  76. ifstream myfile;
  77. myfile.open(fn);
  78. for (int index = 0; index < rc; index++)
  79. {
  80. myfile >> ms[index].LName;
  81. myfile >> ms[index].FName;
  82. myfile >> ms[index].Cheesecake;
  83. }
  84. myfile.close();
  85.  
  86. }
  87.  
  88. void sortrecord (struct Roster * mr, const int rc)
  89. {
  90. bool swapped = true;
  91. while(swapped)
  92. {
  93.  
  94. swapped = false;
  95. for(int i = 0; i < rc - 1; i++)
  96. if (mr[i].LName > mr[i+1].LName)
  97. {
  98.  
  99. Roster temp = mr[i];
  100. mr[i] = mr[i+1];
  101. mr[i+1] = temp;
  102.  
  103. swapped = true;
  104. }
  105. }
  106. }
  107.  
  108.  
  109.  
  110. void printsummary (const int rc, struct Roster * mr)
  111.  
  112. { int cake = 0;
  113. int pie = 0;
  114.  
  115.  
  116. cout << setw(15) << left << "THE CHEESECAKE REPORT" << endl;
  117. cout << setw(15) << left << "LAST NAME" ;
  118. cout << setw(15) << left << "FIRST NAME" ;
  119. cout << setw(15) << left << "CAKE OR PIE" << endl;
  120. cout << "-----------------------------------------" << endl;
  121.  
  122.  
  123. for (int index = 0; index < rc; index ++)
  124. {
  125. cout << setw(15) << left << mr[index].LName;
  126. cout << setw(15) << left << mr[index].FName;
  127.  
  128. if (mr[index].Cheesecake=='P' || mr[index].Cheesecake =='p')
  129. {
  130. pie++;
  131. cout << "Pie" <<endl;
  132. }
  133. else if (mr[index].Cheesecake == 'C' || mr[index].Cheesecake == 'c')
  134. {
  135. cake++;
  136. cout << "Cake" << endl;
  137. }
  138.  
  139.  
  140. }
  141. cout << "Total number of records: " << rc << endl;
  142. cout << "Number of people who believe Cheesecake is a pie: " << pie << endl;
  143. cout << "Number of people who believe Cheesecake is a cake: " << cake << endl;
  144.  
  145.  
  146.  
  147. }
Add Comment
Please, Sign In to add comment