Next: Example Program: Pythagorean Triples
Up: The if-else Statement
Previous: Examples of if-else statements
In Lesson 5 an algorithm was developed to calculate wages depending on hours worked and on whether any overtime had been worked. This can now be written in C++. The program is listed below:
// IEA 1996 // Program to evaluate a wage #include <iostream.h> void main() { const float limit = 40.0, overtime_factor = 1.5; float hourly_rate, // hourly rate of pay hours_worked, // hours worked wage; // final wage // Enter hours worked and hourly rate cout << "Enter hours worked: "; cin >> hours_worked; cout << "Enter hourly_rate: "; cin >> hourly_rate; // calculate wage if (hours_worked <= limit) wage = hours_worked * hourly_rate; else wage = (limit + (hours_worked - limit) * overtime_factor) * hourly_rate; // Output wage cout << "Wage for " << hours_worked << " hours at " << hourly_rate << " is " << wage << endl; }
Download program.Note that this program contains the minimal amount of comment that a program should contain. Comments have been used to:
Also note how constants have been used for the number of hours at which the overtime weighting factor applies and the weighting factor itself. Hence if subsequent negotiations change these quantities the program is easily changed.