C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old June 20th, 2002, 08:20 PM
ponch9's Avatar
ponch9 ponch9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: San Diego, CA
Posts: 34 ponch9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m 11 sec
Reputation Power: 8
Send a message via ICQ to ponch9 Send a message via AIM to ponch9 Send a message via Yahoo to ponch9
Question 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 hwprnet;
     
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

Reply With Quote
  #2  
Old June 20th, 2002, 10:43 PM
Onslaught's Avatar
Onslaught Onslaught is offline
/(bb|[^b]{2})/
Dev Shed Demi-God (4500 - 4999 posts)
 
Join Date: Nov 2001
Location: Somewhere in the great unknown
Posts: 4,834 Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level)Onslaught User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Day 23 h 30 m 30 sec
Reputation Power: 88
Send a message via ICQ to Onslaught
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;
}

Reply With Quote
  #3  
Old June 20th, 2002, 11:20 PM
ponch9's Avatar
ponch9 ponch9 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2001
Location: San Diego, CA
Posts: 34 ponch9 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 29 m 11 sec
Reputation Power: 8
Send a message via ICQ to ponch9 Send a message via AIM to ponch9 Send a message via Yahoo to ponch9
thanks bro', that will do it!!
you are the man....

peace,
ponch

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > easy c++ question: calculate employee's net pay


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway