Char arrays

Assignment is to loop through two data files and compare the answers to find the number of correct answers.Then produce a report that shows what responses were given to the 20 questions.This second part includes a 2-D array of counters.What I have that works for the first section is as follows:
#include<fstream>
#include<iostream>
#include<iomanip>
using namespace std;
void main()
{
ifstream finans,fintests;
char key[21],idno[23],responses[21];
int i=0,j=0,count=0, numcorrect=0,counter[21][6]={0};

finans.open("answers.dat");
fintests.open("tests.dat");
finans>>key;
fintests>>idno>>responses;
cout<<setw(20)<<"student id"<<setw(40)<<"number correct"<<endl;
cout<<endl;
while(!fintests.eof())
{
count++;
for(i=0;i<20;i++)
{
if (responses[i]==key[i]&&responses!="")
numcorrect++;
counter[i][j]++;



}
cout<<setw(20)<<idno<<setw(40)<<numcorrect<<endl;
cout<<""<<endl;

numcorrect=0;
fintests>>idno>>responses;

}
cout<<"number of students taking exam="<<count<<endl;
for(i=0;i<20;i++)
for(j=0;j<5;j++)
counter[i][j]++;
cout<<counter[i][j]<<"";

cout<<" "<<endl;
//fintests>>idno>>responses;

}


This produces the following output:

student id number correct

430-52-6192 13

112-81-5225 19

120-49-5322 12

456-65-3211 2

990-45-0978 4

324-98-5445 6

112-34-3443 12

435-56-8790 18

452-23-5675 6

776-45-5454 7

567-71-9909 6

345-54-7834 9

555-12-2341 18

568-09-0096 20

664-61-0987 10

612-45-7687 5

567-23-1125 3

561-43-6781 6

498-12-4321 3

479-01-4867 4

489-90-0999 17

456-09-1111 19

561-66-6657 3

number of students taking exam=23
0
Press any key to continue . . .

The second part should look like this:

Question A B C D E

1 5 1 13 3 1
2 4 7 1 7 4
3 etc..........

I am not making any headway on the second part.Would be happy to post my numerous attempts if anyone is interested.Any help is appreciated
You could try first casting the characters as int's and then add one to the counter set per question i and per responses[i]:
1
2
3
4
5
6
7
8
9
10
11
12
for(i=0;i<20;i++)
{ 
	if (responses[i]==key[i]&&responses!="")
	{
		numcorrect++;
	}
	//cast as int
int respons[21];
	respons[i]=(int)responses[i]-'A';
	//counter gets larger for every respons[i] to question i
		counter[i][respons[i]]++;
}

then to output do something like :
1
2
3
4
5
6
cout << setw(3) << i+1 <<  " ";
	for(j=0;j<5;j++)
	{
		
		cout<< setw(3) << counter[i][j] <<" ";
	}
Last edited on
Topic archived. No new replies allowed.