I have been writing a program using arrays and strings.Here is my question.
A class of students takes a 20 question multiple-choice exam;each question has 5 choices(a,b,c,d,e)only one of them are correct.In "tests.dat"Each record of which consists of a student id, followed by a blank, followed by students 20 responses. In "answers.dat" which consists of a single record, namely the correct answers to the exam.Then It needs to produce a grade summary report like this.If the answer is correct it should put a "*" by the correct answer


student-id number correct
1231231212312 12
1233424325435 25
.... ..


question A B C D E
1 5 1 13* 3 1
2 4 7* 5 12 7
.
.
...

this is what i got so far.Thank you

#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;
int question[20][5];
int main()
{
	ifstream fanswers, fcorrect_keys;
	
	char correct_keys[21];
	char student_id[100][12], answers[100][21];
	char id_buf[12], answers_buf[21];
	
	int student_count = 0;
	
	fcorrect_keys.open("Answers.dat");
	while(!fcorrect_keys.eof())  {		
		fcorrect_keys >> answers_buf;
		strcpy(correct_keys, answers_buf);
	}
//	cout << correct_keys << endl;
	fcorrect_keys.close();
	fanswers.open("Tests.dat");
	while(!fanswers.eof())  {		
		fanswers >> id_buf >> answers_buf;
		strcpy(student_id[student_count], id_buf);
		strcpy(answers[student_count],answers_buf);
		student_count++;  		
   	}
//	for(int i=0; i < student_count;i++)
//		cout << student_id[i] << "  " << answers[i] << endl;
 	 fanswers.close();// close the file  
	cout << " student_id      number_correct" << endl << endl;
	for(int i = 0; i < student_count; i++)  {
		int correct_count = 0;
		for(int j = 0; j < 20; j++)  { 
			if (answers [i][j] == correct_keys[j])
				correct_count++;
			question[j][answers[i][j] - 'A']++;
		}
		printf("%11.11s         %2d\n", student_id[i],correct_count);
	}	
	cout << endl << endl << endl << "Number of students taking exam : " << student_count << endl; 
	
	printf("\nquestion     A    B    C    D    E\n");
	
	for(int k = 0; k < 20 ; k++)  {
		printf("   %2d      ",1+k);
		for(int l=0;l < 5; l++)
			printf(" %2d%c ", question[k][l], (l == (correct_keys[k] - 'A') ? '*' : ' '));
		printf("\n");
	}
	return 0;
}

So what exactly is your question?

I am doing

question[j][answers[i][j] - 'A']++

this part wrong.But I dont know how to change it.Also..Question is a 20x5 array of int and answers is a 100x21 of char.I dont know how to change it to char without messing up the code

This article has been dead for over six months. Start a new discussion instead.