Help with the switch statement. It wont build and I dont know what mistakes I made in the code. Please, help.

The problem is ....

A company pays its employees as managers (who receive a fixed weekly
salary), hourly workers (who receive a fixed hourly wage for up to the first 40 hours they work and “time-and-a-half”—1.5 times their hourly wage—for overtime hours worked), commission workers(who receive $250 plus 5.7 percent of their gross weekly sales), or pieceworkers (who receive a fixed amount of money per item for each of the items they produce—each pieceworker in this company
works on only one type of item). Write a program to compute the weekly pay for each employee. You do not know the number of employees in advance. Each type of employee has its own pay code: Managers have code 1, hourly workers have code 2, commission workers have code 3 and pieceworkers have
code 4. Use a switch to compute each employee’s pay according to that employee’s paycode. Within the switch, prompt the user (i.e., the payroll clerk) to enter the appropriate facts your program needs to calculate each employee’s pay according to that employee’s paycode.

This is the code I wrote. I'm not sure if I'm even on the write path to answer the question. This is my first time programming. The program wont build and I cant figure out why. Please, point out my mistakes.

#include<iostream>
using namespace std;
int main()
{
int worker_type, managers = 1, hourlyworkers = 2, commission = 3, pieceworkers = 4;
cin>>"Please enter employee's paycode ";
switch (worker_type)
{
case 1: cout<<"Managers weekly pay is $500";
break;
case 2: cin>>"Please enter the amount of hours worked";
int amthours;
cin>>"Please enter the hourly rate.";
float hourlyrate, pay;
if (amthours < 40) { pay = hourlyrate * amthours;
}else pay = amthours * (hourlyrate * 1.5);
cout<<"Employee's pay is "<< pay;
break;
case 3: cin>>"Please enter the gross weekly sales made by employee";
float grosssales, pay;
pay = (grosssales * 0.057) + 250;
cout<<"Employee's pay is" << pay;
break;
case 4: cin>>"Please enter the amount the employee is paid per product";
float amtpaid, pay;
cin>>"Please enter the amount of products produced by employee";
int amtproducts;
pay = amtpaid * amtproducts;
cout<<"Employee's pay is "<<pay;
break;
default:
cout<<"No matching worker"<<endl;
}
return 0;
}





Theres a couple of issues here.
this block:
1
2
3
4
5
6
7
8
9
10
switch (worker_type)
{
case 1: cout<<"Managers weekly pay is $500";
break;
case 2: cin>>"Please enter the amount of hours worked";
int amthours;
cin>>"Please enter the hourly rate.";
float hourlyrate, pay;
if (amthours < 40) { pay = hourlyrate * amthours;
}else pay = amthours * (hourlyrate * 1.5);


is the complete switch statement. But you continue the switch cases afterwords. This is probably why it won't build.

You also have a confusion with the switch statement and if statements.

We are going to start with the switch statement.

Each employee type must be included in the switch statement.

do you see how you gave switch the worker_type? When the program executes the switch statement, its going to check the value of worker_type against the cases. when it finds a matching number, it will execute the case. Go it? If not, reread this paragraph.

so lets say we wanted to find the price of some candy. The selections are Nestle crunch, mars, skittles.

the code might look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14

switch(candy_type)
{
    case Nestle:
             output: NestlePrice;
             break;
    case Mars:
             output: MarsPrice;
             break;
    case Skittles:
             output: SkittlesPrice;
             break;
}


In your code you want to do the same thing, but choose each worker_type.

When you've done that post it.
Thank you for your help. I fixed it. And now am debugging it. :) Yay
Topic archived. No new replies allowed.