1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

need help in vectors

Discussion in 'C' started by doforumda, Mar 1, 2011.

  1. doforumda

    doforumda New Member

    Joined:
    Mar 1, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hi I am trying to read text from text file and put it in a vector v1. then I copy text from v1 to another vector v2.
    my text file is in this format
    Code:
    studentName
    Student Name One
    FatherName
    Father Name of Student One
    
    separator
    
    studentName
    Student name Two
    FatherName
    Father name of Student Two
    
    separator
    
    studentName
    Student Name three
    FatherName
    Father Name of Student three
    
    separator
    
    studentName
    Student name four
    FatherName
    Father name of Student four
    
    so while writing text to v2 from v1 I dont want to write "studentName", FatherName and separator. I do not want to write these because these are my separators. I just want to write student name one, father name of student one and so on.

    here is my code so far
    Code:
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <fstream>
    using namespace std;
    
    int main()
    {
    	vector<string> v1;
    	vector<string> v2;
    	string line;
    
    	ifstream in("names.txt", ios::in);
    	while(!in.eof()) {
    		getline(in, line);
    		v1.push_back(line);
    	}
    	//cout << v1.size();
    	copy(v1.begin(), v1.end(), v2.begin());
    	int ii;
    	for(ii = 0; ii < v2.size(); ii++) {
    		cout << v2[ii];
    	}
    	system("pause");
    	return 0;
    }
    
    while running the above code it also displays this error

    Code:
    Debug assertion failed
    Expression: vector iterator + offset out of range
    
    Please help how can I achieve above solution?
     
  2. Targen

    Targen New Member

    Joined:
    Mar 5, 2011
    Messages:
    1
    Likes Received:
    1
    Trophy Points:
    0
    Hello. First thing, the error you get is from the copy() function you use to make v2 equal to v1. The simple way to fix this is to remove that and instead use an equals sign:

    v2 = v1;

    That gets rid of the error. Now, for the lines of text you do not want to display, I could only think of one way to do so, and there may be a better way, but this does work. All you would have to do is have an if statement under the line:

    getline(in, line);

    This if statement would pretty much just say, if line contains "studentName", "FatherName", or "separator", continue (literally, use a continue statement). Else, push line onto vector.

    To me, that is the simplest way to achieve the solution you want. Other than just removing studentName, FatherName, and separator from the text file. But I assume you can't do that. Anyway, I hope this helped.

    Oh, and you might want to close the text file at the end of your code also. I'm not sure if you just hadn't gotten to that yet, but it wasn't in your code, so I thought I'd mention it.

    Kurt Bales
    UAT Student
     
    shabbir likes this.

Share This Page