gpt4 book ai didi

c++ - 数组循环结构

转载 作者:行者123 更新时间:2023-11-30 01:55:39 25 4
gpt4 key购买 nike

我一直在尝试循环匹配两个数组中的一系列答案。代码每次都运行循环并输出前 20 个问题,但不会继续进行下一组检查这些答案。

answeredCorrectly 是一个计数器studentAnswers 是一个包含 100 个问题的数组,与大小为 20 的数组 correctAnswers 相匹配。QUESTIONS 是常量 int,值为 20 并与 correctAnswers 配对。

我尝试放弃 for 循环结构,只使用 while 循环来检查 studentAnswers 中的位置,但这导致了崩溃。

有什么想法可以继续为其他每个学生计算而不是坚持前 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

此外,在主 for 循环中声明 string arr[20] 。或将 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;
}

关于c++ - 数组循环结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20553611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com