如何将文件的特定部分读入私有数据成员
How to read the specific part of file into the private data members
我正在尝试读取包含客户详细信息的virtual void BillRecord::ReadCustDetails(ifstream &fin)
文本文件。代码工作正常,但我不知道如何读取整个地址和负值并将它们存储给私人成员。我尝试了以下代码,但输出错误。
文本文件:
Phone
Origin
George Carter
24 Dingo St Exeter SA
-0.018
28
Gas
EA
Paul Scott
21 Beach Rd Barham NSW
15.48786
356567
Elect
...
我的输出:
Origin
George
Carter
0
24
所需输出:
Origin
George Carter
24 Dingo St Exeter SA
-0.018
28
我的程序:
public:
BillRecord();
virtual void ReadCustDetails(ifstream &fin);
private:
BillType BType;
string Supplier; // Supplier's name
string Name, Address; // Customer's name and address
double BillAmount;// Amount in dollars and cents of this bill
int DaysSinceLastReading; // Days since last reading
};
virtual void BillRecord::ReadCustDetails(ifstream &fin)
{
fin >> Supplier;
getline(fin,Name);
getline(fin,Address);
fin >>AccountBalance;
fin >>BillAmount;
fin >>DaysSinceLastReading;
fin >>i;
DaysSinceLastReading=i;
//put the code here for reading the customer details part of the file
record only into the private data members
cout << Supplier<< endl;
cout << Name << endl;
cout << Address << endl;
cout << BillAmount << endl;
cout << AccountBalance << endl;
}
您的代码只有小问题。 尝试更具体。 除了这段代码之外,真正的问题是什么? 您一定在其他功能中存在一些问题。尝试将它们发布在这里。我试图尽可能多地修复。
void BillRecord::ReadCustDetails(ifstream &fin)
{
fin >> Supplier;
fin.ignore();
getline(fin,Name);
getline(fin,Address);
fin >> BillAmount;
fin >> AccountBalance;
fin >> DaysSinceLastReading;
cout << DaysSinceLastReading;
cout << Supplier << setw(16);
cout << Name << setw(27);
cout << Address << setw(15);
cout << BillAmount << setw(14);
cout << AccountBalance << setw(8);
cout << DaysSinceLastReading << setw(6);
}
相关文章:
- 用于访问容器<T>数据成员的正确 API
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 数据成员SFINAE的C++17测试:gcc vs clang
- 派生类是否可以在抽象工厂设计模式中具有数据成员
- 如何在c++中定义以struct为数据成员的类中的构造函数
- 静态数据成员模板专用化的实例化点在哪里
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 使用指针访问数组中的对象数据成员
- 友元函数无法访问私有数据成员 (c++)
- 我可以在 C++ 中将数据成员/变量从其定义之外添加到结构中吗?
- 为什么将一个结构的引用设置为等于另一个结构只会更改一个数据成员?
- 将私有数据成员添加到野牛生成的类中
- 输入数据成员未按要求工作
- 二维矢量数据成员
- 在类 A 中创建类型为 B 类的向量 - 访问数据 [C++] [成员在两个类中都是私有的]
- 调用在 HXX 文件中声明的静态数据成员
- | 9 |错误:非静态数据成员'matrix :: row'| 9 |错误:数组绑定不是在']']'令牌|之前的整数常数
- 结构绑定:与公共数据成员(继承的基类)结合
- 绑定数据成员在VS2012中不起作用
- Qt 属性、底层数据成员、绑定和开销