输出一个数组,它跳过一个元素

Outputting an array and it skips one element

本文关键字:一个 元素 输出 数组      更新时间:2023-10-16

所以我说实话,我对编程很陌生,我读过其他有同样主题的板,我找不到我可能会搞砸的地方。

对于我的程序,我将一个多选测验的文本文件输入到两个不同的数组中。一个数组包含测验问题和多选答案(因此它是一个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,这对于打印出下一个问题是不正确的。