C++ class assignment

I am having a few problems writing a program for my class. I have to write a program that allows the user to input his name and gross salary. After that I have to show how much money is being taken away due to taxes. So for example things like federal and state taxes medicare SS pension and health insurance. These things I have some understanding. The problem is that I have to include an output file. How would I do that? Also I have to include the iomanip library but I don't know why that is needed. Another thing is how do I set number to only show up to two decimal places? And finally how would I use setw() right/left to justify the text.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream> 
#include <string> 
#include <fstream> 
#include <iomanip> 
using namespace std; 

int main() 
{


	string Name = string();
	double Gross_Salary; 
	double Federal_Income_Tax;
	double State_Tax; 
	double Social_Security_Tax; 
	double Medicare_Tax; 
	double Pension_Plan; 
	double Health_Insurance = 75; 
	double Net_Salary; 

	//Inputs 
	cout << "Name:"; 
	cin >> Name; 
	cout << "Gross Salaray:";
	cin >> Gross_Salary;

	//Outpus 
	Federal_Income_Tax = Gross_Salary * .15;
	cout << "Federal Income Tax:$" << Federal_Income_Tax << "\n"; 
	State_Tax = Gross_Salary * .035;
	cout << "State Tax:$" << State_Tax << "\n"; 
	Social_Security_Tax = Gross_Salary * .0575;
	cout << "Social Security Tax:$" << Social_Security_Tax << "\n"; 
	Medicare_Tax = Gross_Salary * .0275; 
	cout << "Medicare Tax:$" << Medicare_Tax << "\n"; 
	Pension_Plan = Gross_Salary * .05;
	cout << "Pension Plan:$" << Pension_Plan << "\n"; 
	cout << "Health Insurance:$" << Health_Insurance << "\n"; 
	Net_Salary = Gross_Salary - (Federal_Income_Tax + State_Tax + Social_Security_Tax + Medicare_Tax + Pension_Plan + Health_Insurance); 
	cout << "Net Salary:$" << Net_Salary << "\n"; 
	
	
	
	
	return 0;
}


This is my code so far. Help will be kindly appreciated. Thank you!
Outputting to file is as simple as:

ofstream outFile("filename goes here");


Iomanip is a library with useful functions, including a setprecision() that will allow you to round floats to two decimal places as well setw() that lets you set the column width.

http://www.cplusplus.com/reference/iomanip/setprecision/
http://www.cplusplus.com/reference/iomanip/setw/


Last edited on
thanks but one more thing! where one my lines should I put the precision and the setw?
Setprecision() should apply for anything after it was used. I believe setw() needs to be used everytime but I may be wrong.

It would be a good idea to use constants or variables for the values so you can change them in one place.

const int PRECISION = 5;
const int WIDTH = 3;

cout << setprecision(PRECISION) << foo << setw(WIDTH) << foo;


EDIT - actually just use variables so it can be controlled by the user if need be.
Last edited on
Topic archived. No new replies allowed.