Switch statement

I was up until 4AM last night trying to figure this out for my payroll project. I just can't get it... My dilemma is incorporating the deduction of union dues from the net pay. Union dues are based on union code (A, B, or C). I also can't seem to get the program to accept LETTERS instead of NUMBERS as the union code. I have temporarily changed the coding to numbers.

cliffs:
-Don't understand how to make switch work
-Need to use letters instead of numbers for union code
-Currently program closes when entering a union code

EDIT: Removed some problem code. Inserted breaks in the switch statement. Program now functions as intended. Unresolved issues are how to display union code as a letter. Also when trying to output unionCode at the end of the program I get a string of garbage. Why is this?

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
#include <iostream> //allows program to output data to the screen

//function main begins the payroll calculation program execution
int main()
{
    std::cout << "Welcome to gmsd's Payroll Calculator!\n\n\n";
    
    // variable declarations
    int employeeID; // employee's ID number
    double hoursWorked; // number of hours employee worked
    double regularPay; // if hoursWorked <=40 then regular pay = hoursWorked * payRate
    double overtimePay; // if hoursWorked >40 then over times pay = 1.5 * (hoursWorked - 40) * payRate
    double federalTax; // amount of tax employee pays based on grossPay
    double payRate; // employee's pay rate
    double unionDues; // dues paid based on unionCode
    double grossPay; // gross pay is hoursWorked times payRate
    double netPay; // netPay is grossPay - (grossPay * federalTax)
    double paidTax; // paidTax is grossPay - netPay
    int unionCode[1]; // employee's one character union code
    
    std::cout << "Employee ID: "; //request user input for employeeID
    std::cin >> employeeID;
    
    std::cout << "\n\nHours worked: ";
    std::cin >> hoursWorked;
    if (hoursWorked >= 40) //qualifier for overtime pay
    std::cout << "You will be paid time and a half for hours worked over 40"; // informing user they will receive overtime pay
    else
    std::cout << "You will be paid your regular rate of pay"; // informing user they will be paid the standard rate
    
    std::cout << "\n\nPay rate: ";
    std::cin >> payRate;
    
    std::cout << "\n\nUnion code 1, 2, or 3? ";
    std::cin >> unionCode[1];
    
    regularPay = hoursWorked * payRate;
    overtimePay = 1.5 * (hoursWorked - 40) * payRate;
    
    if (hoursWorked <= 40)
    regularPay = hoursWorked * payRate;
    overtimePay = 0;
    
    if (hoursWorked > 40)
    overtimePay = (1.5 * payRate) * (hoursWorked - 40);
    
    switch (unionCode[1]) {
    case 1: (unionCode[1] = 1);
    unionDues = 25;
    std::cout << "You must pay $25 in union dues.";
    break;
    case 2: (unionCode[1] = 2);
    unionDues = 50;
    break;
    std::cout << "You must pay $50 in union dues.";
    case 3: (unionCode[1] = 3);
    unionDues = 75;
    break;
    std::cout << "You must pay $75 in union dues.";
    }
    
    grossPay = regularPay + overtimePay;
    
    std::cout << "\n\nGross Pay $" << grossPay << "\n";
    
    if (grossPay < 1000)
    federalTax = .1;
    else if (grossPay <= 2000)
    federalTax = .15;
    else (grossPay > 2000);
    federalTax = .25;
    
    netPay = grossPay - (grossPay * federalTax) - unionDues;
    
    paidTax = grossPay - netPay;
    
    std::cout << "\n\nNet Pay $" << netPay << "\n"; // display net pay
    
    std::cout << "\n\nTax paid $" << paidTax << "\n\n";
    
    std::cout << "\nEmployee ID: " << employeeID << " Hours Worked: " << hoursWorked << " Pay Rate: " << payRate << " Union Code: " << unionCode << ""; //Summary of input variables
    
    std::cout << "\n\n\nThank you for using gmsd's Payroll Calculator\n\n\n"; // Termination message
    
    
    system("PAUSE");
    return 0;
}
 //end function main 
Last edited on
To make the switch work, add a break; at the end of each case.

To use letters for union codes, declare unionCode as a char.

Your crash may be due to the fact you keep accessing unionCode[1]. You declared unionCode as int unionCode[1];, which means the only existing element is unionCode[0].

Line 71 : a "else" doesn't take any condition.
Since you do not use brackets, your compiler will consider that (grossPay > 2000); is the code for your "else" and that federalTax = .25; is outside the if/else if/ else code. As a consequence you will always apply a federal tax of 0.25
Topic archived. No new replies allowed.