c++ - C ++循环遍历数组以显示内容
问题描述
我正在编写一个循环来遍历 3d 数组并显示 [0][0][0] 到 [0][2][4] 的内容。它从 [0][0][0] 到 [0][0][4] 显示并正常运行,但之后它跳转到 [0][2][0] 而不是 [0][1] [0]。如果有人能纠正我的错误,我无法弄清楚为什么是我自己。
a、b、c、d 和 e 都是整数变量。在这个循环的开始,我将 b 初始化为 0,c 初始化为 1,d 初始化为 0,e 初始化为 1。
for (int a = 0; a < students;)
{
cout << "Let's help Student #" << e << "!" << endl;
do
{
cout << "Question #" << c << ":" << endl;
cout << math_rooms[0][a][b] << endl;
cin >> math_rooms[0][a][b];
cout << " " << endl;
if (math_rooms[0][a][b] != math_answers[0][a][b])
{
d++;
//Set up the error variable so we keep track of pass/fail
cout << "Sorry, that was incorrect." << endl;
cin >> math_rooms[0][a][b];
cout << " " << endl;
}
else
{
cout << "That's correct!" << endl;
cout << " " << endl;
}
//Increment b and c to go through all questions/keep track of question #.
b++, c++;
//Set up loop that checks pass_fail on final question.
if (d >= 3)
{
cout << "Sorry, you failed the test." << endl;
pass_fail = false;
return pass_fail;
}
} while (b < questions);
//restart counter
c = 1;
//Increment a so that it changes to the next student & f so that it keeps track of the student #.
a++, e++;
}
所以本质上,循环工作正常,直到它增加 a 变量。它从 0 开始,然后当它应该增加到 1 时,它改为 2。IDK 为什么?
解决方案
每次执行for循环都需要重置b的值
推荐阅读
- c++ - 定点幂函数
- message - 服务总线的消息合约是如何版本化的?
- python - 如何在 Keras 或 Tensorflow 中将 L1 范数添加到损失函数中?
- javascript - 附加带有精灵图像的文本并使文本能够与精灵图像进行补间
- php - 从 HTML 表单使用 PHP 编辑 SQL 表
- indexing - Julia MetaCollection:实现集合的集合
- javascript - 使页脚停留在页面底部而不被剪切
- arrays - 如何在groovy中迭代数组参数
- angular - Angular AOT forRoot 有条件地定义提供者
- android - 如何在颤振中创建 SharedPreferences 的 Singleton 类