CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2008
    Location
    USA
    Posts
    12

    Comparing characters in two arrays of strings

    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:
    Code:
    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:

    Code:
    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:

    Code:
    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

  2. #2
    Join Date
    Apr 2007
    Location
    Ireland
    Posts
    81

    Re: Comparing characters in two arrays of strings

    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?

  3. #3
    Join Date
    Mar 2008
    Location
    USA
    Posts
    12

    Re: Comparing characters in two arrays of strings

    Sure. Here's the key and the first few student responses from the file:

    Code:
    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.

  4. #4
    Join Date
    Apr 2007
    Location
    Ireland
    Posts
    81

    Re: Comparing characters in two arrays of strings

    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.:
    Code:
    for (int k = 0; k < 20; k++)
    {
        if (key[k] !=answers[j][k])
            cout << "*" << answers[j][k] << "   ";
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured