Please Help array char comparison

Write your question here.

So when running the program the array labeled CorrectAnswers outputs
perfectly. However the array studentAnswers outputs random symbols character or number. Any help would be greatly appreciated.

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

using namespace std;

int main()
{
	const int SIZE = 20;
	char studentAnswers [SIZE],
		 CorrectAnswers [SIZE];
	int count = 0,
            counter = 0,
            QuestionCounter = 1,
	    QuestionsCorrect = 0,
	    QuestionsIncorrect = 0,
	    Percentage = 0;
	ifstream inputFile;
	
	
	
	inputFile.open("CorrectAnswers.txt");
	
	while(count < SIZE && inputFile >> CorrectAnswers[count])
		count++;
		
	inputFile.close();
	
	inputFile.open("StudentAnswers.txt");
	
	while(count <SIZE && inputFile >> studentAnswers[count]);
		count++;
	
	inputFile.close();
	
	while(counter < SIZE)
	{
		if(CorrectAnswers[counter] == studentAnswers[counter])
		{
			
	         cout << "Question #" << QuestionCounter << " is incorrect.\n";
			QuestionCounter++;
			QuestionsIncorrect++;
		}
		
		else
		{
		 cout << "Question #" << QuestionCounter << " is correct.\n";
			QuestionsCorrect++;
		}
		counter++;
		
	}
	
	
	Percentage = (QuestionsCorrect / SIZE)*100;
	cout << "Student Answer\t" << "Correct Answer\t" << "Result.\n";
	for(int count =  0; count < SIZE; count++)
	{
		cout << CorrectAnswers[count];
                cout << "\t" << studentAnswers[count];
		
		if(studentAnswers[count] == CorrectAnswers[count])
		cout << "\tCorrect\n";
		else
		cout << "\tIncorrect\n";
  }
	
	cin.ignore();
	cin.get();
	return 0;
	



}

	
At lines 21 to 31, inputFile is used to read from two different files into the arrays
CorrectAnswers and studentAnswers. That looks almost ok. The part which looks wrong is the use of the same variable count in both cases.

The variable should be reset to zero, before commencing to read from the second file. But then of course, the existing value will be lost. So I think two separate variables need to be used - after all there's no guarantee that the number of items read from the two files will be the same.

Later, there are loops such as at line 35 while(counter < SIZE) and at line 57 for(int count = 0; count < SIZE; count++) where it is assumed that the arrays will contain exactly SIZE items. But what happens if one or other of the files contained fewer than that?
Fixed the count variables to different variables for each one and in this particular program it will only be reading from these 2 files already in the computer due to it being an assignment. So the const variable SIZE is being used for both arrays and each file contains the exact same number of characters. For whatever reason it is still doing the same output for the second file I commented everything out and did a
for(int countD = 0; countD < SIZE; countD++)
cout << studentAnswers[countD];

and it gives the exact same output as from the for loop comparing them it seems there is something wrong with that array or file. From what I can see in the file the the file itself is fine. Something goes wrong from it reading the file and storing it in the array and when it outputs the array.
Ok I fixed this issues somehow I opened a new source code in dev and wrote a small program to read that particular file and output it from an array. It worked there so I commented out the old one copy and pasted and it worked fine. I can only guess it was a Dev error.
I can only guess it was a Dev error.
If you mean the compiler got it wrong, I think that is extremely unlikely. Much more likely you overlooked an error in your code - which happens to all of us :)
Topic archived. No new replies allowed.