Ошибка нижнего индекса вектора вне диапазона

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 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 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;

        }
}   
Теги:
vector
stl

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 с фактическими данными, а не полагаться на код, подобный этому.)

  • 0
    Поэтому я мог бы просто использовать: for (k = 0; k <qnum; k ++) pplans.push_back (infile) ;?
  • 0
    @ user2750772: Нет, for(k=0;k<qnum; k++){int n; infile>>n; pplans.push_back(n);} . Или int n; while(infile>>n){pplans.push_back(n);} .

Ещё вопросы

Сообщество Overcoder
Наверх
Меню