C++字符串流 istringstream 的问题

发布于 2024-08-29 02:42:23 字数 1681 浏览 11 评论 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;

        }
    }

I am reading a file in the following format

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

where the items are: loan id, principal, months, interest rate.

I'm not sure what it is that I am doing wrong with my input string stream, but I am not reading the values correctly because only the loan id is read correctly. Everything else is zero. Sorry this is a homework, but I just wanted to know if you could help me identify my error.

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;

        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-09-05 02:42:23

你没有按行阅读。将条件替换为

while( getline(inputstream, line) )

If you use operator>> 它将仅提取 line 的第一个单词。

You are not reading linewise. Replace the condition by

while( getline(inputstream, line) )

If you use operator>> it will extract only the first word to line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文