Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ### 27th Jumada Al-Akhirah 1436.
- Khalid was here.
- Notes on improving this code
- =============================
- 1. That is not the proper way to encode time into a cout string. Printing time is a tricky matter,
- ask your lecturer how she would do it. I didn't bother since I've never done this sort of thing.
- References: a. http://en.cppreference.com/w/cpp/chrono/c/time
- b. http://www.cplusplus.com/reference/ctime/strftime/
- c. http://www.tutorialspoint.com/cplusplus/cpp_date_time.htm
- 2. You had changed the layout of the original input file provided by the lecturer. It is quite possible
- to program around it. No, I don't think I'll have the time to do it.
- 3. The lecturer has told us that there would only be 10 students. This is strange. If she says that it can
- be changed, then account for it accordingly.
- 4. I didn't add the condition to check whether input.txt is available in the root folder or not. I'm sure
- you can try to figure this out.
- 5. Style is everything ! :)
- */
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <iomanip>
- using namespace std;
- char getLetterGrade(int grade){
- char letterGrade;
- if (grade <= 100 && grade >= 0) {
- if (grade > 69)
- letterGrade = 'A';
- else if (grade > 59)
- letterGrade = 'B';
- else if (grade > 49)
- letterGrade = 'C';
- else if (grade > 39)
- letterGrade = 'D';
- else letterGrade = 'F';
- }
- else letterGrade = 'X';
- return letterGrade;
- }
- void getInputFromFileAndOutput() {
- ifstream infile;
- ofstream outfile;
- infile.open("input.txt");
- outfile.open("output.txt");
- string name;
- int assign1, assign2, test, quiz, exam;
- int coursework;
- int total;
- char studentLetterGrade;
- int As = 0, Bs = 0, Cs = 0, Ds = 0, Fs = 0;
- outfile << left << setw(20) << "Name";
- outfile << left << setw(20) << "Coursework(70%)";
- outfile << left << setw(20) << "Exam(30%)";
- outfile << left << setw(20) << "Overall Score(100%)";
- outfile << left << setw(20) << "Grade" << endl;
- for (int i = 0; i < 10; i++) {
- infile >> name >> assign1 >> assign2 >> test >> quiz >> exam;
- coursework = assign1 + assign2 + test + quiz;
- total = coursework + exam;
- studentLetterGrade = getLetterGrade(total);
- switch(studentLetterGrade) {
- case ('A'):
- As ++;
- break;
- case ('B'):
- Bs ++;
- break;
- case ('C'):
- Cs ++;
- break;
- case ('D'):
- Ds ++;
- break;
- case ('F'):
- Fs++;
- break;
- }
- outfile << left << setw(20) << name;
- outfile << left << setw(20) << coursework;
- outfile << left << setw(20) << exam;
- outfile << left << setw(20) << total;
- outfile << left << setw(20) << studentLetterGrade << endl;
- }
- infile.close();
- outfile.close();
- cout<< "\nResult Summary\tDate:13 April 2015" << endl;
- cout<< "Subject:Programming Methodology" << endl << endl;
- cout<< "Grade\tStudent" << endl;
- cout<< "A\t" << As << endl;
- cout<< "B\t" << Bs << endl;
- cout<< "C\t" << Cs << endl;
- cout<< "D\t" << Ds << endl;
- cout<< "F\t" << Fs << endl;
- // The question assumes only 10 students
- cout << endl << "Total Students: " << 10 << endl;
- }
- int main() {
- getInputFromFileAndOutput();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment