输出一个数组并跳过一个元素
Outputting an array and it skips one element
老实说,我是编程的新手,我读过其他有相同主题的论坛,但我找不到我可能搞砸的地方。
对于我的程序,我将多项选择测验的文本文件输入到两个不同的数组中。一个数组包含测验问题和多项选择答案(因此它是一个二维数组),而另一个数组包含答案。然后将这些输出给用户,用户可以参加测验,最后得到他们的测验结果。
所以我可能已经完成了最基本的调试,包括单步调试我的程序并确保我的文件正确加载到我的数组中(确实如此)所以我认为将我的文件加载到我的数组中不是问题数组,但它们是如何输出的。
那么我的问题是什么?
当我开始输出我的测验并且用户开始做它时,第一个问题很好(以及多项选择答案)并且答案是正确的,但是当它进入第二个时question 它跳过并输出第三个问题,但仍然输出问题 2 的多项选择答案,这会导致整个测试失败。所以本质上它是输出 array[2][0] 然后是第 1 行中的其余元素,而不是 array[1][0] 然后是第 1 行中的其余元素。所以所有的问题都关闭了一.
有没有人知道为什么它只跳过数组的一个元素但读出其他所有元素都很好?预先感谢您的帮助!
下面是文本文件的示例。
An Integrated Development Environment typically consists of:
A text editor
A compiler
A debugger
All of the above
None of the above
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;
}
}
由于您的问题存储在 'column' 0 中,
变化:
cout << "Question " << questionNum << ": " << testArr[row][col] << endl;
收件人:
cout << "Question " << questionNum << ": " << testArr[row][0] << endl;
在循环打印出问题的第一次迭代后,变量 col
保留值 ARRAY_COL
,这对于打印出下一个问题是不正确的。
老实说,我是编程的新手,我读过其他有相同主题的论坛,但我找不到我可能搞砸的地方。
对于我的程序,我将多项选择测验的文本文件输入到两个不同的数组中。一个数组包含测验问题和多项选择答案(因此它是一个二维数组),而另一个数组包含答案。然后将这些输出给用户,用户可以参加测验,最后得到他们的测验结果。
所以我可能已经完成了最基本的调试,包括单步调试我的程序并确保我的文件正确加载到我的数组中(确实如此)所以我认为将我的文件加载到我的数组中不是问题数组,但它们是如何输出的。
那么我的问题是什么?
当我开始输出我的测验并且用户开始做它时,第一个问题很好(以及多项选择答案)并且答案是正确的,但是当它进入第二个时question 它跳过并输出第三个问题,但仍然输出问题 2 的多项选择答案,这会导致整个测试失败。所以本质上它是输出 array[2][0] 然后是第 1 行中的其余元素,而不是 array[1][0] 然后是第 1 行中的其余元素。所以所有的问题都关闭了一.
有没有人知道为什么它只跳过数组的一个元素但读出其他所有元素都很好?预先感谢您的帮助!
下面是文本文件的示例。
An Integrated Development Environment typically consists of:
A text editor
A compiler
A debugger
All of the above
None of the above
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;
}
}
由于您的问题存储在 'column' 0 中,
变化:
cout << "Question " << questionNum << ": " << testArr[row][col] << endl;
收件人:
cout << "Question " << questionNum << ": " << testArr[row][0] << endl;
在循环打印出问题的第一次迭代后,变量 col
保留值 ARRAY_COL
,这对于打印出下一个问题是不正确的。