数组循环结构
Array Looping Structure
我一直在尝试循环两个数组中匹配的一系列答案。代码每次都会运行整个循环,并输出前20个问题,但不会继续到下一组来检查这些答案。
answeredCorrect是计数器studentAnswers是一个由100个问题组成的数组,与数组correct匹配。答案大小为20。QUESTIONS是一个常数int,值为20,并与correctAnswers配对。
我试着放弃for循环结构,只使用while循环来检查studentAnwers中的位置,但这导致了崩溃。
有什么想法可以继续为其他每个学生计算,而不是坚持前20个答案吗?
这是我正在处理的代码部分。
编辑添加了函数createReport2()
编辑2添加了更新代码
string createReport2 (int questNum, char stuResponse, char correctResponse)
{
stringstream ss; // the string stream object that will be used for the conversions
// Add the various elements to the stringstream object.
ss << questNum << "(" << stuResponse << "/" << correctResponse << ")";
// Return the final result as a C++ string class.
return ss.str();
} // end function string createReport (int questNum, char stuResponse, char correctResponse)
int main()
{
const int QUESTIONS = 20;
const int STUDENT_QUESTIONS = 100;
ifstream inputFile;
inputFile.open("CorrectAnswers.txt");
char correctAnswers[QUESTIONS];
for (int i=0; i<20; i++)
{
inputFile >> correctAnswers[i];
}
ifstream inputFile2;
inputFile2.open("StudentAnswers.txt");
char studentAnswers[STUDENT_QUESTIONS];
for (int t=0; t<STUDENT_QUESTIONS; t++)
{
inputFile2 >> studentAnswers[t];
}
int answeredCorrectly = 0;
string name;
for(int c = 0; c < 5; c++)
{
string arr[100];
for (int x=0; x < QUESTIONS; x++)
{
if (studentAnswers[(5*c)+x] == correctAnswers[x])
answeredCorrectly++;
else
arr[c*5+x] = createReport2(x,studentAnswers[c*5+x],correctAnswers[x]);
}
cout << "Report for Student " << c+1 << ":" << endl;
cout << "---------------------" << endl;
cout << "Missed " << 20 - answeredCorrectly << " out of 20 questions for " << (answeredCorrectly / 20.0) * 100 << "% correct." << endl;
cout << "Answered Correctly: " << answeredCorrectly << endl;
cout << "Questions missed:";
for (int i = 0; i < 20; i++)
{
cout << arr[c*5+i];
}
cout << endl << endl;
answeredCorrectly = 0;
}
}
我认为一个快速的解决方案是将studentAnswers[(20*c)+x]
放在任何有studentAnswers[x]
的地方。
实际上,你应该把学生的答案放在二维数组中(studentAnswers[STUDENTS][QUESTIONS]
,其中STUDENTS应该是5)。
在createReport2()
中可能存在一个of-by-1,其中questNum可能更改为questNum+1
。
此外,在的main for循环中声明string arr[20]
。OR将arr[x]
更改为arr[c*20+x]
以及将arr[i]
更改为arr[c*20+i]
。
编辑:所以总结
for(int c = 0; c < 5; c++)
{
for (int x=0; x < QUESTIONS; x++)
{
if (studentAnswers[c*20+x] == correctAnswers[x])
answeredCorrectly++;
else
arr[c*20+x] = createReport2(x,studentAnswers[c*20+x],correctAnswers[x]);
}
cout << "Report for Student " << c+1 << ":" << endl;
cout << "---------------------" << endl;
cout << "Missed " << 20 - answeredCorrectly
<< " out of 20 questions for " << (answeredCorrectly / 20.0) * 100
<< "% correct." << endl;
cout << "Answered Correctly: " << answeredCorrectly << endl;
cout << "Questions missed:";
for (int i = 0; i < 20; i++)
{
cout << arr[c*20+i];
}
cout << endl << endl;
answeredCorrectly = 0;
}
我认为你需要一个单独的变量来处理不正确的答案,比如answeredIncorrectly。
for (int c = 0; c < 5; c++) {
int answeredCorrectly = 0;
int answeredIncorrectly = 0;
for (int x = 0; x < QUESTIONS; x++) {
if (studentAnswers[x] == correctAnswers[x])
answeredCorrectly++;
else {
arr[answeredIncorrectly] = createReport2(x, studentAnswers[x],
correctAnswers[x]);
answeredIncorrectly++;
}
}
cout << "Report for Student " << c + 1 << ":" << endl;
cout << "---------------------" << endl;
cout << "Missed " << 20 - answeredCorrectly
<< " out of 20 questions for "
<< (answeredCorrectly / 20.0) * 100 << "% correct." << endl;
cout << "Answered Correctly: " << answeredCorrectly << endl;
cout << "Questions missed:";
for (int i = 0; i < answeredIncorrectly; i++) {
cout << arr[i];
}
cout << endl << endl;
answeredCorrectly = 0;
}
相关文章:
- 如何循环打印顶点结构
- 在基于范围的for循环中使用结构化绑定声明
- 在C++中循环访问自定义结构列表的小问题
- C++ - 使用用户输入的字符串数据检查结构字符串数据(无限执行 while 循环)
- 输入 ctrl + x 后如何再次 cin (cin<<x) 循环(cin 一个结构)
- 类方法 - 数据结构中 For 循环的运行时错误
- 用于循环访问多个集合的数据结构
- 将结构数据存储在循环缓冲区中
- 为什么基于范围的 for 循环中的结构化绑定只是一个副本而不是引用?
- for 循环说 - 错误 成员引用基类型"int [13]"不是结构或联合
- 循环依赖结构,使用前向声明时结构的错误重定义
- 如何在 for 循环中声明和使用结构数组?
- 在C++中循环数组时访问结构属性
- 在 for 循环中声明结构是否合法C++?
- C++ 数据结构队列:使用 for 循环查找队列中最大的元素
- 简化C++中的嵌套循环结构
- CPP 在读取结构数据时无限循环错误?
- 如何在C++循环中创建结构成员
- 使用 for 循环将值分配给结构的成员
- 具有模板专用化的复合结构中的循环依赖关系