If anyone could give me a pointer here I would be grateful. I am making a public function that takes to container objects called bags and a file name in their arguments. I'm trying to create a loop that would extract the first line of doubles in a file and insert them into this bag/container object "firstBag" and then go to the second line and insert them into the "secondBag" object. There is a function that the bag object uses to insert numbers called insert(). Now I have figured out how to break the files lines up using the getline() function. But within that line i need to loop through each string line and pick up the double/floating point values.

essentially here is the idea

    // getline( fileobject, stringline )
   // loop through string and insert values into firstBag
  // do it again for bag2

here is what i have started

void readFiles( correaj::bag& firstBag, correaj::bag& secondBag, char fileName[]  )
{
    std::ifstream inData;
    std::string fileLine;

    inData.open( fileName ); // opens the user inserted file from main

    getline ( inData, fileLine ); 
     // start here

    inData.close();
}

Thanks all

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Change an if() to a while().

[edit]

#include <iostream>
#include <string>
#include <sstream>

int main()
{
   std::string line("123.45 67.89 34.567");
   std::istringstream iss(line);
   double value;
   while ( iss >> value )
   {
      std::cout << "value = " << value << "\n";
   }
   return 0;
}

/* my output
value = 123.45
value = 67.89
value = 34.567
*/

Edited 6 Years Ago by Dave Sinkula: n/a

Comments
Quick responder! Knows his stuff. Thanks D

Thanks dude. Worked amazing!

void readFiles( correaj::bag& firstBag, correaj::bag& secondBag, char fileName[]  )
{
    std::ifstream inData;
    std::string fileLine;

    inData.open( fileName );
    getline( inData, fileLine );

    std::istringstream iss( fileLine );

    double coef;

    while( iss >> coef )
    {
    firstBag.insert( coef );
    }

    getline( inData, fileLine );

    std::cout << "space" << std::endl;

    std::istringstream iss2( fileLine );

    while( iss2 >> coef )
    {
    secondBag.insert( coef );
    }

    inData.close();
}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.