1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
|
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
void getgrade(char answersheet[], char response[], float &percent, float &score, char &grade);
int main()
{
ifstream infile("Ch8_Ex6Data.txt");
char studentid[9];
char response[21];
char answersheet[21];
float score = 0;
float percent = 0;
char grade;
infile.getline(answersheet,21) ;
cout << answersheet << endl << endl;
cout << "Student ID --- Response --- Score --- Grade" << endl;
while ( infile.getline(studentid,9, ' ') && infile.getline(response, 21) )
{
cout << studentid << " '" << response << "' ";
getgrade(answersheet,response,percent, score, grade);
cout << score << " " << grade << endl;
score = 0;
percent = 0;
}
cout << answersheet << endl;
system("pause");
return 0;
}
void getgrade(char answersheet[], char response[], float &percent, float &score, char &grade)
{
for (int i = 0; i < 20; i++)
{
if (answersheet[i] == response[i])
score = score + 2;
else if (answersheet[i] = ' ')
score = score;
else if (answersheet[i] != response[i])
score = score - 1;
}
percent = ((score/40) * 100);
if (percent >= 90)
grade = 'A';
if (percent >= 80 && percent < 90)
grade = 'B';
if (percent >= 70 && percent < 80)
grade = 'C';
if (percent >= 60 && percent < 70)
grade = 'D';
if (percent < 60)
grade = 'F';
}
|