khalid_hussain

Saqib Shabir Assignment

Apr 15th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. /*
  2. ### 27th Jumada Al-Akhirah 1436.
  3. Khalid was here.
  4.  
  5. Notes on improving this code
  6. =============================
  7. 1.  That is not the proper way to encode time into a cout string. Printing time is a tricky matter,
  8.     ask your lecturer how she would do it. I didn't bother since I've never done this sort of thing.
  9.     References: a. http://en.cppreference.com/w/cpp/chrono/c/time
  10.                 b. http://www.cplusplus.com/reference/ctime/strftime/
  11.                 c. http://www.tutorialspoint.com/cplusplus/cpp_date_time.htm
  12. 2.  You had changed the layout of the original input file provided by the lecturer. It is quite possible
  13.     to program around it. No, I don't think I'll have the time to do it.
  14. 3.  The lecturer has told us that there would only be 10 students. This is strange. If she says that it can
  15.     be changed, then account for it accordingly.
  16. 4.  I didn't add the condition to check whether input.txt is available in the root folder or not. I'm sure
  17.     you can try to figure this out.
  18. 5.  Style is everything ! :)
  19. */
  20. #include <iostream>
  21. #include <fstream>
  22. #include <string>
  23. #include <iomanip>
  24. using namespace std;
  25.  
  26. char getLetterGrade(int grade){
  27.     char letterGrade;
  28.  
  29.     if (grade <= 100 && grade >= 0) {
  30.         if (grade > 69)
  31.             letterGrade = 'A';
  32.         else if (grade > 59)
  33.             letterGrade = 'B';
  34.         else if (grade > 49)
  35.             letterGrade = 'C';
  36.         else if (grade > 39)
  37.             letterGrade = 'D';
  38.         else letterGrade = 'F';
  39.     }
  40.     else letterGrade = 'X';
  41.  
  42.     return letterGrade;
  43. }
  44.  
  45. void getInputFromFileAndOutput() {
  46.     ifstream infile;
  47.     ofstream outfile;
  48.     infile.open("input.txt");
  49.     outfile.open("output.txt");
  50.  
  51.     string name;
  52.     int assign1, assign2, test, quiz, exam;
  53.     int coursework;
  54.     int total;
  55.     char studentLetterGrade;
  56.     int As = 0, Bs = 0, Cs = 0, Ds = 0, Fs = 0;
  57.  
  58.     outfile << left << setw(20) << "Name";
  59.     outfile << left << setw(20) << "Coursework(70%)";
  60.     outfile << left << setw(20) << "Exam(30%)";
  61.     outfile << left << setw(20) << "Overall Score(100%)";
  62.     outfile << left << setw(20) << "Grade" << endl;
  63.  
  64.     for (int i = 0; i < 10; i++) {
  65.         infile >> name >> assign1 >> assign2 >> test >> quiz >> exam;
  66.         coursework = assign1 + assign2 + test + quiz;
  67.         total = coursework + exam;
  68.         studentLetterGrade = getLetterGrade(total);
  69.  
  70.         switch(studentLetterGrade) {
  71.             case ('A'):
  72.                     As ++;
  73.                     break;
  74.             case ('B'):
  75.                     Bs ++;
  76.                     break;
  77.             case ('C'):
  78.                     Cs ++;
  79.                     break;
  80.             case ('D'):
  81.                     Ds ++;
  82.                     break;
  83.             case ('F'):
  84.                     Fs++;
  85.                     break;
  86.         }
  87.  
  88.         outfile << left << setw(20) << name;
  89.         outfile << left << setw(20) << coursework;
  90.         outfile << left << setw(20) << exam;
  91.         outfile << left << setw(20) << total;
  92.         outfile << left << setw(20) << studentLetterGrade << endl;
  93.     }
  94.  
  95.     infile.close();
  96.     outfile.close();
  97.  
  98.     cout<< "\nResult Summary\tDate:13 April 2015" << endl;
  99.     cout<< "Subject:Programming Methodology" << endl << endl;
  100.     cout<< "Grade\tStudent" << endl;
  101.     cout<< "A\t" << As << endl;
  102.     cout<< "B\t" << Bs << endl;
  103.     cout<< "C\t" << Cs << endl;
  104.     cout<< "D\t" << Ds << endl;
  105.     cout<< "F\t" << Fs << endl;
  106.  
  107.     // The question assumes only 10 students
  108.     cout << endl << "Total Students: " << 10 << endl;
  109. }
  110.  
  111. int main() {
  112.     getInputFromFileAndOutput();
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment