Using std::string.find() leads me to std::out_of_range

115 views Asked by At

I tried to write a code about searching student ID from text file... I used std::string.find() function but I got an exception "std::out_of_range"

(Actually I'm not good at English. I'm so sorry..)

The code is below.

void search_ID(std::ifstream& f1, int sort_num)
{
    std::string ID;

    std::cout << "Student ID keyword?: " << std::endl;
    getline(std::cin, ID);

    if (ID.length() > 10)
    {
        std::cout << "You should type under 10 words" << std::endl;
        exit(0);
    }

    std::string line;
    int pos;
    int size = 0;

    while (f1) {
        getline(f1, line);
        **if ((pos = line.substr(20, 10).find(ID)) != std::string::npos) {**
            size++;
        }
    }
// ...
// ...
}

The error is on bold typing. Why is it happened..??

0

There are 0 answers