Click to See Complete Forum and Search --> : Comparing characters in two arrays of strings


evapisces
March 26th, 2008, 05:49 PM
Hello. I'm having some trouble with a section of a project I'm working on. The general task of the program is to read in a list of student exam answers from a file along with an answer key, score the answers, and print some reports on the results.

Okay, so I have created the following arrays:

char ansKey[MAXLEN];
char id[MAXCT][5];
char responses[MAXCT][MAXLEN];
int indices[MAXCT];

MAXCT = 40 and MAXLEN = 25

I'm am working on the first report. I have to grade the answers against the answer key. So I'm pretty sure I have to do a string compare where I compare each element of the answer key to the repective element in each student's responses. However, I'm not sure how to do this. Here's the function where I fill the arrays and compare the answer key and student responses:


int fillArrays(char key[], char iden[][5], char answers[][MAXLEN], int index[], int ct)
{
ifstream inFile;
inFile.open("P7Grades.txt");

if (!inFile)
{
cout << "Cannot find input file.\n\n";
system("PAUSE");
exit(1);
}

inFile >> key;
int i = -1;

while (!inFile.eof() )
{
i++;
inFile >> iden[i] >> answers[i];
index[i] = i;
}

for (int j = 0; i < ct; j++)
{
for (int k = 0; k < 20; k++)
{
if (strcmp(key[k], answers[j][k]) != 0)
cout << "*" << answers[j][k] << " ";
}
}

return i;
inFile.close();
}



When I run the code, I get this error message:


invalid conversion from 'char' to 'const char*'
(initializing argument 1 of 'int strcmp(const char*, const char*))
and
invalid conversion from 'char' to 'const char*'
(initializing argument 2 of 'int strcmp(const char*, const char*))



I'm not sure what I'm doing wrong. Any help is greatly appreciated. If you need additional info, please let me know.

I'm fairly new to C++, so if you guys could explain things as simple as possible, I would really appreciate it.
Thanks in advance :)

stephendoyle75
March 26th, 2008, 06:58 PM
The clue is in the error message. Strcmp compares 2 strings (or more specifically, char* strings). You are passing a single character as both parameters and this is giving you the compiler error.

What format are the keys in? Can you give an example?

evapisces
March 26th, 2008, 07:07 PM
Sure. Here's the key and the first few student responses from the file:


bcddacbaaebdbecceacb
H926 bcedaccbaexdbedceabb
H080 acddacbaxebdbecaeacb
H233 bcddacbaaebdbecceacb

The first line is the answer key and the other lines are the student's ID number followed by their responses.

stephendoyle75
March 26th, 2008, 08:48 PM
Ok, so you really do want to compare the answer against the key one character at a time. You don't need strcmp() for this, instead use a simple equality comparison, e.g.:

for (int k = 0; k < 20; k++)
{
if (key[k] !=answers[j][k])
cout << "*" << answers[j][k] << " ";
}