Here is my code which compares answers in two files and outputs correct answer and wrong. The problem is why I can't output all elements of array inside function.
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//prototypes of functions
void openAnswerKeyFile(ifstream&);
// put into array
void readAnswerKeyFile(string answerArray[], ifstream&);
void openStudentFile(ifstream&);
//void openOutputFile(ofstream&);
void readStudentAnswers(string studentAnswersArray[], ifstream&);
int compare(string answers[], string studentAnswersArray[], bool missedQuestions[]);
int main() {
// creates object answerFile of type ifstream
string answers[25];
string studentAnswersArray[27];
bool missedQuestions[25] = {false};
ifstream answerFile;
openAnswerKeyFile(answerFile);
ifstream studentAnswers;
openStudentFile(studentAnswers);
//ofstream outputFile;
//openOutputFile(outputFile);
readAnswerKeyFile(answers, answerFile);
readStudentAnswers(studentAnswersArray, studentAnswers);
int numberOfWrong= compare(answers, studentAnswersArray, missedQuestions);
//outputFile.close();
return 0;
}
// opens answer file
void openAnswerKeyFile(ifstream& answerFile) {
// filenames are declared as an array because
// ifstream class method open requires char as parameter
char answerFilename[] = "Answer Key 1.txt";
char answerFilename2[] = "Answer Key 2.txt";
char answerFilename3[] = "Answer Key 3.txt";
string userInput = "";
while (userInput != answerFilename && userInput != answerFilename2 && userInput != answerFilename3) {
cout << "Enter answer key filename: " << endl;
getline(cin, userInput);
}
// take from user number of file. Number is located at index 11
answerFile.open(answerFilename);
}
void openStudentFile(ifstream& studentAnswers) {
// filenames are declared as an array because
// ifstream class method open requires char as parameter
char studentAnswerFilename[] = "Student Answers 1.txt";
char studentAnswerFilename2[] = "Student Answers 2.txt";
char studentAnswerFilename3[] = "Student Answers 3.txt";
string userInput = "";
while (userInput != studentAnswerFilename && userInput != studentAnswerFilename2 && userInput != studentAnswerFilename3) {
cout << "Enter student answers filename: " << endl;
getline(cin, userInput);
}
// takes from user number of file. Number is located at index 16
studentAnswers.open(studentAnswerFilename);
}
void openOutputFile(ofstream& outputFile) {
char outputFilename[] = "";
cout << "Enter output filename (max 25 characters): " << endl;
//takes input from user
cin.getline(outputFilename, 25);
// open file
outputFile.open(outputFilename);
if (!outputFile.is_open()) {
cout << "Cant open file";
}
}
void readAnswerKeyFile(string answerArray[], ifstream& answerFile) {
string line;
short i = 0;
while (answerFile.good()) {
getline(answerFile, line);
answerArray[i] = line;
i++;
}
answerFile.close();
}
void readStudentAnswers(string studentAnswersArray[], ifstream& studentAnswers) {
string line;
short i = 0;
while (studentAnswers.good()) {
getline(studentAnswers, line);
studentAnswersArray[i] = line;
i++;
}
studentAnswers.close();
}
int compare(string answers[], string studentAnswersArray[], bool missedQuestions[]) {
short numberOfWrong=0;
for (short i = 0, j = 2; i < 25, j < 27; i++, j++) {
if (answers[i] != studentAnswersArray[j]) {
missedQuestions[i] = true;
numberOfWrong++;
cout << answers[i];
}
}
return numberOfWrong;
}
Part of code which is confusing is this :
if (answers[i] != studentAnswersArray[j]) {
missedQuestions[i] = true;
numberOfWrong++;
cout << answers[i];
}
I got as output only 1 element
If you want to compile make 2 text files named Answer Key 1.txt with following content:
A
B
B
D
and so on (25 items)
Next file is student Answers 1.txt with following:
id
name
A
D
C
D
(27 items)