数组循环结构

时间:2013-12-12 20:30:30

标签: c++ arrays loops

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

回答错误的是一个计数器 studentAnswers是一个100个问题的数组,匹配数组correctAnswers,大小为20。 QUESTIONS是值为20的常量int,与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;
    }
}

2 个答案:

答案 0 :(得分:4)

我认为快速解决方法是studentAnswers[(20*c)+x]放置studentAnswers[x]

你实际上应该在二维数组(studentAnswers[STUDENTS][QUESTIONS]中有学生答案,其中学生将是5)。

createReport2()中可能有1个,其中questNum可能会更改为questNum+1

此外,在主要for循环中声明{/ 1}} 。或者将string arr[20]更改为arr[x],将arr[c*20+x]更改为arr[i]

编辑:总结一下

arr[c*20+i]

答案 1 :(得分:0)

我认为你需要一个单独的变量用于错误答案,例如答案正确。

for (int c = 0; c < 5; c++) {
    int answeredCorrectly = 0;
    int answeredIncorrectly = 0;

    for (int x = 0; x < QUESTIONS; x++) {
        if (studentAnswers[x] == correctAnswers[x])
            answeredCorrectly++;
        else {
            arr[answeredIncorrectly] = createReport2(x, studentAnswers[x],
                    correctAnswers[x]);
            answeredIncorrectly++;
        }
    }

    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 < answeredIncorrectly; i++) {
        cout << arr[i];
    }
    cout << endl << endl;

    answeredCorrectly = 0;
}