Tutorial 4

Your First Real Program

Okay you've stayed with me this long so I must be doing something right. So now it's down to the real stuff, writing the programs that will tackle real problems and get you under way. For starters I have made up an example that will give you an idea of how to break down a problem and then put the appropriate code into your program to give the desired result. The task that I have chosen is a simple little system to take in a persons gross wage and calculate their net wage given that they pay tax @27% on the first £150 of which £100 is tax free and @37% on the rest. If a person makes less than or exactly £100 then they pay no tax. The program will run in a loop so that more than one wage can be calculated per program execution and the program will be exited via the appropriate user input. No details will be kept on the calculation after it has finished. By the way I presume that you have gotten your hands on a reference book of some description as it would be rather difficult for me to give you information on every command, etc. of the language. I am presuming that you have some way to get a background to the code that I give you. If not then continue on and I hope that you can follow it, the comments should help you. However, it would be better if you did get a book of some sort.


Okay first things first. We can see that this task is but a simple calculation that is performed on a figure entered by the user and the result of the calculation is displayed to the screen. Secondly and less important is the fact that the program continues to run in a loop until the user enters the response that will terminate the program. Getting back to the calculation we can see that there are two parts to it. Firstly the tax is calculated at 27% on the first £150 the person makes, £100 of this is tax free, and at 37% on every pound after that but no tax if they earn less than or exactly £100. This calculation is very simple but how would we implement it into the program. We must consider the fact that no calculation will take place if the person earns £100 or less. We must have a condition to decide this. If the wage is greater than £100 then we must check to see if it is greater than £150. If this condition fails then we have no need for the 37% tax calculation. Otherwise the entire calculation is carried out. So how would our pseudo code look?


Sentinel loop to keep program going.
Prompt user for input.
Get input from user.
If input is less than £100 then return that amount as the nett wage.
Else if greater than £100 then check to see if greater than £150.
If greater than £150 then perform full calculation and return adjusted value.
Else just calculate tax at 27% on the taxable wage and return the adjusted value.


Note a sentinel loop can be simply a while loop that keeps the program going until the user enters in the appropriate value to stop it. Say, for example, we have the condition while(userinput != -1) This will keep the program going until the user enters -1 to stop it. Now here is how I would implement the program but have a go yourself to see what solution you would come up with first. Remember my program is not the final and ultimate answer, it may not even suit your ideas. So it's best that you code your own version first and then make a comparison with mine.



//this is a simple program that takes a user input for a gross wage
//it calculates the appropriate tax on that wage and then sends out 
//the nett wage after the calculations have finished.

//this is the preprocessor directive to include iostream
#include <iostream.h>

//this is the main function it takes no parameters
void main(void)
{
	//these are the variable declarations. I have initialised tax1
	//to 13.5 because it will always be this if the wage is greater
	//than 150, read on to see why
	int wage=0, tax1=13.5, tax2=0, nett=0;
	
	//this is the sentinel loop that keeps the program running
	//until -1 is entered
	while(wage != -1)
	{
		//prompt user for wage and read it in
		cout<<"Enter the wage(-1 to exit): ";
		cin>>wage;

		if(wage == -1)
			cout<<"Shutting Down....."<<endl;
		
		//perform the necessary checks on the wage value
		//and calculate the tax as required		
		else if(wage <= 100)
			cout<<"The nett wage is: "<<wage<<endl;

		else if(wage > 150)
		{
			tax2 = (wage - 150) * .37;
			//we don't need to calculate tax1 because we already know it
			nett = wage - tax1 - tax2;
			cout<<"The nett wage is: "<<nett<<endl;
		}

		else if(wage <= 150)
		{
			//tax1 is no longer 13.5 so we need to recalculate it
			tax1 = (wage - 100) * .27;
			nett = wage - tax1;
			cout<<"The nett wage is: "<<nett<<endl;
		}
		
		//if the wage meets none of the conditions then it is
		//a minus value not -1 or the loop would have caught it		
		else
			cout<<"This is not a valid wage!"<<endl;
	}
}


And there we have it a simple program that will calculate a nett wage and has error checking to catch an invalid wage (a minus value). It also keeps the program running until the user decides to exit it. How did your version compare to this?


Previous Index Next


© Redsetter 2000