Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9202381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:14:40+00:00 2026-06-17T23:14:40+00:00

So I’m experimenting with trying to add first and last names into a double

  • 0

So I’m experimenting with trying to add first and last names into a double linked list.
I have a various text files of different lengths with the format “string, string”, and am using list> to store my data.

I am using this code:

typedef std::list< std::pair<string,string> > listPair;

…

list<pair<string, string> > mylist;
ifstream myFile; 
myFile.open("20.txt");

pair<string, string> stuff;
while (myFile >> stuff.first >> stuff.second)
{
    mylist.push_back(stuff);
}

listPair::iterator iter = mylist.begin();

for(;iter != mylist.end();iter++)
{
    string s = (*iter).first;
    cout << s << endl;
    string c = (*iter).second;
    cout << c << endl;
}

now the problem i’m having is that firstly, the last item in the list is not being added.
like every file just misses the end line, so that’s a little confusing.

also, I’m doing a “mylist.size()” to ensure all the names have been added, and it’s confusing me because say for a text file containing 99 names, i.e 99 lines of text, it will say (not forgetting it only reads in 98 due to missing the last line) that the list has size 48.

WHY 48?
Is it something to do because I have done pairs, which still would not make sense as if it was not reading in pairs there would actually be double the about, since the pairs are just to take the first and last name as one value.

Mind boggling to me.

once again thanks for all your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:14:42+00:00Added an answer on June 17, 2026 at 11:14 pm

    I have a feeling your file doesn’t actually have spaces between the values as you’ve described, so it looks like this:

    one,two
    three,four
    five,six
    seven,eight
    nine,ten
    

    If you were to run your program on this, the size of the list will be 2 (floor(number_of_lines/2), which for you would give 48) and the last line won’t have been put in the list at all. Why?

    Firstly, each call to std::ifstream::operator>>(std::string&) will extract up until it hits some white space. In this case, the first white space on the first line is the \n at the end of it. So on the first iteration, stuff.first will be "one,two" and then the next line will be read into stuff.second, making it "three,four". This is then pushed into the list. The next two lines are read in the same way, giving you the pair {"five,six","seven,eight"}. On the next iteration, the first operator>> will extract "nine,ten" and the second will fail, causing the while condition to end and the last line to be discarded.

    Even if you did have spaces, you would end up with commas in the first of every pair, which is certainly not what you want.

    The nicer way to approach a problem like this is to use std::getline to extract a line, and then parse that line as appropriate:

    std::string line;
    std::pair<std::string, std::string> line_pair;
    while (std::getline(myFile, line)) {
      std::stringstream line_stream(line);
      std::getline(line_stream, line_pair.first, ',');
      std::getline(line_stream, line_pair.second);
      mylist.push_back(line_pair);
    }
    

    I also recommend using std::vector unless you have a good reason to use std::list.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.