array problem

I cannot understand why my program only spits out garbage. Here are the letters if you wish to plug them in with a .txt file:
correctanswers.txt

A
B
C
D
A
A
B
C
D
D
C
B
B
A
B
D
A
A
C
C

studentanswers.txt
A
D
C
D
B
A
C
C
D
A
C
C
B
A
A
D
A
B
C
C

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
121
122
123
124
125
126
127
  #include <iostream>
#include <fstream>

using namespace std;

void getRscore(char[], int);
void getBscore(char[], int);
char compare(char[], char[], int);

const int SIZE=20;

int main ()
{
    int amountwrong;
    char studentanswers[SIZE];
    char correctanswers[SIZE];
    char wrong[SIZE];
    int countdex=0;
    
    
    getRscore(studentanswers, SIZE);
    
    getBscore(correctanswers, SIZE);
    
    


     
     
     
     for (int index = 0; index<SIZE; index++)
         {
         while (studentanswers[index]!=correctanswers[index])

              {
              wrong[countdex]=studentanswers[index];
              
              countdex++;}}



              
     
              cout<<"The student answers have been pulled from their file and compared to \n";
              cout<<"the correct answers. The amount of answers that were wrong is: " <<countdex <<endl;
              cout<<"The incorrect answers are as follows: ";
              for (int count=0; count<countdex; count++)
          cout<<wrong[count];             
              
 
              
                                            
    
 
 
 
 
 
 
 
 
 
 
 
 
 
     system ("pause");
    return 0;
}

void getRscore(char score[], int SIZE)

{
     ifstream Answers;
     
     Answers.open("StudentAnswers.txt");
    char answers[20];
    int count=0;
    while (count<20 && Answers)
     Answers>>score[count];
     count++;
     
     Answers.close();
 for(int index=0; index<20; index++)
  cout<<answers[index] <<" ";
}



void getBscore(char Score[], int SIZE)

{
     ifstream answers;
     
     answers.open("CorrectAnswers.txt");
     
     int count=0;
     while (count<SIZE && answers >> Score[count])
     count++;
     
     answers.close();
     
}






















            
+
You still haven't made the changes I mentioned the first time you posted.
http://cplusplus.com/forum/beginner/99538/

You are not checking that the file opens properly
you still have a function prototype for a compare function you are not using
You don't need an answers array.
The loop on line 84-85 is not needed and will output garbage because you never put anything into the array.
Your loop on line 79 is wrong if you are going to use a counting loop consider using a for loop
and whats with the + on line 127? this is a syntax error that should have been fixed before posting
Last edited on
I have solved my problem opening the file, and I think I have most everything corrected that you said to. When I run it now, it says that 50 questions were answered incorrectly. There are only supposed to be 20 answers. I think the problem is in the comparison of the two arrays.



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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <fstream>

using namespace std;

void getRscore(char[], int);
void getBscore(char[], int);


const int SIZE=20;

int main ()
{
    int amountwrong;
    char studentanswers[SIZE];
    char correctanswers[SIZE];
    char wrong[SIZE];
    int countdex=0;
    
    
    getRscore(studentanswers, SIZE);
    
    getBscore(correctanswers, SIZE);
    
    


     
     
     
     for (int index = 0; index<SIZE; index++)
         {
         while (studentanswers[index]!=correctanswers[index])

              {
              wrong[countdex]=studentanswers[index];
              
              countdex++;}}



              
     
              cout<<"The student answers have been pulled from their file and compared to \n";
              cout<<"the correct answers. The amount of answers that were wrong is: " <<countdex <<endl;
              cout<<"The incorrect answers are as follows: ";
              for (int count=0; count<countdex; count++)
          cout<<wrong[count];             
              
 
              
                                            
    
 
 
 
 
 
 
 
 
 
 
 
 
 
     system ("pause");
    return 0;
}

void getRscore(char score[], int SIZE)

{
     ifstream Answers;
     
    
     Answers.open("StudentsAnswers.txt");
    
    
    if (Answers)
   { 
    
    
    
    while (Answers)
     {for (int count=0; count<20; count++)
     Answers>>score[count];}
    
     
     Answers.close();
                           }


    else 
         {cout<<"The file 'StudentAnswers.txt.' did not open correctly."; }








}






void getBscore(char Score[], int SIZE)

{
     ifstream answers;
     
     answers.open("CorrectAnswers.txt");
     
     if (answers)
        {
     
     int count=0;
     
     while (count<SIZE && answers >> Score[count])
     count++;
     
     answers.close();}
     
     else
     {"The file 'CorrectAnswers' did not open correctly";}



     
}






















            
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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

void getRscore(char[]);
void getBscore(char[]);

const int SIZE = 20;

int main ()
{
	
	char studentanswers[SIZE];
	char correctanswers[SIZE];
	char wrong[SIZE];
	int numberWrong = 0;


	getRscore(studentanswers);

	getBscore(correctanswers);

	for (int index = 0; index < SIZE; index++)
	{
		if (studentanswers[index] != correctanswers[index])
		{
			wrong[numberWrong] = studentanswers[index];
			numberWrong++;
		}
	}
	
	cout<<"The student answers have been pulled from their file and compared to \n";
	cout<<"the correct answers. The amount of answers that were wrong is: " <<numberWrong<<endl;
	cout<<"The incorrect answers are as follows: ";
	
	for(int i = 0; i < numberWrong; i++)
	{
		cout  << wrong[i] << " ";
	}

	cout << endl;

	system ("pause");
	return 0;
}

void getRscore(char score[])

{
	ifstream inFile;
	int count = 0;

	inFile.open("StudentAnswers.txt");
	if (inFile.fail())
	{
		cerr << "File failed to open " << endl;
		cin.ignore();
		exit(0);
	}

	inFile >> score[count];
	
	while(!inFile.eof())
	{
		count++;
		inFile >> score[count];	
	}

	inFile.close();
}



void getBscore(char score[])

{
	ifstream inFile;
	int count = 0;

	inFile.open("CorrectAnswers.txt");
	
	if (inFile.fail())
	{
		cerr << "File failed to open " << endl;
		cin.ignore();
		exit(0);
	}

	inFile >> score[count];

	while(!inFile.eof())
	{
		count++;
		inFile >> score[count];
	}

	inFile.close();

}
If you have time, can you please explain what I've done wrong so that I'll know in the future what not to do?
Topic archived. No new replies allowed.