0

さまざまな人が回答する一連の質問を比較するためのコードがいくつかあります。1 つのセットはユーザーが入力し、他のセットは .txt ファイルから取得されます。実行すると、範囲外のアサーション エラー ベクトル添え字が表示され、それがどこから来ているのかわかりません。コードは次のとおりです。

 void compare()
{
    const int qnum = 11;
    string filename, temp;
    string search[10] = {"1","2","3","4","5","6","7","8","9","0"};
    int answers[qnum], i, j, k, q= 0, numtemp = 0, pplNum;
    ifstream infile;
    vector <string> people;
    vector <int> pplans;
    vector <int> pplscore;

    cout << "Please enter the name of the file you would like to enter: ";
    cin >> filename;

    string infilename= filename;
    infile.open(infilename.c_str());

    //Temporary test part of the program until we add the GUI just copy another person's answer or put similar ones
    //for testing purposes


    infile >> pplNum;
    //allows for all names and scores to fit in each vector
    for(i = 0; i < pplNum; i++)
    {
        people.push_back("");
        people.push_back("");
        pplscore.push_back(0);
    }
    //takes the person's name
            for(j = 0; !infile.eof(); j+ 2)
        {
            infile >> people[j];
            infile >> people[j+1];
        }

        for(i = 0; i < pplNum; i++)
        {
            //sets the numbers for the individual in question
            for(k = 0; k < qnum; k++)
            {
                infile >> pplans[k];
                if(answers[k] == pplans[k] && k < 10)
                    numtemp++;
                else if (k == 10 && answers[k] == pplans[k])
                    if(answers[answers[k]] == pplans[pplans[k]])
                        numtemp++;
            }
            pplscore.push_back(0);
            pplscore[i] = numtemp;

        }
}   
4

1 に答える 1

0
vector <int> pplans;
...
infile >> pplNum;
//allows for all names and scores to fit in each vector
for(i = 0; i < pplNum; i++)
{
  people.push_back("");
  people.push_back("");
  pplscore.push_back(0);
}
...
for(i = 0; i < pplNum; i++)
{
  //sets the numbers for the individual in question
  for(k = 0; k < qnum; k++)
  {
    infile >> pplans[k];
    ...
  }
}

に部屋を作るのを忘れましたpplans。(そして、push_backこのようなコードに依存するのではなく、実際のデータで使用することをお勧めします。)

于 2013-10-08T14:40:52.657 に答える