Advertisement
Guest User

good code

a guest
Nov 20th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. //including libraries
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. void function_inputData();
  9.  
  10. int main(){
  11.  
  12. int num_students;
  13.  
  14. const double GRADE_A = 90.0;
  15. const double GRADE_B = 80.0;
  16. const double GRADE_C = 70.0;
  17. const double GRADE_D = 60.0;
  18.  
  19. const int STUDENT_COUNT = 3;
  20. const int STRING_COUNT = 9;
  21. const int INT_COUNT = 2;
  22. const int COURSE_COUNT = 3;
  23. const int GRADE_COUNT = 6;
  24.  
  25. const string WARN = "Warning: Your grade is low and needs improvment";
  26. const string COMMENDATION = "Commendation: Congratulations, Your grade is excellent!";
  27. const double WARN_NUM = 70.0;
  28. const double COMMENDATION_NUM = 95.0;
  29.  
  30. const string FILE_ERROR = "Error: File is not reading.";
  31. const string INV_NUM = "Error: invalid number, an integer must be between 1 and 100";
  32. const string INV_FLOAT = "Error: invalid input, floating point must be between 1.0 and 100.0";
  33. const string INV_STRING = "Error: string must be longer than 0 spaces";
  34. const string ARRAY_ERROR = "Error: Array out of bounds";
  35.  
  36. // Get input for number of students in class
  37. do {
  38. cout << "Please input the number of students in the class " << endl;
  39. cin >> num_students;
  40. if (num_students < STUDENT_COUNT)
  41. cout << "Number of students must be greater than 3" << endl;
  42. } while (num_students < STUDENT_COUNT);
  43.  
  44.  
  45.  
  46. //Declare files
  47. ifstream file_input;
  48. ofstream file_output;
  49.  
  50. //open input file
  51. file_input.open("Project4_David_Janicki_Input.txt");
  52. if (file_input.fail()){
  53. cout << FILE_ERROR << endl;
  54. return 0;
  55. }
  56.  
  57. //create student arrays
  58. string nonNumeric [num_students][STRING_COUNT];
  59. int numeric1 [num_students][INT_COUNT];
  60. double numeric2 [STUDENT_COUNT][COURSE_COUNT][GRADE_COUNT];
  61. char letterGrade[STUDENT_COUNT][COURSE_COUNT];
  62.  
  63. // outer loop for students
  64. for (int i=0; i<num_students; i++){
  65. //check for error
  66. if (i < STUDENT_COUNT) {
  67. cout << ARRAY_ERROR << endl;
  68. // break;
  69. }
  70. // getting input from file and saving in arrays
  71. //student name
  72. getline(file_input, nonNumeric[i][0]);
  73. if (nonNumeric[i][0].length() <= 0){
  74. cout << INV_STRING << endl;
  75. continue;
  76. }
  77. // student id
  78. getline(file_input, nonNumeric[i][1]);
  79. if (nonNumeric[i][1].length() <=0){
  80. cout << INV_STRING << endl;
  81. continue;
  82. }
  83. //student address
  84. getline(file_input, nonNumeric[i][2]);
  85. if (nonNumeric[i][2].length() <=0){
  86. cout << INV_STRING << endl;
  87. continue;
  88. }
  89. //student phone number
  90. getline(file_input, nonNumeric[i][3]);
  91. if (nonNumeric[i][3].length() <=0){
  92. cout << INV_STRING << endl;
  93. continue;
  94. }
  95. //student social
  96. getline(file_input, nonNumeric[i][4]);
  97. if (nonNumeric[i][4].length() <=0){
  98. cout << INV_STRING << endl;
  99. continue;
  100. }
  101. //student age
  102. file_input >> numeric1[i][0];
  103. file_input.ignore();
  104. if (numeric1[i][0] < 1 || numeric1[i][0] > 100){
  105. cout << INV_NUM << endl;
  106. // break;
  107. }
  108. // years at txstate
  109. file_input >> numeric1[i][1];
  110. file_input.ignore();
  111. if (numeric1[i][1] < 1 || numeric1[i][1] > 100){
  112. cout << INV_NUM << endl;
  113. // break;
  114. }
  115.  
  116. //class loop
  117. for (int n=0; n < COURSE_COUNT; n++){
  118. //check for error
  119. if (5+n > STRING_COUNT){
  120. cout << ARRAY_ERROR << endl;
  121. //break;
  122. }
  123. //course number
  124. getline(file_input, nonNumeric[i][5+n]);
  125. if (nonNumeric[i][5+n].length() <= 0){
  126. cout << INV_STRING << endl;
  127. continue;
  128. }
  129. numeric2[i][n][5] = 0.0;
  130.  
  131. for (int x=0; x<5; x++){
  132. //check for error
  133. if (x > GRADE_COUNT){
  134. cout << ARRAY_ERROR << endl;
  135. // break;
  136. }
  137.  
  138. //exam score
  139. file_input >> numeric2[i][n][x];
  140. file_input.ignore();
  141. if (numeric2[i][n][x] <1.0 || numeric2[i][n][x] > 100.0){
  142. cout << INV_FLOAT << endl;
  143. // break;
  144. }
  145.  
  146. //compute grade #
  147. switch (x) {
  148. case 0:
  149. numeric2[i][n][5] += numeric2[i][n][x] * .1;
  150. break;
  151. case 1:
  152. case 2:
  153. numeric2[i][n][5] += numeric2[i][n][x] * .15;
  154. break;
  155. case 3:
  156. numeric2[i][n][5] += numeric2[i][n][x] * .2;
  157. break;
  158. case 4:
  159. numeric2[i][n][5] += numeric2[i][n][x] * .4;
  160. break;
  161.  
  162.  
  163. }
  164. }
  165.  
  166. if (numeric2[i][n][5] >= GRADE_A){
  167. letterGrade[i][n] = 'A';
  168. }else if (numeric2[i][n][5] >= GRADE_B) {
  169. letterGrade[i][n] = 'B';
  170. }else if (numeric2[i][n][5] >= GRADE_C) {
  171. letterGrade[i][n] = 'C';
  172. }else if (numeric2[i][n][5] >= GRADE_D) {
  173. letterGrade[i][n] = 'C';
  174. } else {
  175. letterGrade[i][n] = 'F';}
  176.  
  177. }
  178. }
  179. //close input file
  180. file_input.close();
  181.  
  182.  
  183. //open output file
  184. file_output.open("Project4_David_Janicki_Output.txt");
  185.  
  186. // student loop
  187. for (int i=0; i<num_students; i++){
  188. //check for error
  189. if (i < STUDENT_COUNT) {
  190. cout << ARRAY_ERROR << endl;
  191. // break;
  192. }
  193. //write information to file
  194. file_output << "============================================================================================" << endl;
  195. file_output << setw(46) << "Student Grade Sheet" << endl;
  196. file_output << "============================================================================================" << endl;
  197. file_output << setw(33) << "Name of Student: \t" << nonNumeric[i][0] << endl;
  198. file_output << setw(33) << "Student ID: \t" << nonNumeric[i][1] << endl;
  199. file_output << setw(33) << "Student Address: \t" << nonNumeric[i][2] << endl;
  200. file_output << setw(33) << "Student Telephone Number: \t" << nonNumeric[i][3] << endl;
  201. file_output << setw(33) << "Student Social Security # \t" << nonNumeric[i][4] << endl;
  202. file_output << setw(33) << "Student Age: \t" << numeric1[i][0] << endl;
  203. file_output << setw(33) << "number of years at Texas State: \t" << numeric1[i][1] << endl;
  204.  
  205. //course loop
  206. for (int n=0; n < COURSE_COUNT; n++){
  207. //check for error
  208. if (5+n > STRING_COUNT){
  209. cout << ARRAY_ERROR << endl;
  210. // break;
  211. }
  212. //write course # to file
  213. file_output << endl << setw(33) << "Course Number \t" << nonNumeric[i][5+n] << endl;
  214. file_output << "--------------------------------------------------------------------------------------------" << endl;
  215. //exam loop
  216. for (int x=0; x<5; x++){
  217. //check for error
  218. if (x > GRADE_COUNT){
  219. cout << ARRAY_ERROR << endl;
  220. // break;
  221. }
  222.  
  223. file_output << setw(33) << "Exam #" << x+1 << ": \t" << fixed << showpoint <<
  224. setprecision(2) << numeric2[i][n][x] << endl;
  225. }
  226. //write grades to file
  227. file_output << setw(28) << "Numerical Grade in " << nonNumeric[i][5+n] <<
  228. ": \t" << fixed << showpoint << setprecision(2) << numeric2[i][n][5] << endl;
  229. file_output << setw(28) << "Letter Grade in " << nonNumeric[i][5+n] <<
  230. ": \t" << letterGrade[i][n] << endl;
  231.  
  232. //write warning or commendation
  233. if (numeric2[i][n][5] < WARN_NUM)
  234. file_output << setw(37) << "Comment: \t" << WARN << endl;
  235. else if (numeric2[i][n][5] >= COMMENDATION_NUM)
  236. file_output << setw(37) << "Comment: \t" << COMMENDATION << endl;
  237. }
  238. //file_output << endl << endl;
  239. }
  240. //close file
  241. file_output.close();
  242.  
  243. cout << "Data written to file" << endl;
  244.  
  245. return 0;
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement