output being saved into a separate file

Hi folks,

Im taking a C++ class and the assignment is to make a program that calculates sales tax and then saves it to a text doc that can be viewed later. I guessed on a lot of this stuff so I was wondering if anyone can peak at this code and offer some advice. Any help would be greatly, greatly appreciated!!!!



// Problem 5: Sales Tax,
// problem5.cpp

// This program calculates sales tax and puts it into a file


#include <iostream>
#include <iomanip> // needed for setw

#include <fstream> //needed for writing files out

using namespace std;

int main()

{
ofstream sales_tax_data;
outputFile.open("sales_tax_data.txt") //this creates the sales tax doc

char month[11];
int year;
double total_income;
float county_sales_tax, state_sales_tax, total_sales_tax;

// This is where the program collects data from you

cout << “Please enter the month for this report: \n”;
cin >> month;

cout << “Please enter the year for this report: \n”;
cin >> year;

cout << “Please enter the total income for this month: \n”;
cin >> total_income;

cout << "Thanks, now writing this to a file \n";

// This is where the math happens

county_sales_tax = (total_income / .02);
state_sales_tax = (total_income/.04);
total_sales_tax = (total_income/.06)

// This is where the results are saved into the file

outputFile << left << "Month:" << month <<" " << year << "\n";
outputFile << left << "Total Collected: " << right << "$ " << total_income << "\n";

outputFile << left << "County Sales Tax: " << right << "$ "
<< county_sales_tax << "\n";

outputFile << left << "State Sales Tax: " << right << "$ " << state_sales_tax << " \n";

outputFile << left << "Total Sales Tax: " << right << "$ "
<< total_sales_tax << "\n";

outputFile.close();
cout << "Done! \n";

return 0;
}
Try splitting up the program into functions so that main has less to do. Thats what I usually do, that way each function takes care of its own.
Also it may be easier to use a class for tax and output the tax object but what you've got there is good.
Topic archived. No new replies allowed.