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
|
//Ch7Ex11
//June Stine
//This program reads CorrectAnswers.txt and StudentsAnswers.txt
//into separate char arrays, determines the number of missed answers,
//displays a list of questions missed with the student's answer
//and the correct answer, displays the total number of questions missed
//and displays the percentage correct. Finally the program
//detirmines if the percentage was a pass or fail and displays the correct message.
#include <fstream>
#include <iostream>
#include <limits>
#include <string>
//Define size variable for CorrectAnswers and StudentAnswers.
constexpr int SIZE = 21;
//Define the function prototypes.
void readFile(std::ifstream &, char[], const int);
void compareAnswers(char[], char[], int &, int &, const int);
void TestResults(char[], char[], const int);
int main()
{
//Print my name as the first line of output in this program
std::cout << "June Stine\n";
//Define an array to hold the CorrectAnswers and the student answers.
char CorrectAnswers[SIZE], StudentAnswers[SIZE];
//Create std::ifstream object and open correct answers file
std::ifstream inFile("CorrectAnswers.txt");
if(!inFile) {
std::cout << "File can't be opened. Exiting now.\n";
return 1;
}
//Read the CorrectAnswers into the array.
readFile(inFile, CorrectAnswers, SIZE);
std::cout << "CorrectAnswers: " << CorrectAnswers << '\n';
inFile.open("StudentAnswers.txt");
if(!inFile) {
std::cout << "File can't be opened.\nExiting now.\n";
return 1;
}
//Read the student answers file into an array.
readFile(inFile, StudentAnswers, SIZE);
std::cout << "StudentAnswers: " << StudentAnswers << '\n';
//Initialize accumulator for number of correct answers.
int correct = 0;
//Initialize accumulator for number of incorrect answers.
int incorrect = 0;
//check the student's answers against the correct answers.
compareAnswers(CorrectAnswers, StudentAnswers, correct, incorrect, SIZE);
//Calculate percentage correct
int percentage;
percentage = (static_cast <double>(correct) / (SIZE-1)) * 100;
//Display the student's incorrect answers with the correct answers.
TestResults(StudentAnswers, CorrectAnswers, SIZE);
//Display the total number of incorrect answers and the percentage correct.
std::cout << "\n\nTotal of incorrect answers: " << incorrect
<< "\nThe percentage correct: " << percentage << "%" << '\n';
//Determine whether the student passed or failed.
if (correct > 14) { std::cout << "\n\nThe student passed the exam."; }
else { std::cout << "\n\nThe student failed the exam."; }
std::cout << "\n\nPress the enter key to exit";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
//Define the readFile function to get the answers into arrays.
void readFile(std::ifstream &inFile, char answers[], const int size)
{
for (int i = 0; i < size-1; i++) {
char c = 0;
inFile.get(c);
if(c == '\n') { i--; }
else { answers[i] = c; }
}
answers[size] = '\0';
inFile.close();
}
//Define the compareAnswers function to detirmine the number of missed answers
//and correct answers.
void compareAnswers(char CorrectAnswers[], char StudentAnswers[], int &correct,
int &incorrect, const int size)
{
for (int i = 0; i < size - 1; i++)
{
if (CorrectAnswers[i] == StudentAnswers[i]) { correct++; }
else { incorrect++; }
}
}
//Display the student's incorrect answers with the correct answers.
void TestResults(char StudentAnswers[], char CorrectAnswers[], const int size)
{
std::cout << "Student's Answer\tCorrect Answer\n\n";
for (int i = 0; i < size - 1; i++)
{
if (StudentAnswers[i] != CorrectAnswers[i])
std::cout << "Question " << (i + 1) << ": " << StudentAnswers[i]
<< "\t\t" << CorrectAnswers[i] << '\n';
}
}
|