Guest User

Untitled

a guest
Feb 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. //Kevin Pavao
  2. //School: FAU
  3. //Class: ISM3230
  4. //Written: 11/5/06
  5. //purpose: input male and female student GPAs in a class and write the list and the average to a file
  6. //GenderGrades.cpp
  7.  
  8. #include<iostream>
  9. #include<string>
  10. #include<fstream>
  11. #include<iomanip>
  12.  
  13. using namespace std;
  14.  
  15. //writes to the file chosen
  16. void input(int file);
  17.  
  18. //averages the gpa
  19. double average(double gpa, double numGrades);
  20.  
  21. //if invalid data entered, requests user to enter again
  22. void reEnter(char &resp, string type);
  23.  
  24. //clears screen
  25. void clear();
  26.  
  27.  
  28. int main()
  29. {
  30. char resp='y';
  31. int file; //what file to open
  32.  
  33. do
  34. {
  35. clear();
  36. //menu
  37. cout << "1. historyData.dat" << endl;
  38. cout << "2. politicalscienceData.dat" << endl;
  39. cout << "3. scienceData.dat" << endl;
  40.  
  41. cout << endl << "Which file would you like to open? 1, 2 or 3: ";
  42. cin >> file;
  43.  
  44. clear();
  45.  
  46. input(file);
  47.  
  48. cout << "File written.\n";
  49. cout << "Open another file? (y/n): ";
  50. cin >> resp;
  51. if(resp!='Y' && resp!='y' && resp!='n' && resp!='N') //if invalid char entered
  52. reEnter(resp,"continue");
  53. clear();
  54. }while((resp=='Y' || resp=='y') && (resp!='n' || resp!='N')); //continues until N or n is input
  55.  
  56.  
  57. return 0;
  58. }
  59.  
  60. void input(int file)
  61. {
  62. ofstream outfile;
  63. char gender;
  64. char resp='y';
  65. double gpa, maleGPA=0.0, femaleGPA=0.0;
  66. int numMale=0, numFemale=0; //total number of males and females for averaging
  67.  
  68. if(file==1)
  69. {
  70. outfile.open("historyData.dat");
  71. outfile << "Class: HST3239 History" << endl << endl;
  72. }
  73. if(file==2)
  74. {
  75. outfile.open("politicalscienceData.dat");
  76. outfile << "Class: POL5695 Political Science" << endl << endl;
  77. }
  78. if(file==3)
  79. {
  80. outfile.open("scienceData.dat");
  81. outfile << "Class: SCI3460 Science" << endl << endl;
  82. }
  83.  
  84. //headings for the columns
  85. outfile << left << setw(10) << "Gender";
  86. outfile << right << setw(10) << "GPA" << endl;
  87.  
  88. //continue until N or n is input
  89. while((resp=='Y' || resp=='y') && (resp!='n' || resp!='N'))
  90. {
  91. clear();
  92. cout << "Is the student male or female? (m/f): ";
  93. cin >> gender;
  94. if(gender!='M' && gender!='m' && gender!='F' && gender!='f')
  95. reEnter(gender,"gender");
  96. outfile << left << setw(10) << gender; //doesnt write to file until m or f is input
  97.  
  98. cout << "Enter student GPA: ";
  99. cin >> gpa;
  100. outfile << setprecision(2) << setiosflags(ios::showpoint) << setiosflags(ios::fixed);
  101. outfile << right << setw(10) << gpa << endl; //write GPA to file with 2 decimal points
  102.  
  103. if(gender=='M' || gender=='m')
  104. {
  105. maleGPA+=gpa; //adds gpa to total male gpa for averaging
  106. numMale++;
  107. }
  108. else
  109. {
  110. femaleGPA+=gpa; //adds gpa to total female gpa for averaging
  111. numFemale++;
  112. }
  113.  
  114. clear();
  115.  
  116. cout << "Is there another student? (y/n): ";
  117. cin >> resp;
  118. if(resp!='Y' && resp!='y' && resp!='n' && resp!='N')
  119. reEnter(resp, "continue");
  120. }
  121.  
  122. outfile << setprecision(2) << setiosflags(ios::showpoint) << setiosflags(ios::fixed);
  123.  
  124. //writes averages to file
  125. outfile << "\nNumber of males: " << numMale << endl;
  126. outfile << "Average male GPA: ";
  127. outfile << average(maleGPA,numMale) << endl << endl;
  128. outfile << "Number of females: " << numFemale << endl;
  129. outfile << "Average female GPA: ";
  130. outfile << average(femaleGPA,numFemale) << endl << endl;
  131. outfile << "Total average: ";
  132. outfile << average(femaleGPA+maleGPA,numFemale+numMale) << endl; //adds male and female GPA's
  133. outfile.close();
  134.  
  135. }
  136.  
  137. double average(double gpa, double numGrades)
  138. {
  139. double avg=gpa/numGrades;
  140. return avg;
  141. }
  142.  
  143. void reEnter(char &resp, string type)
  144. {
  145.  
  146. //if gender is not M or F
  147. if(type=="gender")
  148. {
  149. do
  150. {
  151. cout << "Invalid data entered, please enter M or F: ";
  152. cin >> resp;
  153. }while(resp!='M' && resp!='m' && resp!='F' && resp!='f');
  154. }
  155.  
  156.  
  157. //if value is not Y or N
  158. if(type=="continue")
  159. {
  160. do
  161. {
  162. cout << "Invalid data entered, continue? (y/n) ";
  163. cin >> resp;
  164. }while(resp!='Y' && resp!='y' && resp!='n' && resp!='N');
  165. }
  166. }
  167.  
  168. void clear()
  169. {
  170. for(int x=0; x<=50; x++)
  171. cout << endl;
  172. }
Add Comment
Please, Sign In to add comment