Выражение: ошибка нижнего индекса вектора в Visual Studio - PullRequest
1 голос
/ 26 апреля 2019

Я столкнулся с ошибкой векторного индекса вне диапазона, но я не смог выяснить, где ошибка, есть 6 функций плюс основная функция. Некоторые коды были опущены. Есть ли что-то, на что я мог бы обратить внимание при написании кода, чтобы избежать этой ошибки?

Функция 1

int matchInput(vector<string> fileRead, int index) {
    for (size_t i = index; i < fileRead.size(); i++) {
        if (fileRead[i].find(in) != string::npos) {
            return i;
        }
    }
    return 0;
}

Функция 2

vector<string> grabReplies(vector<string> fileRead, int index) {

    vector<string> replies;

    for (size_t i = index + 1; i < fileRead.size(); i++) {
        while (fileRead[i].find(out) != string::npos) {
            replies.push_back(fileRead[i]);
        }
    }
    return replies;
}

Функция 3

string pickReply(vector<string> replies) {
    string reply;

    int item = rand() % replies.size();

    return replies[item];
}

Функция 4

vector<string> fileRead(string fileDir)
{
    vector<string> fileRead;            
    string line;                        

    ifstream rawFile(fileDir);          

    while (getline(rawFile, line)) {    
        fileRead.push_back(line);       
    }

    return fileRead;                    
}

Функция 5

bool wordMatch(string userInput, vector<string> fileRead, int index)    
{
    string delimiter = " ";             
    vector<string> wordsUserInput;      
    vector<string> wordsOnFile;         
    string s;                           
    string token;                       

    s = userInput;                      

    size_t pos = 0;                     
    token = "";                         
    while ((pos = s.find(delimiter)) != string::npos) {     
        token = s.substr(0, pos);                           
        wordsUserInput.push_back(token);                    
        s.erase(0, pos + delimiter.length());               
    }

    s = fileRead[index];            //s set to on file record

    pos = 0;                        //reset position counter
    token = "";                         //reset token
    while ((pos = s.find(delimiter)) != string::npos) {     
        token = s.substr(0, pos);                           
        wordsOnFile.push_back(token);                       
        s.erase(0, pos + delimiter.length());               
    }

    for (size_t i = 0; i < wordsUserInput.size(); i++) {    
        for (size_t j = 0; j < wordsOnFile.size(); j++) {
            if (wordsUserInput[i] == wordsOnFile[j]) {      
                return true;    
            }
        }
    }
    return false;               
}

Функция 6

string sanitising(string dirty) {
    string clean;           
    clean = dirty.erase(0, 8);      
    return clean;           
}

Основная функция

int main() {

fileDir = "../src/reply.txt";                       
fileVector = fileRead(fileDir);

cout << "Welcome to the ChatBot" << endl            
     << "a program by 27023119"  << endl << endl
     << "Please ask me anything" << endl
     << "to end chat just enter quit"    << endl;

while(1)                                            
{
    index = 0;
    cout << ">";                                
    getline(cin, userInput);                    

    if (userInput == "quit"){                   
        cout << "Thanks for using Chat Bot!" << endl;   
        break;                                          
    }

    index = matchInput(fileVector, index);

    if (wordMatch(userInput, fileVector, index) == true) {
        replies = grabReplies(fileVector, index);
        reply = pickReply(replies);
        cout << sanitising(reply) << endl;
    }
    else {
        cout << "Sorry I don't understand." << endl;
    }
}

return 0;                                           
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...