因此,该程序应该为每个学生阅读和计算正确的答案,并列出所有答案的答案。我的问题是在第二张桌子上的正确答案旁边应该有一个星号。这是我出错的唯一部分。
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void main()
{
char ch;
string answers, id, responses;
int numstudents = 0, i, j, numcorrect, chart[20][5] = { 0 };
ifstream ftests, fans;
ofstream fout;
ftests.open("Tests.dat");
fans.open("answers.dat");
fout.open("table.out");
fans >> answers;
fout << setw(11) << "student-id" << setw(19) << "number correct" << endl << endl;
ftests >> id >> responses;
while (!ftests.eof())
{
numstudents++;
numcorrect = 0;
for (i = 0; i < 20; i++)
{
if (responses[i] == answers[i])
{
numcorrect++;
}
for (j = 0; j < 5; j++)
{
if (responses[i] == (char)('A' + j))
{
chart[i][j]++;
}
}
}
fout << setw(11) << id << setw(19) << numcorrect << endl;
ftests >> id >> responses;
}
fout << endl << endl << endl;
fout << "number of students taking exam = " << numstudents << endl << endl;
fout << setw(8) << "question" << setw(5) << "A" << setw(5) << "B" << setw(5) << "C" << setw(5) << "D" << setw(5) << "E" << endl;
for (i = 0; i < 20; i++)
{
fout << setw(2) << i + 1 << " " << setw(10);
for (j = 0; j < 5; j++)
{
fout << chart[i][j];
if (responses[i] == i)//I BELIEVE THE PROBLEM LIES HERE!!!
{
fout << "*" << setw(4);
}
else
{
fout << " " << setw(4);
}
}
fout << endl;
}
}
表格如下:
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
question A B C D E
1 5 1 13 3 1
2 4 7 1 7 4
3 2 3 2 2 14
4 2 1 3 13 4
5 2 8 1 4 8
6 5 1 3 3 11
7 5 7 0 4 7
8 11 4 0 3 5
9 3 2 1 2 15
10 0 1 12 3 7
11 6 2 4 10 1
12 2 2 4 9 6
13 9 4 0 6 4
14 4 2 2 2 13
15 3 3 1 1 15
16 1 8 4 3 7
17 6 3 1 7 6
18 4 1 2 5 11
19 0 1 1 10 11
20 2 0 1 1 19
表2应如下所示:
question A B C D E
1 5 1 13* 3 1
2 4 7* 1 7 4
3 2 3 2 2 14*
4 2 1 3 13* 4
5 2 8* 1 4 8
6 5 1 3 3 11*
7 5 7* 0 4 7
8 11* 4 0 3 5
9 3 2 1 2 15*
10 0 1 12* 3 7
11 6 2 4 10* 1
12 2 2 4 9* 6
13 9* 4 0 6 4
14 4 2 2 2 13*
15 3 3 1 1 15*
16 1 8* 4 3 7
17 6 3 1 7* 6
18 4 1 2 5 11*
19 0 1 1 10* 11
20 2 0 1 1 19*
我希望我足够清楚,感谢任何帮助!
答案 0 :(得分:0)
我认为该行&#34; if(respond [i] == i)&#34;会导致编译错误。这是将字符串数据类型与int进行比较。基于您之前的代码行,我认为&#34; if(respond [i] == answers [i])&#34;是一个更好的选择。我正在根据上下文解释每个变量的含义,我可能是错的。但似乎答案[]是正确答案的列表,这个比较用于计算正确答案的数量。如果我对变量的解释是错误的,那么我建议定义一个允许这种比较的变量。