grading program found only one of three incorrect answers

Can anyone see something wrong the compareAnswers function or the reading of the text files into the arrays. The files contain one character per line and 20 characters total. Is there a way to make sure that operators start at the beginning of the file or array?

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
119
120
 //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 <iostream>
#include <fstream>
#include <string>

using namespace std;

//Define size variable for CorrectAnswers and StudentAnswers.
const int SIZE = 20;

//Define the function prototypes.
void readFile(ifstream &, char[], const int);
void compareAnswers(char[], char[], int &, int &, const int);
void TestResults(char[], char[], const int SIZE);

int main()
{
	//Print my name as the first line of output in this program
	cout << "June Stine" << endl;

	//Define an array to hold the CorrectAnswers and the student answers.
	char CorrectAnswers[SIZE], StudentAnswers[SIZE];
	
	//Create ifstream object and open correct answers file
	ifstream inFile;
	inFile.open("CorrectAnswers.txt");

	//Read the CorrectAnswers into the array.
	readFile(inFile, CorrectAnswers, SIZE);

	inFile.open("StudentAnswers.txt");

	//Read the student answers file into an array.
	readFile(inFile, StudentAnswers, SIZE);
	
	//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) * 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.
	cout << "\n\nTotal of incorrect answers:  " << incorrect << endl;
	cout << "The percentage correct:  " << percentage << "%" << endl;

	//Determine whether the student passed or failed.
	if (correct > 14)
		cout << "\n\nThe student passed the exam.";
	else
		cout << "\n\nThe student failed the exam.";

	cout << "\n\nPress the enter key to exit";
	cin.ignore(cin.rdbuf()->in_avail() + 1);

	return 0;
}

//Define the readFile function to get the answers into arrays.
void readFile(ifstream &inFile, char answers[], const int SIZE)
{
	for (int i = 0; i < SIZE; i++)
		inFile.get(answers[i]);
	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; 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)
{
	cout << "Student's Answer\tCorrect Answer" << endl;
	cout << "\n";
	for (int i = 0; i < SIZE; i++)
	{
		if (StudentAnswers[i] != CorrectAnswers[i])
			cout << "Question "<<(i + 1) << ": " << StudentAnswers[i] << "\t\t" << CorrectAnswers[i] << endl;
	}
}

June Stine
Student's Answer        Correct Answer
Question 7: D           C
Total of incorrect answers:  1
The percentage correct:  95%

The student passed the exam.
Press the enter key to exit
ADBDAADBDCAABACCCBCA
ADBCAADBDCADBACCDBCA

 
Actually, it works fine if your answers are laid out in one line as at the bottom of the post! This made me wonder why you were asking the question.

If they are actually 1 per line, however, then you need to change line 80 to
inFile >> answers[i];
The files contain one character per line

In case your “CorrectAnswers.txt” and “StudentAnswers.txt” were ‘in vertical’ like this:
A
D
B
C
A
A
D
B
D
C
A
D
B
A
C
C
D
B
C
A

you’d need to get rid of the terminating ‘\n’ in each line:
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';
    }
}

Output:
June Stine
CorrectAnswers: ADBDAADBDCAABACCCBCA
StudentAnswers: ADBCAADBDCADBACCDBCA
Student's Answer        Correct Answer

Question 4: C           D
Question 12: D          A
Question 17: D          C


Total of incorrect answers:  3
The percentage correct:  85%


The student passed the exam.

Press the enter key to exit

Thanks so much for the help. This was faster than I expected.
Registered users can post here. Sign in or register to post.