C ++ file input looping thru sets of data

This is my first posting. I need to process the contents of a file that has about 4000
records where every 12 to 36 lines represent a discrete set of data that needs
to be fed into an object.

I have been able to open the file, fill an object but moving to the next
block of 12 to 36 lines has me scratching my head. How do I set up a loop
to do this.

If the “dealer” or “part number” changes then the block of data is done and the next object should be filled.
 Implementation ( .cpp) , the data file itself appear below and the class (.h).

#pragma warning(disable:4786)

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
#include <stdlib.h>
#include "partNo.h"

using namespace std;

int main()

      string s1, s3, s4, s5, s6;  // s1 dealer code, s3 partnumber
      char delim[]=",";  // comma delimter to look for
      int m = 0;  // index for moving thru vector
      ifstream f("test.txt");  // open file
      vector<string> vs;            // vector to hold each string
      vector<string>::iterator strit ;
                  string s;
                  vs.push_back(s);  // put the entire string in the vector

      while(strit != vs.end()) // loop thru the vector parsing each line
            PartNumber p;                  // part number class

            string s2 = vs[m];  // using vector index assign string to string variable
      int j = 0, k =0;  // placement variables
      int i=0;   // for moving thru string
      int dcnt = 0; // count delimters to parse out dealer info
            i = s2.find_first_of(delim, j);  // find first delimiter , set i to end of string
            dcnt = dcnt + 1;  // increment delimiter count

            // each delimiter parses out a field based on dcnt
                  case 1 :
                        s3.assign(s2,j,i-j); // assign s2 with k start and i end of string , grab dealer
                  //      cout << s3 << " " ;
                        p.setDealer(s3);   // update dealer in part object
                         break;   // causes exit from loop

                  case 2 :
                        s4.assign(s2,j,i-j);  // grab partnumber
                        p.setPartNumber(s4);  // update partnumber in part object
                        //  cout << s4 << " "  ;

                  case 3 :
                        // s5.assign(s2,j,i-j); // date  unused field
                        //      p.set
                        // cout << s5 << " " ;

                  case 4 :
                        s6.assign(s2,j,10);   // units
                        //cout << s6 << endl;
                        // convert the string to double
                         p.setDmd(atof(s6.c_str()));  // update dmd vector in part object
                  j = i+1;  // move the start for delim search forward

            //       }while(i < s2.length());    // VERIFY THIS
             }while( i < s2.find_last_of(delim, 0));  // and i >= 0 ???

            p.getDmd();  // prints the units of demand contents of vector      

            m++;   // increment counter for vector
      // code needs to be added to put each object into a container for future work

       return 0;

/* test.txt note should stop at change
in dealer or partnumber.


/// class /////
#include <iostream>

#include <string>

using namespace std;

class PartNumber

      public :
            friend ostream& operator<< (ostream& ostr, const PartNumber& obj);

            setDealer( string&  s) ;  // address constants here and below
            setPartNumber(string& s);
            string getDealer() ;
            void getPartNumber();
            setDmd(const double& units);
            void getDmd();

      private :
            string strDealer;
            string strPartNumber;
            double unitDemand;
            vector<double> vdmd;  // hold units of demand
            vector<double>::iterator p ;


void PartNumber::getDmd()
            p = vdmd.begin();
            while(p != vdmd.end())
                  cout << *p << "    " << "units stored in obj's vector" << endl ;

// set the unitDemand variable
PartNumber::setDmd(const double& units)

// set dealer code
PartNumber::setDealer(string& s)
      strDealer = s;

// set dealer code
PartNumber::setPartNumber(string& s)
      strDealer = s;

// get the dealer code
string PartNumber::getDealer()
      return strDealer;

// output the object
ostream& operator<< (ostream& ostr, const PartNumber& obj)   // output
      ostr << obj;
      return ostr;
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


strit = vs.begin();
while(++strit != vs.end()) // loop thru the vector parsing each line
marcon33Author Commented:
thanks for the thought. I can process each string held by the vector and even move thru each element of the vector. It is how to stop and start over with a new object that is the problem.
My latest thought is maybe a queue (?) could be used at the beginning , popping the strings that all have the same dealer code and part , then stopping and starting over when they change.
If I understand the problem correctly, you want to create a new object if either of the first two numbers change(dealer or part number)

I would create the following variables outside of the main while loop:

vector<PartNumber*> vp; //vector of part numbers
PartNumber* pn = new PartNumber();
vp.push_back(vp); // initialize the first element of vp
int pObjIndex = 0; //keep track of position in vp (unnecessary, but simple)
in your case statements, only do the string assign, i.e.
 case 1 :

then after the case statement, test for a change in part or dealer:

   if(s3 != (vp[pObjIndex].getDealer()) || s4 != (vp[pObjIndex].getPartNumber())) {
      //create new partnumber object
      PartNumber* pn = new PartNumber();
      //push it onto the vector
      //increment index - this is not a necessary step

//now put your information into the top object in vp

//I might also suggest more useful names for your strings
//such as currDealerStr - s# is a pain to maintain
//Instead of using pObjIndex you could pop_back the last
//element from vObjIndex or use an iterator for vp.end()-//1;

Ok - I hope I answered the right question... :)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
marcon33Author Commented:
That gives me plenty to go on. Great clarity. Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.