#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    San Diego, CA
    Posts
    37
    Rep Power
    14

    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
  2. #2
  3. /(bb|[^b]{2})/

    Join Date
    Nov 2001
    Location
    Somewhere in the great unknown
    Posts
    5,163
    Rep Power
    792
    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;
    }
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2001
    Location
    San Diego, CA
    Posts
    37
    Rep Power
    14
    thanks bro', that will do it!!
    you are the man....

    peace,
    ponch

IMN logo majestic logo threadwatch logo seochat tools logo