Question

I'm reading in a file that is a question pool that has the type of question, chapter, how many points it is worth, the question and the answer. This bit of code is checking to see if the min and max chapters(from user input) are in range(from a file of unknown size). I know that it adds an extra line at the end of the vector, which is causing the error, but how can I fix it? The code is:

void checker(int min, int max, string file) {

        ifstream myfile;
        string line;
        vector<int> numlist;

        myfile.open(file);
        while (myfile.is_open()) {
            if (!getline(myfile, line)) {
                break;
            } else {
                vector<string> chap = split_string(line);
                int chapter = str2int(chap[2]);
                numlist.push_back(chapter); //This is where the error is. Makes vector go out of range.
            }
        }

        int small = 1000;
        int large = 0;
        for (size_t i = 0; i < numlist.size(); i++) {
            if (numlist[i] < small) {
                small = numlist[i];
            }
        }
        for (size_t i = 0; i < numlist.size(); i++) {
            if (numlist[i] > large) {
                large = numlist[i];
            }
        }
        if (min > max) {
            cout
                << "Error: Please enter a number lower than or equal to the maximum chapter: "
                << endl;
            cin >> min;
            cout << endl;
        } else if (min < small) {
            cout
                << "Error: Please enter a number bigger than or equal than the minimum chapter ("
                << small << "): " << endl;
            cin >> min;
            cout << endl;
        } else if (max > large) {
            cout
                << "Error: Please enter a number bigger than or equal than the maximum chapter ("
                << large << "): " << endl;
            cin >> max;
            cout << endl;
        }
        myfile.close();
}
Was it helpful?

Solution 2

void checker(int min, int max, string file) {

    ifstream myfile;
    string line;
    vector<int> numlist;

    myfile.open(file);
    while (!myfile.eof()) {
        if (!getline(myfile, line)) {
            break;
        } else if(line!="") {
            vector<string> chap = split_string(line);
            int chapter = str2int(chap[2]);
            numlist.push_back(chapter);
        }
    }
//other code cut out because it was not important

I just turned in this code with my assignment and it worked! chap[2] is the third element of a line from a file that was read in. There are many lines in the file that were (with help from other functions and classes) turned into their own vector. But the third element of each vector (a line read in from a file) was a number which was the chapter number (chap[2]). Now this proves that chap[2] WAS NOT the culprit. Here is a sample of a line from the file: short@1@10@In inheritance, what is the technical term for the "parent" class?@base class

OTHER TIPS

Its telling you that ‘chap‘ doesn't have 3 elements:

str2int(chap[2]);

You should run under the debugger to see what chap looks like, if you are using Visual Studio hit F11 to step into main.

--- Edit ---

This appears to be your problem code:

            vector<string> chap = split_string(line);
            int chapter = str2int(chap[2]);

if split_string returns a vector with less than 3 elements, the line

            int chapter = str2int(chap[2]);

is invalid. The executable will misreport the exact location of the crash when you run the executable outside the debugger because of the way the checks are implemented.

What you need to do is:

std::vector<std::string> chap = split_string(line);
if(chap.size() > 2) {
    int chapter = str2int(chap[2]);
    numlist.push_back(chapter);
}

or possibly

if (!line.empty()) {
    std::vector<std::string> chap = split_string(line);
    if (chap.size() > 2) {
        int chapter = str2int(chap[2]);
        numlist.push_back(chapter);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top