2013-10-08 5 views
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; 

     } 
} 
+3

여기에'+ = 2'를 쓰려고 한 것 같습니다 : for (j = 0;! infile.eof ; j + 2)' –

+1

txt 파일에는 무엇이 있습니까? – Beta

+0

디버거 (예 : g ++를 사용할 경우'gdb')를 사용하여 프로그램을 실행하면 프로그램에서 어설 션이 나타나는 지점을 확인해야합니다 – codeling

답변

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 TPOT94

+0

@ user2750772 : 아니요, for (k = 0; k > n; pplans.push_back (n);}'. 또는'int n; while (infile >> n) {pplans.push_back (n);}'. – Beta

관련 문제