Help on homework problem

Hello everyone, my homework requires us to make a program for an exam grader. The program reads in two different text files and puts them into two separate arrays; one for the correct answers and one with all of the students answers. Every twenty lines in the StudentAnswers text file represents one student. My problem is that my comparison function is seemingly not doing what I'm wanting it to, if anyone has any suggestions I'd appreciate it greatly!
*EDIT*

I should have said that in Visual C++ it says the student missed 20 out of 20 questions for every student

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
#include<iostream>
#include<fstream>

using namespace std;

int compareAns(char [], int, char);
int calcAverage(int);

int main()
{
   const int SIZE_ONE = 20;
   const int SIZE_TWO = 220;
   char correct[SIZE_ONE];
   char student[SIZE_TWO];
   int questCount = 1, stuCount = 1,   // questCount is counter for times for loop has ran, stuCount increases every 20 questCounts
      ansCount = 0, i = 0,             // ansCount is counter for wrong answers
      answers, average;                // answers is variable for compareAns function, average is variable for calcAverage function
   ifstream inputFile1, inputFile2;

   inputFile1.open("CorrectAnswers.txt");

   while (i < SIZE_ONE && inputFile1 >> correct[i])
      i++;

   inputFile1.close();
   i = 0;
   inputFile2.open("StudentAnswers.txt");

   while (i < SIZE_TWO && inputFile2 >> student[i])
      i++;

   inputFile2.close();

   for (i = 0; i < SIZE_TWO; i++)
   {
      answers = compareAns(correct, SIZE_ONE, student[i]);
      if (answers >= 0)
         ansCount++;

      if (questCount % 20 == 0)
      {
         average = calcAverage(ansCount);
         cout << "Report for Student " << stuCount << endl;
         cout << "---------------------" << endl;
         cout << "Missed " << ansCount << " out of 20 questions "
            << "for " << average << "% correct." << endl;
         cout << "Questions missed:" << endl;
         cout << " " << endl;
         if (average >= 70)
            cout << "This student passed the exam!" << endl << endl;
         else
            cout << "This student failed the exam." << endl << endl;
         stuCount++;
         ansCount = 0;
      }
      
      questCount++;
   }

   return 0;
}

// Description of compareAns:
// Acts as a linear search algorithm to compare the two arrays
// for correct answers and student answers. Return value based off if
// the two answers are equal or not.

int compareAns(char correctAns[], int size, char stdAns)
{
   for (int j = 0; j < size; j++)
   {
      if (stdAns == correctAns[j])
         return j;
   }

   return -1;
}

int calcAverage(int wrongAns)
{
   int correctAns, avg;

   correctAns = 20 - wrongAns;
   avg = correctAns / 20 * 100;

   return avg;
}
Last edited on
Line 84. CorrectAns is an int. 20 is an int. Integer division discards remainder and returns an integer. Therefore, if CorrectAns==20, then you get 1, but in every other case you get 0.
Ah, yeah I missed that when debugging. However that wasn't quite the bug I was having an issue with; my compareAns function doesn't seem to do what I want it to do. When it compares either every question is wrong or every question is right (based of what I put in the if statement on my main for ansCount)
Lets see if I understand the question correctly:
* There are 20 questions.
* For each question the answer is one character.
* You should compare student's answer to question number N to the correct answer for the question number N.

That is not what you are doing.

The std::valarray is no longer fashionable, but perhaps this code gives an idea:
1
2
3
4
5
6
7
8
9
10
11
constexpr int N =  5;
std::valarray<char> foo ( N );
// read values to foo

std::valarray<char> bar ( N );
while (something) {
  // read values to bar
  auto gaz = ( foo == bar );
  auto count = std::count( std::begin(gaz), std::end(gaz), true );
  std::cout << 100 * count / N << '\n';
}
Okay I've updated my program. I no longer have the compareAns function and everything is working nicely! Except for one thing

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
116
117
118
#include<iostream>
#include<fstream>

using namespace std;

double calcAverage(int);

int main()
{
   const int SIZE_ONE = 20;
   const int SIZE_TWO = 220;
   char correct[SIZE_ONE];
   char student[SIZE_TWO];
   int wrong[SIZE_ONE];                    // array that holds the wrong answers
   int ansCount = 0, i = 0,                // ansCount totals number of wrong answers
      lowestGrade = 100, highestGrade = 0, // Self-explanatory
      lowestStudent, highestStudent;       // Tracks which student has lowest and highest grade respectively
   double average;                         // average is variable for calcAverage function
   ifstream inputFile1, inputFile2;

   // Open and transfer file one into an array
   inputFile1.open("CorrectAnswers.txt");
   while (i < SIZE_ONE && inputFile1 >> correct[i])
      i++;

   // Close file
   inputFile1.close();
   i = 0;

   // Open and transfer file two into an array
   inputFile2.open("StudentAnswers.txt");
   while (i < SIZE_TWO && inputFile2 >> student[i])
      i++;
   
   // Close file
   inputFile2.close();

   for (i = 0; i < 11; i++)
   {
      for (int j = 0; j < SIZE_ONE; j++)
      {
         if (student[j + i * 20] != correct[j])
         {
            wrong[ansCount] = j + 1;
            ansCount++;
         } // end if
      } // end for (int j=0..)

      // Calculate average
      average = calcAverage(ansCount);

      // Output
      cout << "Report for student " << i + 1 << ":" << endl;
      cout << "---------------------" << endl;
      cout << "Missed " << ansCount << " out of 20 questions "
         << "for " << average << "% correct." << endl;
      cout << "Questions missed: " << endl;
      if (ansCount == 0)
         cout << "  None missed." << endl;

      for (int k = 0; k < ansCount; k++)
      {
         cout << "  " << wrong[k] << "(" << student[wrong[k] - 1] << "/"
            << correct[wrong[k] - 1] << ")";
         if (k + 1 == ansCount)
         {
            cout << endl;
            break;
         }
         if (ansCount > 1)
            cout << ",";
      } // end for(int k=0; ...)

      // Check to see if student passes or fails
      if (average >= 70)
         cout << "This student passed the exam!" << endl << endl;
      else
         cout << "This student failed the exam." << endl << endl;

      // Checking for which student has lowest grade
      // and which student has highest grade
      if (average < lowestGrade)
      {
         lowestGrade = average;
         lowestStudent = i + 1;
      }
      else if (average > highestGrade)
      {
         highestGrade = average;
         highestStudent = i + 1;
      }

      // Reset wrong answer count
      ansCount = 0;
   } // end for(i=0; ...)

   // Output of students with highest and lowest grades
   cout << "Student " << highestStudent << " had the highest grade with "
      << highestGrade << "%." << endl;
   cout << "Student " << lowestStudent << " had the lowest grade with "
      << lowestGrade << "%." << endl;

   return 0;
} // end int main()

// Description of calcAverage()
// Simple function that calculates the average based off of the counted
// wrong answers from the second for loop in the main function.

double calcAverage(int wrongAns)
{
   double correctAns, avg;

   correctAns = 20 - wrongAns;
   avg = correctAns / 20 * 100;

   return avg;
} // end double calcAverage() 


In my for loop that uses index 'k', it seems that it's getting the wrong answers from my student array as well as the correct array. It works as intended on the first Student output but doesn't work for any of the other ones. Anyone have any idea as to why?
OH BABY NEVERMIND I FIXED IT! Oh it feels so good!
Topic archived. No new replies allowed.