Tutorial 5

Let's make it a little fancier

Remember the previous program? Well it was a basic payroll system, that simply calculated the nett wage of a worker. It allowed tax to be calculated on two different tax brackets. However, this was really it's only appealing feature. We could not use it to calculate the nett pay for different workers who were paid in a different way, overtime, commission, etc. So what do you suppose we will be doing this time around? Yes, you guessed it! We'll be modifying the original program to accommodate these new features of different worker types. However, while we are doing that, I suppose we'll throw in a few extra modifications to make things more visually appealing to the user. Nothing fancy, just clearing the screen so it doesn't get cluttered and creating a summary table to be displayed at the end. I have changed the tax calculation to a more believable amount of tax. The basic layout of the program is the same. Really it's only a couple of extra if statements that allow us to calculate the wage for different workers.



The thing to remember is that this program is essentially the same thing as the last. If you break down the problem you will notice one very important thing. The tax calculation for each worker, no matter how they are paid is exactly the same. Hence, all we need to do is calculate their individual gross wages so that we can get the nett wage from this. There is no need for a separate calculation for their tax. A couple of notes about my code below. Firstly you will notice more #include statements at the beginning. These are for various functions I have used later in the code. The conio.h header file allows us to use the clrscr( ) fuction that clears the screen of all text. The stdlib.h header file allows us to use the exit( ) function that allows us to terminate the program at any time. Finally the iomanip.h header file allows ud to use statements that control output to the screen. For example we can specify the number of decimal places we want for a number or set the output position on the screen for some text. Apart from these things that you can get from your reference book there is not much more to explain about my coding in this application.


//this is another simple program that presents the user with the types of worker
//prompts them for an input and then proceeds with the necessary prompts and
//calculations for that chosen worker. It has some things that help its presentation
//they are simple enough to learn and with practice you won't even think about them.
//basically it's an elaborated version of the previous program.

//pre-processor directives
#include <iostream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <conio.h>

void main (void)
{
	//declaration and initialisation of variables
	int code=0, workers[4]={0}, numHrs=0, numWorkers=0;
	float gross, nett=0, hrPay=0, wkSales=0, numPces=0,
	       pcePay=0, tax=0, taxable=0, totNett=0;

	//use of sentinel to keep loop going until user wishes to stop
	while(code != -1)
	{
		//selection table
		cout<<"Enter 1 for manager.\n";
		cout<<"Enter 2 for hourly worker.\n";
		cout<<"Enter 3 for commission worker.\n";
		cout<<"Enter 4 for piecerate worker.\n\n";
		cout<<"Enter code (-1 to end): ";
		cin>>code;

		if((code > 0 && code < 5) || code == -1)
		{
			//use the code to determine which worker to pay
			if(code == 1)
			{
				clrscr( );
				cout<<"Manager selected.\n\n\n"<<"Enter salary: ";
				cin>>gross;
				cout<<"\nThe managers gross pay is: "<<gross<<endl;
				//we will use the array elements to track how many of each worker is paid 
				workers[0]++;
			}

			else if(code == 2)
			{
				clrscr( );
				cout<<"Hourly worker selected.\n\n\n"<<"Enter no. of hours worked: ";
				cin>>numHrs;
				cout<<"Enter pay per hour: ";
				cin>>hrPay;

				if(numHrs > 40)
					gross=((numHrs - 40) * (1.5 * hrPay)) + (40 * hrPay);

				else
					gross=numHrs * hrPay;

				cout<<"The hourly workers gross pay is: ";
				//make use of the precision settings for decimal places
				cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint)<<gross<<endl;
				workers[1]++;
			}

			else if(code == 3)
			{
				clrscr( );
				cout<<"Commission worker selected.\n\n\n"<<"Enter gross weekly sales: ";
				cin>>wkSales;
				gross =(wkSales * .57) + 250;
				cout<<"The commission workers gross pay is: ";
				cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint)<<gross<<endl;
				workers[2]++;
			}

			else if(code == 4)
			{
				clrscr( );
				cout<<"Piece-rate worker selected.\n\n\n"<<"Enter no. of pieces: ";
				cin>>numPces;
				cout<<"Enter pay per piece: ";
				cin>>pcePay;
				gross=numPces * pcePay;
				cout<<"The piece-rate workers gross pay is: ";
				cout<<setprecision(2)<<setiosflags(ios::fixed | ios::showpoint)<<gross<<endl;
				workers[3]++;
			}

			else if(code == -1)
			{
				//clear the screen to display the table
				clrscr( );
				//table of total no of workers paid using the array there are lots of
				//iomanip flag statements here so I would advise you to go and check
				//them out to see what they do
				cout<<"Total number of managers paid:"<<setw(21)
				      <<setiosflags(ios::right)<<workers[0]<<endl;
				cout<<"Total number of hourly workers paid:"<<setw(15)
				      <<setiosflags(ios::right)<<workers[1]<<endl;
				cout<<"Total number of commission workers paid:"<<setw(11)
				      <<setiosflags(ios::right)<<workers[2]<<endl;
				cout<<"Total number of piece-rate workers paid:"<<setw(11)
				      <<setiosflags(ios::right)<<workers[3]<<endl;
				cout<<"The total number of workers paid is:"<<setw(15)
				      <<setiosflags(ios::right)<<numWorkers<<endl;
				cout<<"The total of the salaries paid is:"<<setw(17)
				      <<setprecision(2);
				cout<<setiosflags(ios::right | ios::fixed | ios::showpoint)<<totNett<<endl;
				//use the exit function to terminate the application
				exit(1);
			}
			
			//calculates total of array and hence workers paid
			numWorkers = workers[0] + workers[1] + workers[2] + workers[3];
			
			//calculation of tax
			if(gross < 100)
				nett = gross;

			else
			{
				taxable = gross - 100;

				if(taxable >= 150)
					tax = ((taxable - 150) * .48) + (150 * .27);

				else
					tax = taxable * .27;

				nett = gross - tax;
				totNett = totNett + nett;
				cout<<"The workers net pay is: "<<setprecision(2)
				      <<setiosflags(ios::fixed | ios::showpoint)<<nett<<"\n\n\n";
			}
		}

		else
			cout<<"Invalid code. Enter correct code:\n\n";
	}
}



And there we have it. A very fancy application that will calculate the nett and gross wages for four different types of worker. Finally presenting a summery table, displaying the quantity of each worker paid and the total amount nett pay received by the workers. As you can see after reading down through the code, there are very few differences and the basic calculation that founded our first program is still very much the only major task in this more complicated version. Apart from working out the gross wage calculations, this code should cause you no problems at all, if you understood the first program.

Previous Index Next


© Redsetter 2000