Help with program. Combo of arrays and classes

I am attempting to write a program which will take and array declared and initialized in the main and set to an array from a class via a constructor.

Here is the array from the main
//Variable declaration
char key[NUM_QUESTIONS] = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};//correct answers to test

This is array from the class
class TestGrader
{
private:
char answers[NUM_QUESTIONS];//20 characters which holds the correct answers to the test

Here is the prototype of the function
void setKey(char [NUM_QUESTIONS]);//prototype of setKey function

Here is the constructor
void TestGrader::setKey(char key[NUM_QUESTIONS])
{
for ( int i = 0; i < NUM_QUESTIONS; i++)
{
answers[i] = key[i];
}//End for ( int i = 0; i < NUM_QUESTIONS; i++)


}//End setKey function definition
I have done some modification so I could print out the answers array to see what it is and what prints is a line of strange symbols. I'm not sure what's wrong I can't find any errors in it. If further info is needed please let me know. Any help would very appreciated.
Hi

Please provide more code and please would you put your code in code syntax next time so that it look like code, and better to see and read

like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
char key[NUM_QUESTIONS] = {'B','D','A','A','C','A','B','A','C','D','B','C','D','A','D','C','C','B','D','A'};//correct answers to test 

This is array from the class 
class TestGrader
{
private: 
char answers[NUM_QUESTIONS];//20 characters which holds the correct answers to the test 

Here is the prototype of the function 
void setKey(char [NUM_QUESTIONS]);//prototype of setKey function 

Here is the constructor 
void TestGrader::setKey(char key[NUM_QUESTIONS])
{
for ( int i = 0; i < NUM_QUESTIONS; i++)
{
answers[i] = key[i]; 
}//End for ( int i = 0; i < NUM_QUESTIONS; i++)


}
Last edited on
Use strings instead of char. Should be available if you include <iostream>
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
std::string key = "BDAACABACDBCDADCCBDA";

class TestGrader {
private:
std::string answers;
public:
//heres your constuctor
TestGrader(std::string setToKey): answers(setToKey) {}
//heres if you want to change the key after creation
void setKey(std::string setToKey) {answers = setToKey;}
}

Now you can create the class with the key already initialized:
 
TestGrader test = TestGrader("ABACADABA");

or your existing key
 
TestGrader test = TestGrader(key);
Last edited on
Topic archived. No new replies allowed.