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;
}
Attachments
0001
John Doe
30.00
0002
Sheila Johnson
25.00
0003
Daniel Boone
0.00
0004
Mike Wallace
40.00
0005
Dan Rathers
32.48
0006
Ken Daniels
25.33
0007
Ezekial Flatbush
12.37
0008
Shandra Brooks
54.32
0009
Ivan Horvat
32.88
0001
0001
o
ipod
2
203.00
0002
0001
p
payment
0
203.00
0003
0001
o
stereo headphones
3
29.99
0004
0002
p
payment
0
40.00
0005
0002
o
stereo headphones
0
34.99
0006
0002
o
iphone
1
249.99
0007
0002
p
payment
0
200.00
o
0002
0008
iphone mic
2
24.99

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....

Edited 7 Years Ago by ivanhny: n/a

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 ....:-)

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