Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    [SOLVED] Trouble with out of range vector iterator

    Okay, so I'm not really a C++ noob at all, but I am a little rusty at the moment. Still, I CANNOT figure out what is wrong with the following code. I'm using Visual Studio 2010.

    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    using namespace std;
    struct student {
    	string studentID;
    	vector<string> term;
    	vector<vector<string>> courses;
    int main() {
    	vector<student> recordVec; 
    	student record;
    	vector<string> termVec;
    	vector<vector<string>> classVec;
    	string studentData;
            string name1 = "datain";
    	ifstream infile;;
    	if (
    		cout << "Error opening " << name1 << endl;
    		return 1;
    	string linein;
    	while (getline(infile, linein))
    		int j = 0;
    		if (studentData != record.studentID) {
    			// Get class data
    			int test = linein.size();
    ***			for (j; j < linein.size(); ++j)
   = classVec;
    			record.term = termVec;
    I THINK I got all of the pertinent code. Anyway, it fails at the *** line. I CANNOT figure out why. If I modify it to be linein.size()-30, it STILL gives me an error. That makes zero sense.

    Anyone? Thanks . . .

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Seattle, WA, USA
    What is the exact error that you are getting?

    Also, the line that you mentioned has no vectors in it, so I don't know what you mean by "vector iterator".

  3. #3
    I guess my use of terms was somewhat flawed. I'm not using an iteration type, it, but the for loop iterates through the string linein. Also, I should not have referred to linein as a vector since it is a string, but it was late, and the error I get on that line is

    Assertion error: vector subscript out of range.

    I'm paraphrasing because I'm mobile, so I can't quote verbatim. The error is a runtime error, not compile time.

    It really doesn't make sense. The line is 90 characters. I set a temp variable to track linen.size(), and as the for loop hits 89, the program fails. if I subtract even 30 from the condition in the for loop, the loop still throws an error. I don't get it.

    I'll try to get the exact error when I get home.

  4. $spacer_open
  5. #4
    :P I figured out my problem. I had my breakpoints set wrong, and the error was actually on the next line:


    Since classVec is a multidimmentional vector, I had to create a temporary vector of strings to push onto classVec. You can't push onto a an internal vector if the parent vector is empty.

    Guess I was too tired last night. Stupid mistake. Thanks, though!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts