// This program reads a file with exam answers and an exam key, and prints the scores in a new file.
// ****************************************************************************************************************************
#include <fstream>
#include <string>
using namespace std;
void examine(string key, string test, ofstream outFile);
// This function takes the answer key, the exam information, and the output file variable
// and prints the student's ID number along with their score.
int main()
{
ifstream inFile; // Declares the input file variable
ofstream outFile; // Declares the output file variable
string key; // Declares the answer key string variable
string test; // Declares the string variable for each exam information
// Gets the first line from the file, which is the answer key
getline(inFile, key);
// Loop performs for each line of the file (or each exam)
while(inFile)
{
getline(inFile, test);
// examine function
examine(key, test, outFile);
}
// Closes files
inFile.close();
outFile.close();
return 0;
}
void examine(string key, string test, ofstream outFile)
// Pre: The answer key is provided as a string, the test information is provided as a string, and the output file has been opened.
// Post: The student ID and their score is written in the output file.
{
int length; // Declares the int variable for the length of the answers string
string idnumber; // Declares string variable for the student's ID number
string answers; // Declares string variable for the student's answers
// Breaks the test string into the student ID and answers strings
idnumber = test.substr(0, 7);
answers = test.substr(8, 30);
// Find the length of the answers string
length = answers.length();
if (length > 20) // If the length of the answers string is greater than 20, then print...
outFile << idnumber << " Too many answers" << endl;
else if (length < 20) // Else if the length of the answers string is less than 20, then print...
outFile << idnumber << " Too few answers" << endl;
else // Else...
{
int count = 0; // Assigns the int variable loop counter to 0
int score = 0; // Assigns the int variable for student score to 0
int invalid = 0; // Assigns the int variable invalid to 0, this tests the data input later
char indexKey; // Declares the char variable indexKey, the char taken from the answer key
char indexAnswer; // Declares the char variable indexAnswer, the char taken from the student's answers string
while (count < length) // While the iteration count is less than the length of the string
{
// Gets the char from the answer key
indexKey = key.at(count);
// Gets the char from the student's answers string
indexAnswer = answers.at(count);
if (indexKey == indexAnswer) // If both of the answers are the same...
score = score + 1; // ...add 1 to the students score
else if (indexAnswer != 'a' || indexAnswer != 'b' || indexAnswer != 'c' || indexAnswer != 'd' || indexAnswer != 'e' || indexAnswer != 'f')
invalid = invalid + 1; // Else if the answer is not a, b, c, d, e, or f, then add 1 to the invalid variable
// Increase the iteration counter by 1
count++;
}
if (invalid == 0) // If no answers were invalid, print...
outFile << idnumber << " " << score << endl;
else // Else print to the file that there were invalid answers
outFile << idnumber << " Invalid answers" << endl;
}
}
That's the thing! I don't see any errors! There aren't errors underlined anywhere! When I try to build it still says "There were build errors." I don't get it.