2017-04-01 48 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; 

您在值後添加的內容僅影響下一個輸出。