Requesting help with weird problem in code

Hi, I would like to know if I would be able to get help with a problem that I am having with my code. I am getting a 'string subscript out of range' error and I have no idea why it is doing this. I feel like everything should be fine code wise.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;

void main()
{
	ifstream students, answers;
	ofstream cout;
	answers.open("Answers.dat");
	students.open("Tests.dat");
	string key, id, responses;
	char table[20][5] = { " " };
	int numstudents = 0, numcorrect;
	answers >> key; 
	students >> id >> responses;
	cout << "Student-ID" << setw(20) << "Number Correct" << endl; 
	cout << key;
	while (!students.eof())
	{
		numstudents++;
		numcorrect = 0;
		for (int i = 0; i<20; i++)
		{
			if (key[i] == responses[i])
				numcorrect++;
			if (responses[i] == 'A')
				table[i][0] ++;
			if (responses[i] == 'B')
				table[i][1] ++;
			if (responses[i] == 'C')
				table[i][2] ++;
			if (responses[i] == 'D')
				table[i][3] ++;
			if (responses[i] == 'E')
				table[i][4] ++;
		}
		cout << id << setw(20) << numcorrect << endl;
		students >> id >> responses;
	}
	cout << "Number of students taking exam = " << numstudents << endl;
	cout << "Question" << setw(5) << "A" << setw(5) << "B" << setw(5) << "C" << setw(5) << "D" << setw(5) << "E" << endl;
	for (int i = 0; i < 20; i++)
		cout << i << setw(5) << table[i][0] << setw(5) << table[i][1] << setw(5) << table[i][2] << setw(5) << table[i][3] << setw(5) << table[i][4] << endl;
	system("pause");
}
The only thing I can see that can cause this is:
1
2
if (key[i] == responses[i])
	numcorrect++;

Check key.length( )
Last edited on
Topic archived. No new replies allowed.