Character array Comparison

I am trying to create a program that will take an answer key to a test from a file, put it into a character array, and then take two more two dimensional arrays from files which contains the students IDs and their test answers. It then compares their answers to the key using another function. I have the first two functions done, but I can't get the element comparison to work. Here's what I have for the compare function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void computeResults (char test[][10], char key[])
{
     int correct[30];
     int end, i = 0;
     while (i < 30)
     {
           int j = 0;
           while (j < 10)
           {
                 if (test[i][j] == key[i])
                    correct[i] +=1;
           }
           cout<< correct[i];
           cin>> end;
     }
     
}

It also needs to display the ID Answers, and Number correct in the main function. I am grateful for any help regarding this.
Last edited on
I see something already:

You never increment i or j:

Do something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void computeResults (char test[][10], char key[])
{
     int correct[30];
     int end, i = 0;
     while (i < 30)
     {
           int j = 0;
           while (j < 10)
           {
                 if (test[i][j] == key[i])
                    correct[i] +=1;
                 j++;
           }
           cout<< correct[i];
           cin>> end;
           i++;
     }
     
}


Or better yet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void computeResults (char test[][10], char key[])
{
     int correct[30];
     int end;
     for(int i = 0; i < 30; i++)
     {
           for(int j=0;j < 10;j++)
           {
                 if (test[i][j] == key[i])
                    correct[i] +=1;
           }
           cout<< correct[i];
           cin>> end;
     }
     
}


Your correct array doesn't really need to be an array if you are just trying to keep track of the number of correct answers. Take out the [i] from it.

The cout and cin should probably go outside of that last while or for loop so everything runs automatically. Then output whatever you want at the end.

As per your student ID and stuff, just have a few more inputs to the functions and then COUT those parameters.

Done.
Last edited on
Topic archived. No new replies allowed.