Reputation: 41
I have been trying to loop a series of answers that match in two arrays. The code runs through the loop each time and outputs the first 20 questions, but won't go on to the next set to check those answers.
answeredCorrectly is a counter studentAnswers is an array of 100 questions being matched to the array correctAnswers sized at 20. QUESTIONS is the constant int valued at 20 and paired with correctAnswers.
I tried forgoing the for loop structure and just use while loops that checked the position in studentAnswers, but that resulted in a crash.
Any ideas to continue the counter for each of the other students instead of sticking with the first 20 answers?
Here is the section of code that I am dealing with.
EDIT Added the function createReport2()
EDIT 2 Added updated code
string createReport2 (int questNum, char stuResponse, char correctResponse)
{
stringstream ss; // the string stream object that will be used for the conversions
// Add the various elements to the stringstream object.
ss << questNum << "(" << stuResponse << "/" << correctResponse << ")";
// Return the final result as a C++ string class.
return ss.str();
} // end function string createReport (int questNum, char stuResponse, char correctResponse)
int main()
{
const int QUESTIONS = 20;
const int STUDENT_QUESTIONS = 100;
ifstream inputFile;
inputFile.open("CorrectAnswers.txt");
char correctAnswers[QUESTIONS];
for (int i=0; i<20; i++)
{
inputFile >> correctAnswers[i];
}
ifstream inputFile2;
inputFile2.open("StudentAnswers.txt");
char studentAnswers[STUDENT_QUESTIONS];
for (int t=0; t<STUDENT_QUESTIONS; t++)
{
inputFile2 >> studentAnswers[t];
}
int answeredCorrectly = 0;
string name;
for(int c = 0; c < 5; c++)
{
string arr[100];
for (int x=0; x < QUESTIONS; x++)
{
if (studentAnswers[(5*c)+x] == correctAnswers[x])
answeredCorrectly++;
else
arr[c*5+x] = createReport2(x,studentAnswers[c*5+x],correctAnswers[x]);
}
cout << "Report for Student " << c+1 << ":" << endl;
cout << "---------------------" << endl;
cout << "Missed " << 20 - answeredCorrectly << " out of 20 questions for " << (answeredCorrectly / 20.0) * 100 << "% correct." << endl;
cout << "Answered Correctly: " << answeredCorrectly << endl;
cout << "Questions missed:";
for (int i = 0; i < 20; i++)
{
cout << arr[c*5+i];
}
cout << endl << endl;
answeredCorrectly = 0;
}
}
Upvotes: 1
Views: 194
Reputation: 2432
I think a quick fix would be to put studentAnswers[(20*c)+x]
whereever you have studentAnswers[x]
.
You should actually have student answers in a 2 dimensional array (studentAnswers[STUDENTS][QUESTIONS]
, where STUDENTS would be 5) though.
There's a possible of-by-1 in createReport2()
, where questNum could possibly be changed to questNum+1
.
Also, declare string arr[20]
inside the main for loop. OR Change arr[x]
to arr[c*20+x]
, and arr[i]
to arr[c*20+i]
.
Edit: So to summarize
for(int c = 0; c < 5; c++)
{
for (int x=0; x < QUESTIONS; x++)
{
if (studentAnswers[c*20+x] == correctAnswers[x])
answeredCorrectly++;
else
arr[c*20+x] = createReport2(x,studentAnswers[c*20+x],correctAnswers[x]);
}
cout << "Report for Student " << c+1 << ":" << endl;
cout << "---------------------" << endl;
cout << "Missed " << 20 - answeredCorrectly
<< " out of 20 questions for " << (answeredCorrectly / 20.0) * 100
<< "% correct." << endl;
cout << "Answered Correctly: " << answeredCorrectly << endl;
cout << "Questions missed:";
for (int i = 0; i < 20; i++)
{
cout << arr[c*20+i];
}
cout << endl << endl;
answeredCorrectly = 0;
}
Upvotes: 4
Reputation: 174
I think you need a separate variable for incorrect answers something like answeredIncorrectly.
for (int c = 0; c < 5; c++) {
int answeredCorrectly = 0;
int answeredIncorrectly = 0;
for (int x = 0; x < QUESTIONS; x++) {
if (studentAnswers[x] == correctAnswers[x])
answeredCorrectly++;
else {
arr[answeredIncorrectly] = createReport2(x, studentAnswers[x],
correctAnswers[x]);
answeredIncorrectly++;
}
}
cout << "Report for Student " << c + 1 << ":" << endl;
cout << "---------------------" << endl;
cout << "Missed " << 20 - answeredCorrectly
<< " out of 20 questions for "
<< (answeredCorrectly / 20.0) * 100 << "% correct." << endl;
cout << "Answered Correctly: " << answeredCorrectly << endl;
cout << "Questions missed:";
for (int i = 0; i < answeredIncorrectly; i++) {
cout << arr[i];
}
cout << endl << endl;
answeredCorrectly = 0;
}
Upvotes: 0