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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
void readDataFromFile(ifstream &, char[], string[], string[]); // Function Prototypes
void calculateScores(char[], string[], double[]);
void calculatedGrades(double[], char[]);
const int MAX_ANSWERS = 20; // Global constants/variable
const int MAX_STUDENTS = 200;
const double MAX_MARKS = 40.0;
int studentsCounter = 0;
int main()
{ // Declare variables
ifstream inFile;
string fileName;
char testAnswers[MAX_ANSWERS];
string studentID[MAX_STUDENTS];
string studentAnswers[MAX_STUDENTS];
double studentScores[MAX_STUDENTS];
char studentGrades[MAX_STUDENTS];
cout << "Enter the input file name: ";
cin >> fileName;
inFile.open(fileName);
if (!inFile) // If the file is not found, display error message.
{
cout << "The file " << fileName
<< " is not found. " << endl;
system("pause");
return 1;
}
readDataFromFile(inFile, testAnswers, studentID, studentAnswers); // Call Functions to read file & make calculations
calculateScores(testAnswers, studentAnswers, studentScores);
calculatedGrades(studentScores, studentGrades);
cout << "\nResults of the student's tests: " << endl; // Fancy format for readability
cout << "Student_ID\tStudent_Answers\tScore\tGrade" << endl;
cout << "---------------------------------"
<< "----------------------------------" << endl;
for (int student = 0; student < studentsCounter; student++)
{
cout << studentID[student] << "\t" // Output each student's information
<< studentAnswers[student] << "\t" // and test scores in columns
<< studentScores[student] << "\t"
<< studentGrades[student] << endl;
}
inFile.close(); // Close input file
system("pause");
return 0;
}
void readDataFromFile(ifstream &inFile, char testAnswers[], // Function that reads correct answers from
string studentID[], string studentAnswers[]) // first line in the input file, then the
{ // student's IDs and test answers
char ch;
int index;
for (int i = 0; i < MAX_ANSWERS; i++)
inFile >> testAnswers[i];
index = 0;
inFile >> studentID[index];
while (inFile)
{
studentsCounter++;
inFile.get(ch);
getline(inFile, studentAnswers[index]);
index++;
inFile >> studentID[index];
}
}
void calculateScores(char testAnswers[], // Function to calculate the score of
string studentAnswers[], // each student's test based on what
double studentScores[]) // answer is correct, incorrect, or incomplete
{
string answers;
double sum;
for (int i = 0; i < studentsCounter; i++)
{
answers = studentAnswers[i];
sum = 0;
for (int f = 0; f < answers.length(); f++)
{
if (answers[f] == testAnswers[f])
sum = sum + 2;
else
sum = sum - 1;
}
studentScores[i] = sum;
}
}
void calculatedGrades(double studentScores[], // Function to calculate what letter grade
char studentGrades[]) // a student recieves based on the score
{
double percentage;
for (int i = 0; i < studentsCounter; i++)
{
percentage = (studentScores[i] / MAX_MARKS) * 100;
if ((percentage >= 90) && (percentage <= 100))
studentGrades[i] = 'A';
else if ((percentage >= 80) && (percentage <= 89.99))
studentGrades[i] = 'B';
else if ((percentage >= 70) && (percentage <= 79.99))
studentGrades[i] = 'C';
else if ((percentage >= 60) && (percentage <= 69.99))
studentGrades[i] = 'D';
else
studentGrades[i] = 'F';
}
}
|