HW Help Can't Figure it out

This is a homework assignment for my class, and the professor didn't explain switch and two-way statements very well. Included is what I have so far, so please help out if you can.
1. The job classification of each employee determines the employee's hourly rate. The classifications are as follows:(I included what it's supposed to be in my program) (use a switch statement)
When an invalid Classification Type is entered, the hourly rate from classification type 1 is to be used to calculate the employee's wages and an appropriate message is to be printed after the calculated output

2. Calculate regular pay: (use a two-way if statement)
♦ When an employee works the regular number of hours:
regular_hours * hourly_rate
♦ When an employee works more than the regular number of hours: regular_hours * hourly_rate
♦ When an employee works less than the regular number of hours:
hours_worked * hourly_rate
3. Calculate overtime pay: (use a two-way if statement)
An employee is paid 1.5 times their regular hourly rate for all hours worked over the regular hours, otherwise, the overtime rate is 0.
♦ When an employee works more than the regular number of hours: (hours_worked - regular_hours) * 1.5 * hourly_rate
4. An appropriate message is to be generated for any employee who works less than 40 hours or more than 60 hours. Examples: Inadequate number of hours worked! or Excessive number of hours Worked! This message is to be printed after the calculated output. (Use one-way if statements)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
  //*********************** Preprocessor Directives ********************* 
#include <fstream> 
#include <iostream> 
#include <iomanip> 
#include <string> 
#include <cmath> 
#include <stdlib.h> 
 
using namespace std; 
 
//******************************* main ******************************** 
int main() 
{                                      										  
switch (job_class)															// selector variable/expression: job_class
	case 1:    5.50
	 			break;                        
	case 2:    6.00  
				 break;                      
	case 3:    7.00    
				 break;                    
	case 4:    9.00    
				 break;                    
	case 5:    12.00  
				 break; 
																			//Declaration Section 
int           hours_worked;   
			  job_class,                 
			  total_hours, 			// hours_worked - regular_hours
			  regular_hours,     	// regular_hours   *   hourly_rate                 
			  more_hours,           // regular_hours    *    hourly_rate                
			  less_hours,           // hours_worked   *    hourly_rate                             
			  overtime_hours,      // (hours_worked - regular_hours)  *  1.5  *  hourly_rate                                          
		  	  total_pay,           // regular_pay + overtime_pay     
		char  name[30],        	   // employee name (first and last)                  
 			  id_number[6];        // I.D Number                                                      
																			// input section   
			  system ("cls");      
			  cout <<  "Enter Customer Name:  ";   
			  cin >> ws;   
			  cin.getline(name,(sizeof(name)-1));   
			  cout <<  "Enter I.D. Number:  ";   
			  cin >> ws;   
			  cin.getline(id_number,(sizeof(id_number)-1));   
			  cout <<  "Enter Hours Worked:  ";   
			  cin >>  hours_worked;   
			  cout <<  "Enter the Daily Cost:  ";   
			  cin >>  job_class;   
                                                   
 																			//process section  
			  total_hours = hours_worked - 40;
			  regular_pay = regular_hours   *   hourly_rate;   
			  more_pay = regular_hours    *    hourly_rate;   
			  less_pay =  hours_worked   *    hourly_rate; 
			  overtime_pay = (hours_worked - regular_hours)  *  1.5  *  hourly_rate; 
			  total_pay = regular_pay + overtime_pay;                                                  
																			// output section   
			  system("cls");                   
			  cout << setiosflags(ios::showpoint | ios::fixed) << setprecision (2) << fixed;   
			  cout << "WorkHard Corporation"  << endl;   cout << "~~~~~~~~~~~~~~~~~~~~~~~~~"  << endl  << endl;   
			  cout << "Employee Name        " << name << endl  << endl;  
 			  cout << "I.D. Number    " << id_number << endl << endl;   
			  cout << "Job Classification       " << job_class << endl << endl;   
			  cout << "Hourly Rate   "  << switch (job_class) << endl;  
 			  cout << "Total Hours Worked  " << hours_worked << endl;   
			  cout << "Overtime Hours " << num_days << endl << endl;   
			  cout << "Regular Pay      " << regular_hours << endl;   
			  cout << "Overtime Pay        " << overtime_pay << endl;   
			  cout << "Total Earnings     " << total_pay << endl;      
			  cout << endl << endl;         //provides blank line before pause display message  
			  system("pause");  
			  return 0; 
}
Last edited on
Hello kcattgirl,

While I load up the program, did you compile it and what errors did you receive? At first glance I can see errors right off.

If yo do have error messages that you do not understand post the entire error message.

Andy
Hello kcattgirl,

You may want to take time to read http://www.cplusplus.com/doc/tutorial/control/#switch

And this link shows an example of code: https://www.programiz.com/cpp-programming/switch-case

You also have a problem of where your variables are defined. Some people will say to define a variable just before it is first used. My preference and what I first learned is to define all variables at the beginning of a function. This way I know where they are at and where to find them.

Either way lines 26 - 35 are not correct.

Hope that helps,

Andy
I still don't understand what to do with the numbers; from the link provided, it doesn't give an example like the one I'm dealing with. I don't understand if I have to write "If the employee works 1 hour it equals 5.50" and so forth. And how are my lines 26-35 incorrect?
Hello kcattgirl,

Based on your code my question is: Do you know how to do payroll?

The links are to give you an idea and show you the correct way to code a switch. From the basic information you will need to change the variable names to make it work.

For lines 26 - 35, sorry I do not know how to make the line numbers match,:
1
2
3
4
5
6
7
8
9
10
11
int hours_worked;  // <--- A properly defined variable

job_class,
	total_hours, 			// hours_worked - regular_hours
	regular_hours,     	// regular_hours   *   hourly_rate                 
	more_hours,           // regular_hours    *    hourly_rate                
	less_hours,           // hours_worked   *    hourly_rate                             
	overtime_hours,      // (hours_worked - regular_hours)  *  1.5  *  hourly_rate                                          
	total_pay,           // regular_pay + overtime_pay     
	char  name[30],        	   // employee name (first and last)                  
	id_number[6];        // I.D Number                                                       

Line 1 as the comment says.

Lines 3 - 9 do not have a a type for these variables. Also on line 9 it ends with a comma when it should be a semi-colon. The compiler will try to put lines 3 - 11 all together because it starts reading at line 3 and does not stop until it reaches the semi-colon at the end of line 11.

What you should have is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int hours_worked{};
int job_class{}, num_days{};
double total_hours{}, 	   // hours_worked - regular_hours
	regular_hours{},       // regular_hours   *   hourly_rate                 
	more_hours{},          // regular_hours    *    hourly_rate                
	less_hours{},          // hours_worked   *    hourly_rate                             
	overtime_hours{},      // (hours_worked - regular_hours)  *  1.5  *  hourly_rate                                          
	total_pay{},           // regular_pay + overtime_pay   
	hourly_rate{},         // <--- Missing these variables.
	regular_pay{},
	more_pay{},
	less_pay{},
	overtime_pay{},
	totalHours{};

	std::string  name,          // employee name (first and last)                  
		id_number;      // I.D Number 

Not sure if you have to use a character array for lines 16 and 17, so I changed it to a "std::string" because it is much easier to work with.

For the switch I have come up with this for a start:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
switch (job_class)  // selector variable/expression: job_class
{  // <--- Missing.
	case 1:
		//5.50 // <--- What is "5.50"? Use PAY_RATE1 etc.
		hourly_rate = PAY_RATE1;
		break;
	case 2:
		//6.00
		break;
	case 3:
		//7.00
		break;
	case 4:
		//9.00
		break;
	case 5:
		//12.00
		break;
	default:        // <--- Need to add per instruction 1 part B.
		hourly_rate = PAY_RATE1;
		errorMsg = true;
		break;
}  // <--- Missing. 

You will see that I used a variable called "PAY_RATE1". This is defined as:
1
2
3
4
5
6
7
constexpr double PAY_RATE1{ 5.50 };
constexpr double PAY_RATE2{ 6.00 };
constexpr double PAY_RATE3{ 7.00 };
constexpr double PAY_RATE4{ 9.00 };
constexpr double PAY_RATE5{ 12.00 };
constexpr double MAX_REGULAR_HOURS{ 40.0 };
constexpr double OVERTIME_RATE{ 1.5 };

All these variables are constants that can not be accidentally changed in the program. If you do the compiler will complain. By placing this at the top of "main" and using these names through out the program you only have one place to make a change instead of having to hunt through the whole program where you could miss something. I can not say yet if this is all that you will need, but it is a good start.

