0

我正在阅读以下格式的文件

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;

        }
    }
4

1 回答 1

3

你不是逐行阅读。将条件替换为

while( getline(inputstream, line) )

如果您使用operator>>它将仅提取第一个单词到line.

于 2010-04-17T16:39:21.520 回答