I've looked this up and I haven't gotten anything to work yet, but basically I need this code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
ifstream inFile;
ofstream outFile;
string firstName, lastName, department;
double salary, bonus, taxes;
double distance, timeTravel;
int coffeeCups;
double cupCost;
double paycheck, aveSpeed, salesAmount;
inFile.open("inData.txt");
outFile.open("outData.txt");
inFile >> firstName >> lastName >> department;
inFile >> salary >> bonus >> taxes;
inFile >> distance >> timeTravel;
inFile >> coffeeCups >> cupCost;
paycheck = salary salary*(bonus/100) - ((salary salary*(bonus/100))*(taxes/100));
aveSpeed = distance/timeTravel;
salesAmount = coffeeCups * cupCost;
outFile << "Name: " << firstName << " " << lastName << ", Department: " << department << endl;
outFile << "Monthly gross salary: $" << salary << ", Monthly bonus: " << bonus << "%" << ", Taxes: " << taxes << "%" << endl;
outFile << "Paycheck: $" << paycheck << endl << endl;
outFile << "Distance traveled: " << distance << " miles, Traveling time: " << timeTravel << " hours" << endl;
outFile << "Average speed: " << aveSpeed << " miles per hour" << endl << endl;
outFile << "Number of coffee cups sold: " << coffeeCups << ", Cost: $" << cupCost << " per cup" << endl;
outFile << "Sales amount = $" << salesAmount;
inFile.close();
outFile.close();
return 0;
}
to output this:
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
but instead, I'm getting this:
Name: Giselle Robinson, Department: Accounting
Monthly gross salary: $5600, Monthly bonus: 5%, Taxes: 30%
Paycheck: $4116
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
CodePudding user response:
// Add this before you start writing to the file
outFile << std::fixed << std::setprecision(2) << std::setfill('0');
// Your original output code below
outFile << "Name: " << firstName << " " << lastName << ", Department: " << department << endl;
...
credit https://stackoverflow.com/a/15327758/10808581
CodePudding user response:
Here is some sample code; I hope you will find it useful.
stringstream ssdd;
ssdd.str("");
double aa=243.765;
ssdd << "------------" << std::endl;
ssdd << std::fixed << std::setprecision(2) << aa << std::endl;
ssdd << std::fixed << std::setprecision(3) << aa << std::endl;
ssdd << std::fixed << std::setprecision(4) << aa << std::endl;
std::cout << ssdd.str();
std::cout << "------------";
Output is:
------------
243.76
243.765
243.7650
------------