And your input section, which I changed a bit and looks like this:
1
2
3
4
5
6
7
8
9
10
cout << "Enter Customer Name:  ";
//cin >> ws;  // <--- Even with what you had this is not needed.
std::getline(std::cin, name);
cout << "Enter I.D. Number:  ";
//cin >> ws;  // <--- Even with what you had this is not needed.
std::getline(std::cin, id_number);
cout << "Enter Hours Worked:  ";
cin >> totalHours;
cout << "Enter Job Class:  ";  // <--- Does not make any sence to me.
cin >> job_class;

should be followed by the switch once you have a proper value for the variable "job_class".

If there is anything that you do not understand let me know. Try to keep it to one or two points at a time so I do not miss anything.

Hope that helps,

Andy

To try and simplify it for myself, this is what I have right now. For the declaration section, just having "total_hours," has never been an issue so I don't understand why I would have to add the "{}". The switch section is supposed to signify the hourly rates that would be determined in the output section. For example, it would take the hours worked then multiply by 5.50 if the employee has a job classification of 1. For the input section, it doesn't seem like there is anything wrong just from what my professor has showed with previous assignments. This is what I have so far, hopefully I did it properly but I just have a lot of errors currently.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//*********************** Preprocessor Directives ********************* 
#include <fstream> 
#include <iostream> 
#include <iomanip> 
#include <string> 
#include <cmath> 
#include <stdlib.h> 
 
using namespace std; 
 
//******************************* main ******************************** 
int main() 
constexpr double PAY_RATE1{ 5.50 };
constexpr double PAY_RATE2{ 6.00 };
constexpr double PAY_RATE3{ 7.00 };
constexpr double PAY_RATE4{ 9.00 };
constexpr double PAY_RATE5{ 12.00 };
constexpr double MAX_REGULAR_HOURS{ 40.0 };
constexpr double OVERTIME_RATE{ 1.5 };
{                                      										  
switch (job_class)															// selector variable/expression: job_class
{  // 
	case 1:
		//5.50 // 
		hourly_rate = PAY_RATE1;
		break;
	case 2:
		//6.00 //
		hourly_rate = PAY_RATE2;
		break;
	case 3:
		//7.00 //
		hourly_rate = PAY_RATE3;
		break;
	case 4:
		//9.00 //
		hourly_rate = PAY_RATE4;
		break;
	case 5:
		//12.00 //
		hourly_rate = PAY_RATE5;
		break;
	default:        // 
		hourly_rate = PAY_RATE1;
		errorMsg = true;
		break;
}  //
																			//Declaration Section 
int           hours_worked;  // 
			  job_class,                 
			  total_hours, 			// hours_worked - regular_hours
			  regular_pay,     	// regular_hours   *   hourly_rate                 
			  more_pay,           // regular_hours    *    hourly_rate                
			  less_pay,           // hours_worked   *    hourly_rate                             
			  overtime_pay,      // (hours_worked - regular_hours)  *  1.5  *  hourly_rate                                          
		  	  total_pay           // regular_pay + overtime_pay     
		char  name[30];        	   // employee name (first and last)                  
 			  id_number[6];        // I.D Number                                                      
																			// input section   
			  system ("cls");      
			  cout <<  "Enter Customer Name:  ";   
			  cin >> ws;   
			  cin.getline(name,(sizeof(name)-1));   
			  cout <<  "Enter I.D. Number:  ";   
			  cin >> ws;   
			  cin.getline(id_number,(sizeof(id_number)-1));   
			  cout <<  "Enter Hours Worked:  ";   
			  cin >>  hours_worked;   
			  cout <<  "Enter the Daily Cost:  ";   
			  cin >>  job_class;   
                                                   
 																			//process section  
			  total_hours = hours_worked - 40;
			  regular_pay = regular_hours   *   hourly_rate;   
			  more_pay = regular_hours    *    hourly_rate;   
			  less_pay =  hours_worked   *    hourly_rate; 
			  overtime_pay = (hours_worked - regular_hours)  *  1.5  *  hourly_rate; 
			  total_pay = regular_pay + overtime_pay;                                                  
																			// output section   
			  system("cls");                   
			  cout << setiosflags(ios::showpoint | ios::fixed) << setprecision (2) << fixed;   
			  cout << "WorkHard Corporation"  << endl;   cout << "~~~~~~~~~~~~~~~~~~~~~~~~~"  << endl  << endl;   
			  cout << "Employee Name        " << name << endl  << endl;  
 			  cout << "I.D. Number    " << id_number << endl << endl;   
			  cout << "Job Classification       " << job_class << endl << endl;   
			  cout << "Hourly Rate   "  << switch (job_class) << endl;  
 			  cout << "Total Hours Worked  " << hours_worked << endl;   
			  cout << "Overtime Hours " << total_hours << endl << endl;   
			  cout << "Regular Pay      " << regular_hours << endl;   
			  cout << "Overtime Pay        " << overtime_pay << endl;   
			  cout << "Total Earnings     " << total_pay << endl;      
			  cout << endl << endl;         //provides blank line before pause display message  
			  system("pause");  
			  return 0; 
}
Last edited on
job class does not exist, but you switch off it. You can NOT use a variable before (above, when talking one file of code) the compiler has seen it in c++.

