2

I'm attempting to read from a file a customer's name, id, and loan information. The file is setup like this:

Williams, Bill
567382910
380.86
Davidson, Chad
435435435
400.00

Basically, every time I come to a new name, the information is going to be placed into a new object of the Customer class. My problem is, I'm trying to read from the file but I'm not sure how to overload the operator correctly to read only 3 lines from the file like I want to and put them in the right place.

I create the customer and open the file here:

Menu::Menu()
{
Customer C;
ifstream myFile;

myFile.open("customer.txt");
while (myFile.good())
{
  myFile >> C;
  custList.insertList(C);
}
}

This is just what I have in my .cpp file for a Menu class. Here is my code (the tiny bit that I know how to do) for the overloaded operator in my .cpp file for the Customer class.

istream& operator >> (istream& is, const Customer& cust)
{


}

I'm not sure how to only get the three lines and place them into their respective places inside of a Customer which are:

string name
string id
float loanamount

If anyone could help me out with this I'd really appreciate it.

cadavid4j
  • 171
  • 2
  • 6
  • 19
  • 1
    Looping on `.good()` or `.eof()` is rarely a good idea. [See this question.](http://stackoverflow.com/questions/4324441/testing-stream-good-or-stream-eof-reads-last-line-twice) It's simpler to just do `while (myFile >> C) {}` – Blastfurnace Sep 26 '12 at 22:55

1 Answers1

7

Something like:

istream& operator >> (istream& is, Customer& cust) // Do not make customer const, you want to write to it!
{
    std::getline(is, cust.name); // getline from <string>
    is >> cust.id;
    is >> cust.loanAmount;
    is.ignore(1024, '\n'); // after reading the loanAmount, skip the trailing '\n'
    return is;
}

And here's a working sample.

Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • Would I be able to just write it like that and have getline automatically get the next line that way? – cadavid4j Sep 26 '12 at 22:42
  • @cadavid4j: I'm not sure what you mean? – Cornstalks Sep 26 '12 at 22:43
  • My file is set up in the way that I showed above, would just saying getline once, go to the next line to input the cust.id and cust.loanamount? – cadavid4j Sep 26 '12 at 22:44
  • This looks like it should work but you might need to declare it a `friend` of the Customer class if the member variables aren't `public`. – Blastfurnace Sep 26 '12 at 22:47
  • When I put this in, the only thing I get is Error: no operator" >>" matches these operands. Also, I do already have it as a friend. Edit : This error is now gone, I didn't see where I should have taken out the const. – cadavid4j Sep 26 '12 at 22:49
  • @Cornstalks Thank you, I'm still not sure if it works yet because loanamount is being read in from the file as a string but loan amount is a float. Is there any way to change it from a string to a float before attempting to put it in loanamount? – cadavid4j Sep 26 '12 at 22:54
  • @cadavid4j: What do you mean? It's reading it as a formatted float. `std::istream::operator >>` can extract textual floats as actual float data types. Look at my working sample. You'll see `Customer::loanAmount` is indeed a `float`, not a `std::string`. – Cornstalks Sep 26 '12 at 22:56
  • @Cornstalks Okay nevermind. I was just confused. Thank you very much for your help. – cadavid4j Sep 26 '12 at 22:59