无法获得固定的setprecision双倍显示小数位

时间:2018-02-10 07:40:03

标签: c++ double precision fixed calculation

我是全新的,所以如果我没有正确格式化,我会提前道歉。我有一个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的“Paycheck”字段。我认为这可能与“cout&lt;&lt; fixed&lt;&lt; showpoint&lt;&lt; setprecision(2);”有关。线,以及我的变量有问题。我们在课堂上做了一个与这个非常相似的例子,我做的就像我们在那个任务中做的一样,所以我在这里。我希望这是一个简单的解决方案,即使它不是一个直接的解决方案,我也会非常感谢任何反馈。提前致谢:D

2 个答案:

答案 0 :(得分:1)

尝试直接在输出文件流Option Explicit Sub Column_Locate() Dim current_month As Variant Dim column_find As Range Dim user_data As Range current_month = Worksheets("Inputs").Cells(4, 4).Value Set user_data = Worksheets("Unique Users Data").Range("B2:C3") With Worksheets("Feeder") 'reference "Feeder" worksheet Set column_find = .Range("E2:CZ2").Find(current_month, LookIn:=xlValues, LookAt:=xlWhole).Offset(8, -1) 'search referenced sheet range "E2:CZ2" for 'current_month' value If Not column_find Is Nothing Then 'if the searched value has been found With user_data 'reference "source" range Debug.Print column_find.Address(0, 0) column_find.Resize(.Rows.Count, .Columns.Count).Value = user_data.Value ' reference a "target" range as the one with found value but with same size as "source" range and write values End With End If End With End Sub 上调用setprecision

outFile

答案 1 :(得分:0)

您获得的薪水价值对于您编写的公式是正确的。我猜你打算写的公式是这个

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

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

此公式给出了您期望的值,4116。

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