comparing arrays through class

I'm comparing my arrays, but when I compile it, the class member array does not show the values I copied into the array from my original array in main,
but the array from main keeps the values, and I wrote a test loop after the program runs, just to see what's inside of the class member array, and it's the actual variables that I copied into the array from main. I'm trying to compare the two arrays in the class function "grade", but I do not know what I'm doing wrong.Every time I compare them through a for loop, with an if statement, it does not work, and random characters appear.

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

#include<iostream>
#include<string>


using namespace std;


 class TestGrade
 {
 public: void setKey(char []);
		 void grade(char []);
		 char canswers[20];
		 void display();
 }; 



	void TestGrade::setKey(char answers[])
	{
		for(int index = 0; index < 19; index++)
		{
                canswers[index] = answers[index]; 
		}
	}

  
      void TestGrade::grade(char answer[])
    {
         for(int index = 0; index < 19; index++)
         {
              cout << canswers[index] << " " << answer[index] << endl; 
                  
         }
    }
    
      
       void TestGrade::display()
    {
       {for (int index = 0; index < 19;index++)
       {
            cout << canswers[index] << endl;}
       }
    }


      
int main()
{
	const char SIZE = 20;
	char answer[SIZE];
	char key[20] = {'B', 'D', 'A', 'A',
                                        'C', 'A', 'B', 'A',
	                          'C', 'D', 'B', 'C',
	                          'D', 'A', 'D', 'C',
	                          'C', 'B', 'D', 'A'};

     TestGrade test1,test2;
      
     test1.setKey(key);
     

	cout << "Welcome to the written portion of the DMV exam. \n";
	cout << "You may only enter capital A, B, C, or D for your answers.\n\n" << endl;



	for (int index = 0; index < SIZE; index++)

	{
		cout << "Enter your answer for question " << index+1 << endl;
		cin >> answer[index];


		while (answer[index] != 'A'
			&& answer[index] != 'B'
			&& answer[index] != 'C'
			&& answer[index] != 'D')
	{
		cout << "ERROR: you must input capital A,B,C, or D" << endl;
		cin >> answer[index];
	}

	}

     test2.grade(answer);
     test1.display();
system("pause");
return 0;

}

Last edited on
originally copied wrong code sorry
Get rid of test2 on line 58.
Change line 86 to test1.grade(answer);
Last edited on
Topic archived. No new replies allowed.