使用重载的>>运算符从文件读取

cad*_*d4j 2 c++ operator-overloading

我试图从文件中读取客户的姓名,身份证和贷款信息.该文件设置如下:

Williams, Bill
567382910
380.86
Davidson, Chad
435435435
400.00
Run Code Online (Sandbox Code Playgroud)

基本上,每次我使用新名称时,信息都将被放入Customer类的新对象中.我的问题是,我正在尝试从文件中读取,但我不确定如何正确地重载操作符,只读取文件中的3行,并将它们放在正确的位置.

我在这里创建客户并打开文件:

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

myFile.open("customer.txt");
while (myFile.good())
{
  myFile >> C;
  custList.insertList(C);
}
}
Run Code Online (Sandbox Code Playgroud)

这就是我在.cpp文件中的Menu类.以下是我的.cpp文件中Customer类的重载运算符的代码(我知道该怎么做).

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


}
Run Code Online (Sandbox Code Playgroud)

我不确定如何只获得三条线并将它们放入客户的各自位置:

string name
string id
float loanamount
Run Code Online (Sandbox Code Playgroud)

如果有人能帮我解决这个问题,我真的很感激.

Cor*_*lks 7

就像是:

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;
}
Run Code Online (Sandbox Code Playgroud)

这是一个工作样本.