main does not start (open { ) so you have a weird main followed by constants.
its either of the form
main()
{
const
..code
}
or
const
main()
{
code
}

Hello kcattgirl,

jonnin has made some good points, but I think I should start with this first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//  Section 1.
// Header files.
#include <iostream>
#include <limits>

// Section 2.
// Prototypes.

//Section 3.
// Global variables.  Not advisable to use global variables.

// Section 4.
int main()
{  // <--- Start of main block.

	// Section 5.
	// Declare Variables.

	// Section 6.
	// Main Code.

	std::cout << "\n Hello World" << std::endl;


	// Section 7.
	// <--- Used mostly for testing in Debug mode. Removed if compiled for release.
	// <--- Used to keep the console window open in Visual Studio Debug mode.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;
}  // <--- End of main block.

// Section 8.
// Functions.

/*
Section 1. Your header files that the program will need.

Section 2. For future reference.

Section 3. This is where you would put global variables. Although it is best to avoid a regular variable here as the whole program will have access to it and it canbe changed anywhere in the program. A variable defined as a constant is OK because yo may want the whole file to see this variable.

Section 4. Is the main function. Every program needs only oneand this is where the program will start execution.

Section 5. Some will say to define your variables just before they are needed, but I find this makes it hard to have to search through the whole program to find them. When I started in C we learned to put all the variables at the beginning of a function and this something I am just use to. Whichever way you choose just be consistent in what you do.

Section 6. This is where the code of "main" starts.

section 7. This is something I use to keep the console window open so I can have time to read anything that is last displayed before the window closes because the program ends. You may want to use this or not it is up to you.

Section 8. Not something you need to worry about right now. Some choose to put this section above "main". I like to make "main" the first function and put anything else after "main" or in its own file.
*/


Yes I purposely left out the line using namespace std; as it WILL get you in trouble some day. Now is the time to learn how to qualify what is in the standard name space and what is in the standard name space while it is easier and slower.

Line 14 and 34 define the block of main. This is where all your work goes. Not above, below or in-between:
1
2
int main()
{
as yo did with the constant variables.

Looking at your last code you have:
1
2
3
4
5
6
7
8
9
int main() 
constexpr double PAY_RATE1{ 5.50 };
constexpr double PAY_RATE2{ 6.00 };
constexpr double PAY_RATE3{ 7.00 };
constexpr double PAY_RATE4{ 9.00 };
constexpr double PAY_RATE5{ 12.00 };
constexpr double MAX_REGULAR_HOURS{ 40.0 };
constexpr double OVERTIME_RATE{ 1.5 };
{  // <--- Opening brace of main. 

Lines 2 - 8 need to come after line 9.

As jonnin is saying the switch condition is based on the variable "job_class" which has not yet been defined as the compiler sees it. When I tried to compile your last code I received 83 errors and a good portion of those are because a variable is not defined before it is used or you did not define some of your variables properly. The second part of this switch is that it is coming to soon in the program. I needs to come after you input a value for "job_class".

This bit of code is still not right:
1
2
3
4
5
6
7
8
9
10
11
12
int hours_worked;  // 

job_class,
total_hours, 			// hours_worked - regular_hours
regular_pay,     	// regular_hours   *   hourly_rate                 
more_pay,           // regular_hours    *    hourly_rate                
less_pay,           // hours_worked   *    hourly_rate                             
overtime_pay,      // (hours_worked - regular_hours)  *  1.5  *  hourly_rate                                          
total_pay           // regular_pay + overtime_pay 

char  name[30];        	   // employee name (first and last)                  
id_number[6];        // I.D Number                                                       

Line 1 is OK and properly defined.

Lines 3 - 9 do not have a type and line 9 is missing the semi-colon at the end. Line 12 should end with a comma or line 13 should start with a type.

Based on what I see this is what I would start with:
1
2
3
4
5
6
7
8
9
10
11
12
13
int job_class{};  // <--- Because the switch need an "int"

// <--- As a start these variables would work better as a double.
double hours_worked{},
	total_hours{}, 			// hours_worked - regular_hours
	regular_pay{},     	// regular_hours   *   hourly_rate                 
	more_pay{},           // regular_hours    *    hourly_rate                
	less_pay{},           // hours_worked   *    hourly_rate                             
	overtime_pay{},      // (hours_worked - regular_hours)  *  1.5  *  hourly_rate                                          
	total_pay{};           // regular_pay + overtime_pay 

char  name[30]{},        	   // employee name (first and last)                  
    id_number[6]{};        // I.D Number                                                       

Notice in lines 1 and 4 I switched the variable names.

For lines 12 and 13 I would suggest using a "std::string" unless you have to use the character array. Since you have included the header file "string" you should use it.

You are working with a program that used time and money. It would make thing easier if you make most of your variables "double"s as doing math with "int"s and "double"s can lead to a wrong answer. until you learn how to deal with "int"s and "double"s in a calculation it is best to stick with "double"s since you are dealing with money.

Almost forgot you have no need right now for the header file "cmath". This is just adding more code to the program that you do not need.

I am curious why you think you need the lines cin >> ws; in your input section?

Hope that helps,

Andy
technically money uses integers as 1 = 1 penny ($USD terms) to avoid any floating point problems, but its irrelevant in practice problems at this level.
I was taught to put "cin >> ws;" from my professor and he never corrected me for it so I have just continued to use it. I still have 33 error messages and I still don't understand why the variables are coming up as not defined.
There is a semicolon at the end of line 26 where a comma was intended:
26
27
28
29
30
int           hours_worked;   
			  job_class,                 
			  total_hours, 			// hours_worked - regular_hours
			  regular_hours,
  // ... 
Hello kcattgirl,

Based on the last code you posted line 20 should be put between lines 12 and 13. for lines 49 - 58 I showed you that in my last post.

The line cin >> ws; will skip any leading white space in an input line. So unless you or a user likes pressing "tab" or the space bar before actually typing something on the keyboard there is not much use for this with "cin". This is better used when the input comes from a file, but not every time. Not being there in the class I do not know why your professor told you this except maybe to reach you something that is incomplete.

For your program I believe the use of cin >> ws; is not necessary. When I commented it out I did not see any difference. And I did not see any difference when I left it in.

I am trying to get you to learn what you are doing wrong, which is better for you, than to just showing you the code the way it should be. Using this forum is a slow process and I would like to find a better way.

Earlier I ask if you had to use a character array or if you could use a "std::string". I never did get an answer.

Many parts of your program are good just in the wrong place or wrong order.

Hope that helps,

Andy
@jonnin,

Thank you I did not realize that. Now I will have to work on using a different way.

Andy
I'm a very beginner at programming, I don't really understand what the different character array would be. I'm just trying to get through my class without failing and was hoping someone would be able to show how to fix my program. I'm just using past knowledge from the other programs I have had to make and don't know any differently. I'm not very good at programming, which is why I decided to seek help outside of my programming professor since he hasn't been helpful thus far. Any tips and answers would be appreciated and I have appreciated your help immensely. "constexpr double" comes up as an error and says it does not name a type, not sure how to fix that.
Last edited on
You can find very good material and lessons on learncpp.com
Last edited on
Hello kcattgirl,

It sounds like you are using an old compiler that is not up to the C++11 standards.

If "constexpr" (C++11 on) does not work just use "const".

Did that sample code I showed you earlier make any sense or help any?

I don't really understand what the different character array would be.

A character array would be defined as: char letters[10]; meaning that each element of the array is one character of type "char" and one byte in size. Now for something completely different int numbers[10]; where each element of the array is one "int" or two bytes in size.

Hope that helps,

Andy
The sample code did not make much sense to me, I'm not trying to be a programmer I'm just trying to pass my class. The rest I have understood however.
Topic archived. No new replies allowed.