-1

The person class should have the following functions: • Constructor(s) • Destructor - optional • Get - read first name, last name, and age from an input stream • Put - write last name, first name, and age to an output stream

I was trying to implement the get() so that it could read the text from a file, but I'm kinda stuck.

#include <iostream>
#include <string>
#include "person.h";
using namespace std;

person::person()
{  
   fName = "Default";
   lName = "Default";
   age   = 0;
}

bool person::get(istream &in)
{ 
  in >> fName >> lName >> age;
  return(in.good());
}

// Output
void person::put(ostream &out)
{ 
  out << lName << fName << age;
}

person.h

#include <iostream>
using namespace std;

// Person header.h
class person
{  public:
        person();                       // Empty constructor.
        bool get(istream &);            // Read input from file.
        void put(ostream &);            // Write to an output stream.

        // Operators to compare.
        bool operator < (person);       //lesser.
        bool operator > (person);       //greater.
        bool operator == (person);      //equal.

        string fName;                   //First Name.
        string lName;                   //Last Name.
        int age;                        //Age.
};

main.cpp

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;

#include "person.h"

void main()
{ 
  int i,x;
  string name;
  fstream infile;
  person info[20];

  //Open File.
  cout << "Enter the file name: ";
  cin >> name;
  infile.open(name.data(),ios::in);


  // loop
  while (!infile.eof())
  {  

     if(infile.good())

        {
            for (i = 0; i < 20; i++)
            { 

              info[i].get(cin);
            }

        };

  }; 
  infile.close();
}

It freezes whenever I try to use get() to load input from a file to an array. However, it works when I do:

infile >> info[i].lName >> info[i].fName >> info[i].age;

Here is my data from the file.

Ann Christensen  70
Carlos Morales   68
David Bowman     45
Frank Bowman     37
John Bowman      30
Kathleen Gueller 34
Mark Bowman      42
Mark Bowman      13
Richard Bowman   47
Susan Cox        36

Thank you.

HazemGomaa
  • 1,458
  • 2
  • 12
  • 21
Minh Le
  • 33
  • 1
  • 6

1 Answers1

-1

If I'm reading your code correctly, this is because you are calling info[i].get(cin), while you wanted to do info[i].get(infile). The way it is now, it tries to read input from the console, thus is "hanging".

Also, I believe the for (i = 0; i < 20; i++) loop is no good here. A better approach here would be to use a vector to hold your correctly read persons.

std::vector<person> info;
//...

// loop
while (!infile.eof())
{  
    if(infile.good())
    {
        person new_person;
        if (new_person.get(infile))
            info.push_back(new_person);
    }
}

Or even simpler:

person new_person;
while (new_person.get(infile))  //you can keep overwriting the object
    info.push_back(new_person); //because vector makes a copy of it
slawekwin
  • 5,987
  • 1
  • 42
  • 51
  • http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – kfsone Sep 14 '16 at 06:37