br************@gmail.com wrote:
I turned this in for my programming fundamentals class for our second
exam. I am a c++ newb, this is my first class I've taken. I got a good
grade on this project I'm just wondering if there is a better and more
efficient way to write this program.
Here's what I could come up with (yes, I'm bored :D).
Compiles and runs, didn't verify the calculations.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
static const double state_tax_rate = 0.0825;
static const unsigned federal_table_max = 4;
static const double federal_rates_m[] = { 0.179, 0.168, 0.157, 0.146,
0.125 };
static const double federal_rates_s[] = { 0.151, 0.142, 0.133, 0.124,
0.105 };
int main()
{
string first_name, last_name;
char married = 'U';
double hours_worked = 0;
double hourly_rate = 0;
double gross = 0;
double federal_tax = 0;
double state_tax = 0;
unsigned exemptions = 0;
cout << "Enter your full name: ";
cin >> first_name >> last_name;
cout << "Hours worked: ";
cin >> hours_worked;
cout << "Hourly rate: ";
cin >> hourly_rate;
while( married != 'M'
&& married != 'S' )
{
cout << "Marrital status ('M' or 'S'): ";
cin >> married;
married = toupper( married );
}
cout << "Excemptions: ";
cin >> exemptions;
if ( exemptions > federal_table_max )
exemptions = federal_table_max;
gross = hours_worked * hourly_rate;
state_tax = gross * state_tax_rate;
federal_tax = gross * ( married == 'M' ?
federal_rates_m[ exemptions ] : federal_rates_s[ exemptions ]
);
cout << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Payroll information for ";
cout << last_name << ", " << first_name << ":" << endl << endl;
cout << "Hours worked: " << hours_worked << endl;
cout << "Hourly wage: " << hourly_rate << endl;
cout << "Marrital status: " << ( married == 'M' ? "Married" :
"Single" ) << endl;
cout << "Exemptions: " << exemptions << endl << endl;
cout << "Gross pay: " << gross << endl;
cout << "Federal tax: " << federal_tax << endl;
cout << "State tax: " << state_tax << endl << endl;
cout << "Net pay: " << (gross - federal_tax - state_tax )
<< endl;
}
Notice the difference? ;)
Cheers,
Andre