我在这里是全新的,所以如果我格式化不正确,请提前向您道歉。我有一个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)为“Paycheck”字段获取$ 4116.00而非$ 4200。我认为这可能与“cout <

最佳答案

尝试直接在输出文件流setprecision上调用outFile:
outFile << fixed << showpoint << setprecision(2)<< monthlyGrossSalary

关于c++ - 无法获得固定的setprecision double 以显示小数位,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48718432/

10-11 04:04