0

我有一些代码来比较不同人回答的一组问题,一组是用户输入的,另一组来自 .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 回答