【问题标题】:Can't get fixed setprecision doubles to show decimal places无法获得固定的 setprecision 双精度以显示小数位
【发布时间】:2018-02-10 07:40:03
【问题描述】:

我是新来的,所以如果我的格式不正确,我提前道歉。我有一个 C++ 任务,需要我获取一个数据文件,对其进行处理,然后输出一个数据文件。我想我已经正确地完成了输入和输出,但是我的计算得出的小数位数错误,而且我的一个计算完全不正确。这是给我的 inData.txt 文件:

Giselle Robinson Accounting

5600 5 30

450 9

75 1.5

这是我的源代码:

    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>

    using namespace std;

    int main()
    {
        string firstName, lastName, department;
        double monthlyGrossSalary, bonusPercent, taxPercent, paycheck,
        travelDistance, travelTime, averageSpeed;
        int coffeeCupsSold; double costOfCupOfCoffee, coffeeSalesAmount;

        ifstream inFile;
        ofstream outFile;

        outFile.open("outData.txt");
        inFile.open("inData.txt");
        if (!inFile)
        {
            cout << "Sorry, could not open file!\n";
            system("pause");
            return 1;
        }

        cout << fixed << showpoint << setprecision(2);

        while (!inFile.eof())
        {
            //input
            inFile >> firstName >> lastName >> department >> monthlyGrossSalary         
            >> bonusPercent >> taxPercent >> travelDistance >> travelTime      
            >> coffeeCupsSold >> costOfCupOfCoffee;

            //process
            paycheck = ((monthlyGrossSalary + (monthlyGrossSalary * (bonusPercent * 0.01))) - (monthlyGrossSalary * (taxPercent * 0.01)));
            averageSpeed = (travelDistance / travelTime);
            coffeeSalesAmount = (coffeeCupsSold * costOfCupOfCoffee);

            //output
            outFile << "Name: " << firstName << " " << lastName << "," << " " << "Department: " << department << '\n'
            << "Monthly Gross Salary: " << "$" << monthlyGrossSalary << ", " << "Monthly Bonus: " << bonusPercent << "%, " << "Taxes: " << taxPercent << "%" << '\n'
            << "Paycheck: " << "$" << paycheck << '\n' << '\n'
            << "Distance Traveled: " << travelDistance << " miles, " << "Traveling Time: " << travelTime << " hours" << '\n'
            << "Average Speed: " << averageSpeed << " miles per hour" << '\n' << '\n'
            << "Number of Coffee Cups Sold: " << coffeeCupsSold << ", " << "Cost: " << "$" << costOfCupOfCoffee << " per cup" << '\n'
            << "Sales Amount = " << "$" << coffeeSalesAmount << endl;
    }

    inFile.close();
    outFile.close();

    system("pause");
    return 0;
    }

这是我在 outData.txt 文件中得到的内容:

Name: Giselle Robinson, Department: Accounting
Monthly Gross Salary: $5600, Monthly Bonus: 5%, Taxes: 30%
Paycheck: $4200

Distance Traveled: 450 miles, Traveling Time: 9 hours
Average Speed: 50 miles per hour

Number of Coffee Cups Sold: 75, Cost: $1.5 per cup
Sales Amount = $112.5

这就是我想要实现的目标:

Name: Giselle Robinson, Department: Accounting
Monthly Gross Salary: $5600.00, Monthly Bonus: 5.00%, Taxes: 30.00%
Paycheck: $4116.00

Distance Traveled: 450.00 miles, Traveling Time: 9.00 hours
Average Speed: 50.00 miles per hour

Number of Coffee Cups Sold: 75, Cost: $1.50 per cup
Sales Amount = $112.50

所以我的问题是 1) 让所有数字(“售出的咖啡杯数量”字段除外)显示两位小数,以及 2) 在“薪水”字段中获得 4116.00 美元而不是 4200 美元。我认为这可能与“cout

【问题讨论】:

  • 也许你想再次阅读std::precistion()
  • 请提供 MCVE。没有它,根据网站指南,您的问题将偏离主题。
  • 您在cout 上调用了setprecision(等),而不是在outFile 上。
  • 4200 是代码中公式中paycheck 的正确值。你一定是公式错了。

标签: c++ double precision fixed calculation


【解决方案1】:

尝试在输出文件流outFile上直接调用setprecision

outFile &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setprecision(2)&lt;&lt; monthlyGrossSalary

【讨论】:

    【解决方案2】:

    你得到的薪水值与你写的公式是正确的。我猜你要写的公式是这样的

    paycheck = monthlyGrossSalary - monthlyGrossSalary * (taxPercent * 0.01);
    paycheck = paycheck + paycheck * (bonusPercent * 0.01);
    

    即税款是在应用奖金之前扣除的,而不是在你写它的同时。

    这个公式给出了你期望的值,4116。

    这两个错误都是很好的例子,说明程序完全按照你告诉它们做的事情,而不是你以为你告诉他们做的事情。

    【讨论】:

      猜你喜欢
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-26
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      • 1970-01-01
      相关资源
      最近更新 更多