引发“ std :: out_of_range”错误实例后终止调用

时间:2020-05-10 23:09:36

标签: c++

我必须比较文件,并使用一个文件作为字典来检查另一个文件中拼写错误的单词。运行代码时,我一直收到此错误:

Dictioanry file read
Sorting the dictionary ...
Misspelled word, withering on line 4
Misspelled word, captivity on line 5
Misspelled word, dramatize on line 12
Misspelled word, nation's on line 13
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 4294967295) > this->size() (which is 1)

Process returned 3 (0x3)   execution time : 11.190 s
Press any key to continue.

我的代码:

#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
const int Dictword = 23907;
void getDict(const string& filename,string*);
void spellCheck(char* line, int lineNumb, const string* dict);
bool wordIsInDic(const char* word1, const string* dict);
void toLowerCase(char* text);
void toLowerCase(string& text);
void insertion_sort(string* list1);
int main()
{
    const string wordfile = "unsorted_words.txt";
    const string testfile = "ihaveadream.txt";
    char buffer[1024];
    string word1;
    string* dict= new string[Dictword];
    getDict(wordfile, dict);
    insertion_sort(dict);
    int lineNumb = 0;
    ifstream fina(testfile.c_str());
    if (!fina)
    {
        cerr << "cannot open word file " << testfile << endl;
        exit(2);
    }
    while (!fina.eof())
    {
        lineNumb++;
        fina.getline(buffer,sizeof(buffer));
        if (fina.eof())
            break;
        toLowerCase(buffer);
        spellCheck(buffer,lineNumb, dict);
    }
    delete [] dict;
}
void spellCheck(char* line, int lineNumb, const string* dict)
{
    char* ptr = line;
    ptr = strtok(line," ,.\r");
    while(ptr != NULL)
    {
        if(!wordIsInDic(ptr, dict))
        {
            cout << "Misspelled word, " << ptr << " on line " << lineNumb << endl;
        }
        ptr = strtok(NULL," ,.-\r");
    }
}
void getDict(const string& filename, string* dict)
{
    ifstream fina(filename.c_str());
    if (!fina)
    {
        cerr << "cannot read word file " << filename << endl;
        exit(1);
    }
    cout << "Dictioanry file read" << endl;
    for (int i = 0; i < Dictword; i++)
    {
        fina >> dict[i];
        toLowerCase(dict[i]);
    }
}
bool wordIsInDic(const char* wordv, const string* dict)
{
    int lowv, highv, middlev;
    string word_string(wordv);
    lowv = 0;
    highv = Dictword - 1;
    string middleWord1;
    int compare1;
    while (lowv <= highv)
    {
        middlev = (lowv + highv) / 2;
        middleWord1 = dict[middlev];
        compare1 = strcmp(wordv,middleWord1.c_str());
        if (compare1 < 0)
        {
            highv = middlev - 1;
        }
        else if (compare1 > 0)
        {
            lowv = middlev + 1;
        }
        else
        {
            return true;
        }
    }
    if (word_string.substr(word_string.size() - 2, 2) == "ed")
    {
        if (wordIsInDic(word_string.substr(0, word_string.size() - 2).c_str(), dict))
            return true;
    }
    if (word_string.substr(word_string.size() - 2, 2) == "ly")

        if (wordIsInDic(word_string.substr(0, word_string.size() - 2).c_str(), dict))
            return true;
    if (word_string.substr(word_string.size() - 1, 1) == "s")
        if (wordIsInDic(word_string.substr(0, word_string.size() - 1).c_str(), dict))
            return true;
    return false;
}
void toLowerCase(char* text1)
{
    for (size_t k = 0; k< strlen(text1); k++)
        text1[k] = tolower(text1[k]);
}
void toLowerCase(string& text1)
{
    for (size_t k = 0; k < text1.size(); k++)
        text1[k] = tolower(text1[k]);
}
void insertion_sort(string* list1)
{
    int m, key;
    string tmp;
    bool found;
    cout << "Sorting the dictionary ..." << endl;
    for (int i1 = 1; i1 < Dictword; i1++)
    {
        found = false;
        tmp = list1[i1];
        for (key = 0, m = i1 - 1; m >= 0 && !found; key++)
        {
            if (tmp < list1[m])
            {
                list1[m + 1] = list1[m];
                m--;
            }
            else
            {
                found = true;
            }
        }
        list1 [m + 1] = tmp;
    }
}

0 个答案:

没有答案
相关问题