2010-04-17 95 views
0

我讀的格式如下C++問題字符串流istringstream

1001 16000  300 12.50 
2002 24000  360 10.50 
3003 30000  300  9.50 

其中的項目是一個文件:貸款ID,本金,月利率。

我不確定這是什麼,我做錯了我的輸入字符串流,但我沒有正確讀取值,因爲只有貸款ID被正確讀取。其他一切都是零。對不起,這是一項家庭作業,但我只想知道你是否可以幫助我找出我的錯誤。

if(inputstream.is_open()){ 

     /** print the results **/ 
     cout << fixed << showpoint << setprecision(2); 
     cout << "ID " << "\tPrincipal" << "\tDuration" << "\tInterest" << "\tPayment" <<"\tTotal Payment" << endl; 
     cout << "---------------------------------------------------------------------------------------------" << endl; 

     /** assign line read while we haven't reached end of file **/ 
     string line; 
     istringstream instream; 
     while(inputstream >> line){ 
      instream.clear(); 
      instream.str(line); 

      /** assing values **/ 
      instream >> loanid >> principal >> duration >> interest; 


      /** compute monthly payment **/ 
      double ratem = interest/1200.0; 
      double expm = (1.0 + ratem); 
      payment = (ratem * pow(expm, duration) * principal)/(pow(expm, duration) - 1.0); 

      /** computer total payment **/ 
      totalPayment = payment * duration; 

      /** print out calculations **/ 
      cout << loanid << "\t$" << principal <<"\t" << duration << "mo" << "\t" << interest << "\t$" << payment << "\t$" << totalPayment << endl; 

     } 
    } 

回答

3

您沒有閱讀linewise。如果你使用operator>>它將只提取第一個字line

while(getline(inputstream, line)) 

更換條件。

+0

你是對的。解決了這個問題。謝謝。 – user69514 2010-04-17 16:44:17