void readAccountInfo()
{
ifstream fin;
fin.open("Accounts.dat");
string tempID;
string tempFirst;
string tempLast;
string tempDeposit;
string tempRate;
string tempYear;
int i = 0;
while (fin >> tempID >> tempFirst >> tempLast >> tempDeposit >> tempRate
>> tempYear)
{
accounts[i] =
{ tempID, tempFirst, tempLast, stof(tempDeposit), stof(tempRate), stoi(tempYear)};
i++;
}
}
void writeAccountInfo()
{
ofstream fout;
fout.open("test.dat");
int i = 0;
while (i < 30 && accounts[i].yearTerm != 0)
{
fout << accounts[i].ID << " " << accounts[i].firstName << " "
<< accounts[i].lastName << " ";
fout.precision(2);
fout << accounts[i].deposit << fixed;
fout.precision(1);
fout << " " << accounts[i].rate << fixed;
fout.precision(0);
fout << " " << accounts[i].yearTerm << endl << fixed;
i++;
}
}
输出数据文件应该具有两位小数,但第一行总是以科学计数法结束。例如1000.00应该是这样出来的,而是以1 e + 3出现。
答案 0 :(得分:2)
如果需要输出的特定格式,则必须在值之前添加说明符。例如:
fout << fixed << accounts[i].deposit;
在值仅影响下一个输出后添加的内容。