i created a program that read from a txt file and use getline() to retrive every line from my file. But i wanted to store the digit into list<int>, when i do a print of stl list i get the result which not i wanted. i wanted to have "2 4 6 8 10 1 3 5 7 9" in the list when i print from stl list
what am i suppose to do?

getline(fin,line);
    if(line == "abcd 2 4 6 8 10 1 3 5 7 9")
    {
        line.erase(0,5);
        for(int i=0; i<line.length(); i++)
        {
            line.erase(0,i);
            int num = atoi(line.c_str());
            myList.push_back(num);
        }

this would be less error-prone

if(line == "abcd 2 4 6 8 10 1 3 5 7 9")
  {
    istringstream stm(line) ;
    string eaten ;
    stm >> eaten ; 
    int number ;
    while( stm >> number ) myList.push_back(number);
  }

thanks vijayan121!!! the code works but the first digit is not printed out even thought i have set the iterator to the begin.

Why don't you show us some [[I][/I]code[I][/I]]?

it should. this is what i get.

#include <list>
#include <string>
#include <iostream>
#include <sstream>
#include <iterator>
#include <algorithm>
using namespace std ;

int main()
{
  string line = "abcd 2 4 6 8 10 1 3 5 7 9" ;
  list<int> myList ;

  if(line == "abcd 2 4 6 8 10 1 3 5 7 9")
  {
    istringstream stm(line) ;
    string eatit ;
    stm >> eatit ; 
    int number ;
    while( stm >> number ) myList.push_back(number);
  }
  copy( myList.begin(), myList.end(), 
        ostream_iterator<int>( cout, " " ) ) ;
  cout << '\n' ; 
}
/** 
>c++ list_int.cc && ./a.out
2 4 6 8 10 1 3 5 7 9
*/

thanks vijayan121!!! i manage to solve it!!! thanks for the help!!! :)

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.