2017-04-01 23 views
0
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++; 
    } 
} 

输出dat文件应该有存款有两位小数,但第一行总是以科学记数法结束。例如1000.00应该是这样出来的,但是以1e + 3出现。为什么用科学记数法显示的矿床的第一个输出?

回答

2

如果您想要输出的特定格式,则必须在值之前添加说明符。例如:

fout << fixed << accounts[i].deposit; 

您在值后添加的内容仅影响下一个输出。