Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab 02 - Question #1 solution
- #include <iostream>
- #include <fstream>
- #include <cstring>
- using namespace std;
- const int NO_OF_QUESTIONS = 20;
- //function to return grade given score
- char examGrade(int score)
- {
- float percent = ((float) score / (NO_OF_QUESTIONS * 2)) * 100;
- if (percent < 60)
- return 'F';
- else if (percent >= 60 && percent < 70)
- return 'D';
- else if (percent >= 70 && percent < 80)
- return 'C';
- else if (percent >= 80 && percent < 90)
- return 'B';
- else
- return 'A';
- }
- int main()
- {
- char keyString[NO_OF_QUESTIONS + 1];
- char answerString[NO_OF_QUESTIONS + 1];
- char ID[9];
- char ch;
- int score;
- int len;
- ifstream infile;
- //should take this open to a function
- infile.open("Lab02_Q01_Data.txt");
- if (!infile)
- {
- cout << "Cannot open the input file. Program terminates!"
- << endl;
- return 1;
- }
- cout << "Processing Data" << endl << endl;
- infile.get(keyString, NO_OF_QUESTIONS + 1);
- cout << "Key: " << keyString << endl << endl;
- cout << endl << endl;
- cout << " ID " << "\t\tAnswers \tScore \t\tGrade" << endl << endl;
- //reading the first line
- while (!infile.eof())
- {
- score = 0;
- infile >> ID;
- infile.get(ch);
- infile.get(answerString, NO_OF_QUESTIONS + 1);
- for (int i = 0; i < NO_OF_QUESTIONS; i++)
- {
- if (answerString[i] == keyString[i])
- score += 2;
- else
- score--;
- }
- if (ID[0] != '\0')
- cout << ID << " " << answerString << "\t" << score << "\t\t" << examGrade(score) << endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement