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
|
/*This program reads the file answers.dat which contains quiz answers for a one student. The answers are compared to the answer key.
The incorrect answers are flagged and the quiz score is calculated. Report is printed*/
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
//Formula prototypes
void readFile(string &, vector<string> &, const int);
double calcPct(string, vector<string>, const int, vector<const string>);
void printRpt(string, vector<string>, vector<const string>, const int, double);
int main()
{
// Saves correct quiz answers into vector
const int NUMQS = 11; // number of questions
// answer key vector
vector<const string> KEY = { "c++", "for", "if", "variable", "function", "return", "array", "void", "reference", "main", "prototype" };
vector<string> ans(11); // initializes vector to hold student answers
string name; // student name
double pctScore = 0;
readFile(name, ans, NUMQS); //calls function readFile
pctScore = calcPct(name, ans, NUMQS, KEY); //calls calcPct function
printRpt(name, ans, KEY, NUMQS, pctScore); //calls printRpt function
system("pause");
return 0;
}
//**************************************************************************
// Definition of readFile. *
// Reads answer.dat file containing student's answer into the ans vector. *
//**************************************************************************
void readFile(string &studentName, vector<string> &studentAnswers, const int NUMQUESTIONS)
{
ifstream inputFile;
inputFile.open("answers.dat"); // Opens input file
getline(cin, studentName); // Writes student's name on to variable
for (int i = 0; i < NUMQUESTIONS; i++) // For loop writes student's answers on to the studentAnswers vector
inputFile >> studentAnswers[i];
}
//**************************************************************************
// Definition of calcPct. *
// Calculates percentage scored on quiz. *
//**************************************************************************
double calcPct(string studentName, vector<string> studentAnswers, const int NUMQUESTIONS, vector<const string> ANSKEY)
{
int correctCounter = 0;
double score;
for (int i = 0; i < NUMQUESTIONS; i++)
{
if (studentAnswers[i] == ANSKEY[i])
correctCounter += 1;
}
score = (correctCounter / NUMQUESTIONS) * 100;
return score;
}
//**************************************************************************
// Definition of printRpt. *
// Prints report of student name, student's answers, answer key, incorrect *
// answers and percentage scored. *
//**************************************************************************
void printRpt(string studentName, vector<string> studentAnswers, vector<const string> ANSKEY, const int NUMQUESTIONS, double score)
{
cout << "!!! Marie Dominguez' Most Excellent Quiz Reporter!!!\n\n";
cout << "Report for " << studentName;
cout << setw(20) << "CORRECT ANS" << setw(20) << "STUDENT ANS\n";
for (int i = 0; i < NUMQUESTIONS; i++)
{
cout << setw(20) << ANSKEY[i] << setw(20) << studentAnswers[i];
if (ANSKEY[i] != studentAnswers[i])
cout << setw(15) << "INCORRECT";
cout << endl;
}
cout << "QUIZ SCORE\n";
cout << score;
}
|