7 Replies - 519 Views - Last Post: 01 March 2011 - 02:10 PM Rate Topic: -----

#1 doforumda  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 24-May 09

need help in vectors

Posted 01 March 2011 - 02:14 AM

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
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
#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

Debug assertion failed
Expression: vector iterator + offset out of range


Please help how can I achieve above solution?

Is This A Good Question/Topic? 0
  • +

Replies To: need help in vectors

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: need help in vectors

Posted 01 March 2011 - 02:21 AM

Read this
http://www.cplusplus...algorithm/copy/
Was This Post Helpful? 0
  • +
  • -

#3 doforumda  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 24-May 09

Re: need help in vectors

Posted 01 March 2011 - 02:34 AM

this is error is fixed Thanks. But how can I acheive the first task please read the post above thanks
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: need help in vectors

Posted 01 March 2011 - 02:40 AM

Which is the "first task"?
Was This Post Helpful? 0
  • +
  • -

#5 doforumda  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 24-May 09

Re: need help in vectors

Posted 01 March 2011 - 02:57 AM

i changed my code to this now
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	vector<string> v1;
	vector<string> v2;
	vector<string> v3;
	vector<string> v4;
	string line;

	ifstream in("names.txt", ios::in);
	while(!in.eof()) {
		getline(in, line);
		if(line.find("separator")) {
			v1[0].push_back(line);
		}
	}
	system("pause");
	return 0;
}


what I am trying to do is to find the separator word in text file. if it finds that word then anything that is read before this separator put that in v1[0] and then read the file again after the first separator and when find the second separator put anything that is read before second separator and after first separator in v1[1]. same for v2, v3 and v4.

here is my text file now
level1
studentName
Student Name One
FatherName
Father Name of Student One

separator

studentName
Student Name One
FatherName
Father Name of Student One

separator

level2
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

level3
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

level4
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


Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: need help in vectors

Posted 01 March 2011 - 03:11 AM

You seem to be describing a magical program.
The program can't 'go back' and do stuff before a separator and then jump about like a human brain can.
It can only deal with the piece of information it has at this moment.

Here is what you want to do.
Start loop
..Read a line.
..If that line is not "separator" then
....concatenate this line onto the string in the current vector element
..Otherwise
....Increment to next vector element
End loop

This post has been edited by janotte: 01 March 2011 - 03:12 AM

Was This Post Helpful? 0
  • +
  • -

#7 doforumda  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 16
  • Joined: 24-May 09

Re: need help in vectors

Posted 01 March 2011 - 03:17 AM

View Postjanotte, on 01 March 2011 - 03:11 AM, said:

You seem to be describing a magical program.
The program can't 'go back' and do stuff before a separator and then jump about like a human brain can.
It can only deal with the piece of information it has at this moment.

Here is what you want to do.
Start loop
..Read a line.
..If that line is not "separator" then
....concatenate this line onto the string in the current vector element
..Otherwise
....Increment to next vector element
End loop


that is exactly what I want to do. How can I do this? Can you help because I am new in c++ and I desparately need help for this. thanks
Was This Post Helpful? -1
  • +
  • -

#8 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: need help in vectors

Posted 01 March 2011 - 02:10 PM

You have most (or at least half) of what I described written already.

Just try working through the algorithm I have given you one line at a time.

If you try I am confident you can do this.

If you do have problems getting the code to work correctly then show us the code you have written and describe, exactly, the problems you are having.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1