I'm a student having trouble with creating two vectors of objects for a homework assignment. The first vector creates fine, and prints out the values it imports (readM). The program hangs on the second vector(readT). I've checked the text files and they seem ok. I even tried substituting a simple vector object that only read one field per (and made sure the text file matched), and the program still hangs. It seems only to want to read one vector object. What in the @!@@@ am I doing wrong??? Any help much appreciated!

#include <iostream>
#include <vector>
#include <string>
#include <fstream>
using namespace std;

ifstream in;

struct Master    {   
          int custnum;
          string fcustname;
          string lcustname;
          float balanceDue;          
};  

struct Trans  {
          int trannum;
          int custnum;
          string ordertype;
          string item;
          int quantity;
          float money;   // Holds item cost or payment
};          


void readM(vector<Master>& mastlist)
{
         struct Master p;
         in.open("master.txt"); 
         while(!in.eof())
         {
               in>>p.custnum;
               in>>p.fcustname;
               in>>p.lcustname;
               in>>p.balanceDue;
               mastlist.push_back(p);                     
         }
         for(int i=0; i< mastlist.size(); i++) {
               cout<<mastlist[i].custnum<<" ";
               cout<<mastlist[i].fcustname<<" ";
               cout<<mastlist[i].lcustname<<" ";
               cout<<mastlist[i].balanceDue<<endl;  
         }
         in.close();     
}     

void readT(vector<Trans>& translist)
{
         struct Trans q;
         in.open("trans.txt");
         while(!in.eof())
         {
               in>>q.trannum;
               in>>q.custnum;
               in>>q.ordertype;
               in>>q.item;
               in>>q.quantity;          
               in>>q.money;
               translist.push_back(q);
         }
         for(int i=0; i < translist.size(); i++) {
               cout<<translist[i].trannum<<" ";
               cout<<translist[i].custnum<<" ";
               cout<<translist[i].ordertype<<" ";
               cout<<translist[i].item<<" ";
               cout<<translist[i].quantity<<" ";
               cout<<translist[i].money<<endl;
         }
         in.close();                      
}   
                   
int main()
{         
         vector<Master> arrM;
         vector<Trans> arrT;
         readM(arrM);
         readT(arrT);
         cout<<endl<<endl<<endl;
         cout<<"End of Master Record"<<endl;

         system("pause");
         return 0;
}

Ignore the cout's, they were only put in there for me to see if the program was actually reading and if the for loops would output the vector, I know I should use iterators but if I can't even get a simple for loop working......

I'm thinking it has something to do with eof(), however getline as far as I know only reads strings so that presents a whole another set of problems....

Taking the first 25 it seems to be hanging up and rereading the same one for some reason

1 1 o ipod 2 203
2 1 p payment 0 20
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203
3 1 o stereo 0 203

I'm still looking at it but I figured you might need this info.

Thanks for the help, I figured it out, there was a problem with the input file. I should have started with just one record and built it up from there, once pared down it would read an entire record, it seems a string was trying to be read into an integer field. On another note, how could I use getline and in>> both to read strings and respective integers without hanging. It seems if I try to use the >> before getline, it doesn't work, that's why my strings only have one word in them ....:-)

Put in an in.ignore() after the getline to catch the stray newline after the string is parsed.

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.