Here is the question:

DECIDE YOURSELF
The SuperShop have seven sales assistants. They are paid R55.70 per hour and 1.5 times for overtime. Each sales
assistant is also paid 2% commission on his or her sales. The manager of SuperShop wants a C++ program to print
the weekly payslips for the sales assistants. For each sales assistant the program has to:
- input the number of hours the assistant worked during the past week, as well as the total of the sales he or she made
during this time. A normal workweek consists of 40 hours. According to labour law sales assistants are not allowed
to work more than 50 hours per week. The program has to validate the number of hours worked – if it is more than
50, the user should repeatedly be prompted to enter all three input values again until the number of hours worked is
less than 50.
- calculate the pay and print the payslip for the assistant.


Here is how I've done it:

#include <iostream>

using namespace std;

int main()
{

  int NormalHours, OverTimehours, TotalHours;
  float Sales, Commission, TotalPay;
  float NormalHoursRate = 55.70;
  float OvertimeRate = 55.70 * 1.5;
  float CommissionRate = 0.02;

  cout << "Please enter normal hours workeked this week: ";
  cin >> NormalHours;
  cout << "Please enter overtime hours worked this week: ";
  cin >> OverTimehours;
  cout << "Please enter total sales for the week: " << "R";
  cin >> Sales;
  cout << endl;

  TotalHours = NormalHours + OverTimehours;

  if ( TotalHours < 50){
    Commission = Sales * CommissionRate;
   TotalPay = (NormalHoursRate * NormalHours) + (OvertimeRate * OverTimehours )+ Commission;

   cout << "The sales assistance salary is: " <<"R " << TotalPay << "\n";
   cout << "                                 _______" << endl;}

else

 do {
 cout << "Total hours worked cannot be more than 50 hours, please renter the valid hours: " << endl;
  cout << "Please enter normal hours workeked this week: ";
  cin >> NormalHours;
  cout << "Please enter overtime hours worked this week: ";
  cin >> OverTimehours;
  cout << "Please enter total sales for the week: " << "R";
  cin >> Sales;
  cout << endl;

   Commission = Sales * CommissionRate;
   TotalPay = (NormalHoursRate * NormalHours) + (OvertimeRate * OverTimehours )+ Commission;

   cout << "The sales assistance salary is: " <<"R " << TotalPay << "\n";
   cout << "                                 _______" << endl;}

 while (TotalHours < 50);





   return 0;
}

The problem is if the user enters the wrong values more than once the salary is calculated anyway. Please assist.

simple put the code fragment

Commission = Sales * CommissionRate;
   TotalPay = (NormalHoursRate * NormalHours) + (OvertimeRate * OverTimehours )+ Commission;

   cout << "The sales assistance salary is: " <<"R " << TotalPay << "\n";
   cout << "                                 _______" << endl;

out side the loop

Please read through your program and think about what is happening logically. Do not assume anything. You should be able to come to conclusion fairly quickly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.