The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
easy c++ question: calculate employee's net pay
Discuss easy c++ question: calculate employee's net pay in the C Programming forum on Dev Shed. easy c++ question: calculate employee's net pay C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 20th, 2002, 08:20 PM
|
 |
Contributing User
|
|
Join Date: Jun 2001
Location: San Diego, CA
Posts: 37
Time spent in forums: 3 h 46 m 12 sec
Reputation Power: 13
|
|
easy c++ question: calculate employee's net pay
Hi there:
I'm pretty rusty to C++ and wonder if somebody would be kind to help me with this?
How could I write a program that can calculate an employee's net pay. The pay will be based on the emplyee's worked hours and the pay rate. The tax will be 30% of the gross pay.
I started like this.
PHP Code:
#include <iostream>
using namespace std;
int main ()
{
double hw, pr, net;
char ans;
do
{
cout <<"Enter number of hours worked:";
cin >> hw;
net = hw * pr * .7;
cout <<"Employee's net pay is: $";
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
cout << net << endl;
cout << "Run again (y/n)?";
cin >> ans;
}
while (ans =='y' || ans == 'Y');
return 0;
}
Any help would be greatly appreciated.
I would like to ask for First Name, Last Name and SS# at the beginning too.
I know one of the issues with what I made above is that someone can input alphabetical letters where I am asking for numbers. How could I prompt for an error if someone does that?
Also, if someone clicks yes or no at the end, it should either run agian, or quit.
Peace out,
Ponch
|

June 20th, 2002, 10:43 PM
|
 |
/(bb|[^b]{2})/
|
|
Join Date: Nov 2001
Location: Somewhere in the great unknown
|
|
Here is a simple version of how to do this, but this sounds generally like a homework assignment. If this is so, then you really need to complete these on your own. The code I listed is actually a mix between C++ and C and was written in Linux, but it should work correctly on a win compiler also.
Code:
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>
using namespace std;
#define TAX_RATE .30
int main(void) {
char *first,
*last,
*ssn,
*test,
*final_output;
float hours=0,
pay_rate=0,
gross=0,
taxes=0,
net=0;
/*-- lets create the variables --*/
final_output = new char[1024];
first = new char[50];
last = new char[50];
ssn = new char[12];
test = new char[1024];
/*-- start the loop --*/
do {
/*-- get first & last name --*/
cout << "First Name: ";
cin >> first;
cout << "Last Name: ";
cin >> last;
/*-- get ssn with error checking on length only --*/
do {
cout << "SSN: ";
cin >> ssn;
if (strlen(ssn) == 11)
break;
else
cout << "Invalid ssn. Must be in format 999-99-9999";
} while (1);
/*--
get pay rate with error checking on
null and 0 values
--*/
do {
cout << "Pay Rate: ";
cin >> test;
if (test == NULL)
cout << "Invalid pay rate.";
else if(atof(test) != 0)
break;
else
cout << "Invalid pay rate.";
} while (1);
pay_rate=atof(test);
/*--
get hours worked with error checking on
null and 0 values
--*/
do {
cout << "Hours Worked: ";
cin >> test;
if (test == NULL)
cout << "Invalid amount of hours.";
else if(atof(test) != 0)
break;
else
cout << "Invalid amount of hours.";
} while (1);
hours=atof(test);
/*-- calculate values --*/
gross=hours*pay_rate;
taxes=gross*TAX_RATE;
net=gross-taxes;
/*-- set out precision --*/
cout.precision(2);
cout.setf(ios::fixed | ios::showpoint);
/*-- create output --*/
sprintf(final_output,"\n\n%s %s ssn(%s)\n------------------------------\n",
first,last,ssn);
cout << final_output;
cout << "Gross: " << gross << endl;
cout << "Taxes: " << taxes << endl;
cout << "Net: " << net << endl << endl;
/*-- do another? --*/
cout << "Calculate Another? (y/n)?";
cin >> test;
/*-- check first value of string for y/Y --*/
} while (!strncasecmp(test,"y",1));
/*-- return memory to the heap --*/
delete [] first;
delete [] last;
delete [] ssn;
delete [] test;
return 1;
}
|

June 20th, 2002, 11:20 PM
|
 |
Contributing User
|
|
Join Date: Jun 2001
Location: San Diego, CA
Posts: 37
Time spent in forums: 3 h 46 m 12 sec
Reputation Power: 13
|
|
|
thanks bro', that will do it!!
you are the man....
peace,
ponch
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|