Hello my problem is that I am not for sure my vector < vector < string > > addresses;
is correct. What I am looking for is to read in from a file which looks like:

name:dfsd@msn.com
name:dfsd@msn.com
name:dfsd@msn.com

Once my code parses through the file, addresses should look like:
0 1
0 [name][dfsd@msn.com]
1 [name][dfsd@msn.com]
2 [name][dfsd@msn.com]
3 [name][dfsd@msn.com]

I am not for sure if addresses.push_back(ads);, in the code below, will make the above possible. Here is the while loop:

while(!inputFile.eof())
   {
   
   //call seperateLine() to parse through the input 
   //seperateLine(input,addresses,lastName);
   len = input.find(':', startPoint);
if (len == string::npos)
   len = input.size() - startPoint;
else
   len -= startPoint;
	  
name = input.substr(startPoint, len);  
ads.push_back(name);
addresses.push_back(names);
//ads.push_back(name);
for (int i=0; i < name.size(); i++)
   tolower(name[i]);
len += 1;
end = input.size() - len;
addy = input.substr(len, end);
ads.push_back(addy);
      	
addresses.push_back(ads);

getline(inputFile,input);
}

Thanks!

Recommended Answers

All 4 Replies

I am not for sure if addresses.push_back(ads);, in the code below, will make the above possible.

Can you clarify what you mean by this? Your code is confusing because you have ads.push_back(name); and then addresses.push_back(names); . Did you mean names instead of ads in the first part?

It seems like what you are doing is essentially correct. Have you tried printing out the 2D vector and seeing what the results are?

Yes I have, but I am not for sure if I am doing it corrrectly.

Would the code be for the print out:
for(i=0; i < addresses.size(); i++)
for(j=0; j < addresses.size(); j++)
cout << addresses[j] << endl;

Thank you!

Yes, with your first vector as the first row, and your second vector as the second row.

Ok well I am now getting a segmentation fault.

I am outputting the code and the first run through is great. It prints
out:
Name
name@asdf.com
Then it prints out:
a6 (which idk what that is) then it seg faults. Any ideas? Ill post the updated code with the code for printing it out. Thanks!

getline(inputFile,input);
   while(!inputFile.eof())
   {
   
   //call seperateLine() to parse through the input 
   //seperateLine(input,addresses,lastName);
   len = input.find(':', startPoint);
if (len == string::npos)
   len = input.size() - startPoint;
else
   len -= startPoint;
	  
name = input.substr(startPoint, len);  
ads.push_back(name);
//ads.push_back(name);
for (int i=0; i < name.size(); i++)
   tolower(name[i]);
len += 1;
end = input.size() - len;
addy = input.substr(len, end);
ads.push_back(addy);
      	
addresses.push_back(ads);

getline(inputFile,input);
}	
cout << "ads size is" << ' ' << addresses.size() << endl;
for(j=0;j<addresses.size(); j++)
   for (int i=0; i < addresses.size(); i++)
      cout << addresses[j][i] << endl;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.