Problems using constants in calculations and switch statement?

Hey all, I am having a couple of errors all seeming to involve my variable 'wage_rate'.(Line 74 for some reason?)
Errors are noted in the comments.

Normally I don't use the #define 'const' 'value' format,and I would normally just use doubles instead of floats. This must be the case for this assignment unfortunately...

Here is the full code I wrote, it is an expected earnings program based on an input of weekly hours worked:

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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "stdafx.h"
#include <iostream>



#define overtime 1.50;
#define wage_rate1 8.75;
#define wage_rate2 9.33;
#define wage_rate3 10.00;
#define wage_rate4 11.20;
#define tax_rate1 1.15;
#define tax_rate2 1.20;
#define tax_rate3 1.25;

float wage_rate;
float hours_worked = 0;
float gross_pay, tax_rate, taxes, net_pay;
char selection;

float request_hours() {
	printf("How many hours did you work this week?    ", hours_worked);
	scanf("%f", &hours_worked);
	return hours_worked;

}

float print_income_summary(float hours_worked, float wage_rate) {

	if (hours_worked <= 40.00) {
		gross_pay = (hours_worked * wage_rate);
	}
	else {
		gross_pay = (40.00 * wage_rate) + ((hours_worked - 40.00) * wage_rate * 1.50);		/*Putting the constant overtime here gives error??*/
	}
	

	if (gross_pay < 300.00) {
		tax_rate = tax_rate1;

	}
	else if (300.00 <= gross_pay <= 450.00) {
		tax_rate = tax_rate2;

	}
	else
	{
		tax_rate = tax_rate3;
	}

	taxes = (gross_pay * tax_rate) - gross_pay;

	net_pay = gross_pay + taxes;

	printf("Based on your weekly hours worked, your gross pay is %.2f. endl", gross_pay);
	printf("Based on your gross pay, the tax you will pay is %.2f. endl", taxes);
	printf("Your net pay will then be %.2f. endl", net_pay);

	return 0;


}



int main() {
	
	std::cout <<
		"Here is a menu of available wage rate and menu options:			 "
		"********************************************************************"
		"1) $8.75/hr 2) $9.33/hr											 "
		"3) $10.00 / hr 4) $11.20 / hr                                       "					/*Wage rate redefinition, multiple initialization.*/
		"5) quit                                                             "					/*Error somehow on line 74, init. of wage_rate is skipped by case label, also skipped by default label.*/
		"********************************************************************";
	printf("Enter the number corresponding to the desired wage rate or action:  ", selection);
	scanf("%c", &selection);

	switch (selection) {

	case '1':
		request_hours();
		 float wage_rate = wage_rate1;
		print_income_summary(hours_worked, wage_rate);
		break;
	case '2':
		request_hours();
		 float wage_rate = wage_rate2;
		print_income_summary(hours_worked, wage_rate);
		break;
	case '3':
		request_hours();
		 float wage_rate = wage_rate3;
		print_income_summary(hours_worked, wage_rate);
		break;
	case '4':
		request_hours();
		 float wage_rate = wage_rate4;
		print_income_summary(hours_worked, wage_rate);
		break;
	case '5':
		printf("Quitting the program. ");
	default:
		printf("This is an invalid input!");

	}

					

    return 0;
}


My goal is basically to have the wage rate change depending on the selected 'case'. If this can be done, the other functions I have will be easier to use. I am still fairly new to C++, any advice on how to make this program better is much appreciated! Thanks.
This must be the case for this assignment unfortunately...


Well your prof needs some bar room diplomacy (get him / her on floor, and sit on them until they agree to do it properly)

With the std::cout separate the quoted parts with << , as in :

1
2
3
4
5
6
std::cout << "Here is a menu of available wage rate and menu options:			 "
		 << "********************************************************************"
		<< "1) $8.75/hr 2) $9.33/hr											 "
		<< "3) $10.00 / hr 4) $11.20 / hr                                       "					 
		<< "5) quit                                                             "					l
		<< "********************************************************************";


That code should be in it's own function.

Why are you mixing C++ I/O with C (printf / scanf) ?

If using scanf , check the return value to see if it worked.

Put forward declaration of your functions before main, with the function definitions after main. This is so main is near the top of the file.

Try to avoid global variables, they will bite you in the ass one day :+)

Golden Rule: Always initialise your variables to something. Preferably wait until you have a sensible value to assign to it, then do the declaration and assignment all in 1 line.

Rather than magic numbers like 40.00 , 1.50 , 300, 450 in the code, make them constexpr variables, and use the variable name instead.

constexpr float OrdinaryHours = 40.0f;

Put comments before the line they apply to, not at the end - they make the line too long.

This is wrong:
else if (300.00 <= gross_pay <= 450.00) {

The reason is that 300.00 <= gross_pay evaluates to true or false, so then you have true <= 450.00 , which doesn't make sense.

if (gross_pay >= 300.00 && gross_pay <= 450.00) {
Thanks for the feedback IdeasMan, really helpful information!

All the points you brought up helped clear most of my errors, cheers!
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
#include <iostream>
#include <iomanip>

double request_hours() {

    std::cout << "How many hours did you work this week? " ;
    double hours_worked ;
    std::cin >> hours_worked ;
    return hours_worked;
}

void print_income_summary( double hours_worked, double wage_rate ) {

    double gross_pay = hours_worked * wage_rate ;

    // add overtime (50% extra) for hours_worked > 40
    if( hours_worked > 40.00 ) gross_pay += (hours_worked-40.0) * wage_rate * 0.5 ;

    // may want to create a table here (which maps gross_pay intervals to tax rates)
    double tax_rate = 0.15 ; // minimum tax rate < 300
    if( gross_pay >= 300.00 ) tax_rate += 0.05 ; // 0.20 for [300,450)
    if( gross_pay >= 450.00 ) tax_rate += 0.05 ; // 0.25 for 450+

    const double taxes = gross_pay * tax_rate ;
    const double net_pay = gross_pay - taxes;

    std::cout << std::fixed << std::setprecision(2) // two digits after the decimal pint
              << "\nBased on your weekly hours worked, your gross pay is " <<  gross_pay << '\n'
              << "Based on your gross pay, the tax you will pay is " << taxes << '\n'
              << "Your net pay will then be " << net_pay << '\n' ;
}

int main() {

    const int N = 5 ;
    const double rates[N] = { 0, 8.75, 9.33, 10.00, 11.20 } ; // note: rates[0] is unused

    std::cout << "Here is a menu of available wage rate and menu options:\n"
    "*******************************************************\n" ;

    for( int i = 1 ; i < N ; ++i ) std::cout << i << ") $" << rates[i] << "/hr\n" ;
    std::cout << N << ") Quit\n" ;

    std::cout << "********************************************************\n\n"
    "Enter the number corresponding to the desired wage rate or action: " ;

    int selection ;
    std::cin >> selection ;

    if( selection > 0  && selection < 5 )
    {
        const double wage_rate = rates[selection] ;
        const double hours_worked = request_hours();
        print_income_summary( hours_worked, wage_rate );
    }
}
Topic archived. No new replies allowed.