Malfunctioning program

I wrote this program today for a class but when I enter a name at the start of the program and I input a space the program seems to go hay wire. Any ideas of why that might be? Also how can I get the all the zeros after the decimals to align as well as the dollar symbols?

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/* 
	Author: Jose Ortiz
	Date: 9/21/13
	Program: Employee Taxes
*/ 

#include <iostream> 
#include <string> 
#include <fstream> 
#include <iomanip> 
using namespace std; 

int main() 
{
	ofstream out;
	out.open("output.txt");

	// Header 

	cout << "\t\t\t=======================\n";
	cout << "\t\t\tEmployee Taxes & Salary\n"; 
	cout << "\t\t\t=======================\n";

	string Name = string();
	double GrossSalary; 
	double FederalTax;
	double StateTax; 
	double SSNTax; 
	double MedicareTax; 
	double PensionPlan; 
	double HealthInsurance = 75; 
	double NetSalary; 

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

	//Outpus 
	cout << fixed;
	cout << setprecision(2);

	FederalTax = GrossSalary * .15;
	StateTax = GrossSalary * .035;
	SSNTax = GrossSalary * .0575;
	MedicareTax = GrossSalary * .0275;
	PensionPlan = GrossSalary * .05;
	NetSalary = GrossSalary - (FederalTax + StateTax + SSNTax + MedicareTax + PensionPlan + HealthInsurance); 

	cout << "Federal Income Tax:" << right << setw(10) << "$" << FederalTax << "\n";
	cout << "State Tax:" << right << setw(10) << "$" << StateTax << "\n"; 
	cout << "Social Security Tax:" << right << setw(10) << "$" << SSNTax << "\n"; 
	cout << "Medicare/Medicaid Tax:" << right << setw(10) << "$" << MedicareTax << "\n"; 
	cout << "Pension Plan:" << right << setw(10) << "$" << PensionPlan << "\n";
	cout << "Health Insurance:" << right << setw(10) << "$" << HealthInsurance << "\n"; 
	cout << "Net Salary:" << right << setw(10) << "$" << NetSalary << "\n";

	return 0;
}
at line 24: string Name; // this is all that you have to write

for your problem, it's because cin stops reading after it encounter whitespace, newline or tab, that is why you can't use cin, instead you should use getline() when trying to read with whitespaces:

syntax:
getline ( <source>, <variable_where_you_want_to_store> );

based on your problem, then you should write: getline ( cin, Name );

EDIT: your other problem:

1
2
3
4
5
6
7
cout << left<< setw(25)  << "Federal Income Tax:"    << "$ " << setw (5) << FederalTax  << "\n";
cout << left<< setw(25)  << "State Tax:"             << "$ " << setw (5) << StateTax    << "\n";
cout << left<< setw(25)  <<"Social Security Tax:"    << "$ " << setw (5) << SSNTax      << "\n";
cout << left<< setw(25)  <<"Medicare/Medicaid Tax:"  << "$ " << setw (5) << MedicareTax << "\n";
cout << left<< setw(25)  <<"Pension Plan:"           << "$ " << setw (5) << PensionPlan << "\n";
cout << left<< setw(25)  <<"Health Insurance:"       << "$ " << setw (5) << HealthInsurance << "\n";
cout << left<< setw(25)  <<"Net Salary:"             << "$ " << setw (5) << NetSalary   << "\n";
Last edited on
Thank you man that really helped!! Saved me a whole bunch of time
Topic archived. No new replies allowed.