输出一个数组,它跳过一个元素
Outputting an array and it skips one element
所以我说实话,我对编程很陌生,我读过其他有同样主题的板,我找不到我可能会搞砸的地方。
对于我的程序,我将一个多选测验的文本文件输入到两个不同的数组中。一个数组包含测验问题和多选答案(因此它是一个2D数组),而另一个数组则包含答案。然后将这些输出给用户,用户可以进行测验,然后在最后获得测验的结果。
因此,我可能已经完成了最基本的调试,即逐步完成我的程序,并确保我的文件正确加载到数组中(它确实做到了),所以我不认为将我的文件加载到数组是个问题,而是它们是如何输出的。
那我的问题是什么?
当我开始输出我的测试,用户开始接受时,第一个问题很好(以及多选答案),答案是正确的,但当它转到第二个问题时,它跳过并输出第三个问题,但仍然输出问题2的多选答案,这使整个测试失败。因此,本质上它输出的是数组[2][0],然后是第1行中的其余元素,而不是数组[1][0],然后是第一行中的剩余元素。所以所有的问题都一一解决了。
有人知道为什么它只跳过数组中的一个元素,而读取其他所有元素都很好吗?提前感谢您的帮助!
下面是文本文件的外观示例。
集成开发环境通常包括:
文本编辑器
编译器
调试器
以上所有
以上都不是
D
文件中总共有12个问题,行与行之间没有多余的空格。
下面是我的代码的循环,所有内容都已声明并初始化。
int countRow = 0; //Counter for the rows in the array
int countCol = 0; //Counter for the columns in the array
const int ARRAY_ROW = 11; //Rows in the array
const int ARRAY_COL = 6; //Columns in the array
string line; //String variable to put into the array
//Filling the 2D array and the answer array
for (countRow = 0; countRow < ARRAY_ROW; countRow++)
{
for (countCol = 0; countCol < (ARRAY_COL); countCol++)
{
getline(inFile, line);
testArr[countRow][countCol] = line;
}
//Filling the answer array
if (countCol == 6)
{
getline(inFile, line);
testAnswers[countRow] = line;
}
}
string choice; //Variable for user input
string answer; //Variable for the correct answer
int rightAnswer = 0; //Accumulator for the right answer
int row = 0; //Counter for rows
int col = 0; //Counter for columns
int questionNum = 1; //Number of the question
char option = 'A'; //Lettering options for the user to choose from
cout << "Select the appropriate option for your desired answer." << endl;
cout << endl;
cout << endl;
for (row = 0; row < ARRAY_ROW; row++)
{
cout << "Question " << questionNum << ": " << testArr[row][col] << endl;
cout << endl;
questionNum++;
for (col = 1, option = 'A'; col < ARRAY_COL, option < 'F'; col++, option++)
{
cout << option << " - " << testArr[row][col] << endl;
}
cout << endl;
answer = testAnswers[row];
cin >> choice;
cout << endl;
if (choice == answer)
{
cout << "Correct, you chose the right answer: " << answer << endl;
cout << endl;
cout << endl;
rightAnswer++;
}
else
{
cout << "Incorrect, the correct answer is: ";
cout << answer << endl;
cout << endl;
cout << endl;
}
}
由于您的问题存储在'列'0中,
更改:
cout << "Question " << questionNum << ": " << testArr[row][col] << endl;
收件人:
cout << "Question " << questionNum << ": " << testArr[row][0] << endl;
在打印出问题的循环的第一次迭代之后,变量col
的值为ARRAY_COL
,这对于打印出下一个问题是不正确的。
相关文章:
- lower_bound()返回最后一个元素
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 使用运算符 [] 引用 std::vector 上最后一个元素时出现问题<>
- Lower_bound不适用于具有 3 个元素的向量的最后一个元素
- 检查 2D 网格的某个元素是否与另一个元素共享对角线、水平线或垂直线
- 仅显示链表的最后一个元素
- push_back通过自行创建的对象获取最后一个元素的向量
- 使用 map.end() 访问 map 的最后一个元素
- 如何知道地图中的最后一个元素是否被删除?
- 选择一个元素而不是一个对象的数组的原因
- std::矢量保存 只推送最后一个元素
- 为什么这个选择排序算法仍然切换一个元素,当它已经是其他元素中最小的元素时?
- 反转后不返回最后一个元素
- 如何在新数组较小时创建新数组并将旧数组的最后一个元素复制到新数组中?
- 访问 for_each 函数中的前一个元素
- 在 QListView 中显示最后一个元素
- 从 std::vector 中选择一个元素,而不是给定元素
- C++:在进行切片时对迭代器的约定,特别是对于访问最后一个元素并最终将其删除
- 遍历列表包含排除最后一个元素的内容
- 如何从一个无序集合中获取一个元素