3

I'm brand new here so I apologize in advance if I don't format this correctly. I have a C++ assignment that requires me to take a data file, process it, and then output a data file. I think I've done the input and output correctly, but my calculations are coming out with the wrong number of decimal places, and one of my calculations is incorrect altogether. Here is the inData.txt file I was given:

Giselle Robinson Accounting

5600 5 30

450 9

75 1.5

Here is my source code:

    #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;
    }

Here is what I'm getting in the outData.txt file:

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

And here is what I'm trying to achieve:

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

So my problems are 1) getting all of the numbers (except for the "Number of Coffee Cups Sold" field) to show two decimal places, and 2) getting $4116.00 instead of $4200 for the "Paycheck" field. I think it's probably something to do with the "cout << fixed << showpoint << setprecision(2);" line, as well as something being wrong with the my variables. We did an example assignment in class that was very similar to this one and I did everything the same way we did it in that assignment, so here I am. I hope it's an easy fix, and I would really appreciate any feedback at all even if it's not a direct solution. Thanks in advance :D

Joe Butto
  • 31
  • 2

2 Answers2

1

Try calling setprecision on the output file stream outFile directly:

outFile << fixed << showpoint << setprecision(2)<< monthlyGrossSalary

dividedbyzero
  • 136
  • 1
  • 5
0

The paycheck value you get is correct for the formula you wrote. I'm guessing that the formula you meant to write is this

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

i.e. the tax is deducted before the bonus is applied, not at the same time as you wrote it.

This formula gives the value you were expecting, 4116.

Both bugs are good examples of the fact that programs do exactly what you tell them to do, not what you thought you told them to do.

john
  • 85,011
  • 4
  • 57
  • 81