-2

When the hours worked is below 40, the result of the total pay will always be 0, Why? I tried rearranging the codes yet it still does the same thing. What am I doing wrong here?

#include <cstdio>
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main()
{
    int skill, ins, hour, ret;
    double rate;
    cout << "Input Skill Level (1, 2, or 3)\n";
    cin >> skill;
    cout << "Input Hours Worked\n";
    cin >> hour;

    if (skill==1 &&hour<40) rate = 17;
    else if (skill==2 && hour<40) rate = 20;
    else if (skill==3 && hour<40) rate = 22;
    else if (skill==1 && hour>40) rate = 25.5;
    else if (skill==2 && hour>40) rate = 30;
    else if (skill==3 && hour>40) rate = 33;
    int pay = hour * rate;
    int over = 0.5 * pay;
    if (skill==1 &&hour<40) over = 0;
    else if (skill==2 && hour<40) over = 0;
    else if (skill==3 &&hour<40) over = 0;
    int gross = pay + over;

    switch (skill)
    {
        case 1: 
        {
            if (hour>40) 
            {
                cout << "You are Skill Level 1 and your regular pay is " <<pay << " and your overtime pay is "<< over<< " and your total pay is " << gross <<endl;
            }
            else if(hour<40)
            {
                cout << "You are Skill Level 1 and your regular pay is " <<gross << endl;
            }
            break;
        }
        case 2: 
        {
            if (hour>40) 
            {
                cout << "You are Skill Level 2 and your regular pay is " <<pay << " and your overtime pay is "<< over<< " and your total pay is " << gross <<endl;
            }
            else if(hour<40)
            {
                cout << "You are Skill Level 2 and your regular pay is " <<gross << endl;
            }
            break;
        }
        case 3: 
        {
            if (hour>40) 
            {
                cout << "You are Skill Level 3 and your regular pay is " <<pay << " and your overtime pay is "<< over<< " and your total pay is " << gross<< endl;
            }
            else if(hour<40)
            {
                cout << "You are Skill Level 3 and your regular pay is " <<gross << endl;
            }
            break;
        }
        default: cout << "Invalid Input" << endl; break;
    }
    if (skill==2 || skill==3) cout << "Select one of the following Insurance Options \n1 for Medical Insurance \n2 for Dental Insurance\n3 for Long-term Disability Insurance\n";
    else cout << "Thank you for using our program\n";
    cin >> ins;

    switch(ins)
    {
        case 1: cout << gross - 32<< " is your final pay\n";break;
        case 2: cout << gross - 20<< " is your final pay\n";break;
        case 3: cout << gross - 10<< " is your final pay\n";break;
        default: cout << "Invalid Input\n";
    }
    if (skill==3) cout << "Do you want to take our Retirement Plan? \n1 for Yes\n2 for No\n";
    else cout << "Thank you for using out program\n";
    cin >> ret;
    if (ret==1) cout << "You have took our Retirement Plan and your Final Pay becomes "<< 0.97 * gross << endl;
    else if (ret==2) cout << "You did not took our Retirement Plan and your Final Pay remains the same at " << gross << endl;
    else cout << "Invalid Input\n";

    cout << "Thank you for using our program, Hope you enjoy it and May You Have A Good Day!";
}
waka
  • 3,362
  • 9
  • 35
  • 54
  • 8
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/) – NathanOliver Oct 03 '17 at 15:31
  • 1
    I'm betting it's a truncation issue between floating point and integer types. – Fred Larson Oct 03 '17 at 15:32
  • 2
    Please format your code, for example like the samples in your C++ textbook. As it stands here it is unreadable. – Jabberwocky Oct 03 '17 at 15:34
  • 1
    Didn't you get compiler warnings on lines `int pay = hour * rate;` and `int over = 0.5 * pay;`? May not be the cause though... – Jabberwocky Oct 03 '17 at 15:36
  • 1
    [`using namespace std;` is a bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice), never use it. – tambre Oct 03 '17 at 15:37
  • 2
    ...and what is your actual output vs. your expected output for what input? – Jabberwocky Oct 03 '17 at 15:38
  • @MichaelWalz: I got no warnings on the OP's code using `-Wall -Wextra`. – Fred Larson Oct 03 '17 at 15:39
  • I also haven't duplicated the problem. No zeroes, at least. – Fred Larson Oct 03 '17 at 15:41
  • Using "if (hour>40)" and "else if(hour<40)" excludes the case when the hours equal 40 exactly. If I understand the logic correctly, the later case should just be "else", as anything not greater than 40 will not have overtime. – Stephen Oct 03 '17 at 17:29

1 Answers1

0

I can't quite reproduce the problem you've asked, however I can see that there's a bug in your code if the number of hours is exactly 40. Assuming that's what you meant, this section

if (skill==1 &&hour<40) rate = 17;
else if (skill==2 && hour<40) rate = 20;
else if (skill==3 && hour<40) rate = 22;
else if (skill==1 && hour>40) rate = 25.5;
else if (skill==2 && hour>40) rate = 30;
else if (skill==3 && hour>40) rate = 33;

leaves rate uninitialized. This will cause the result of the multipilcation of rate by the number of hours to be undefined, and the exact behaviour you observe will depend on the compiler you're using.

Steve
  • 1,747
  • 10
  • 18
  • 1
    "_Depending on the complier you're using, this will have unspecified behaviour when multiplying rate by the number of hours._" Is not correct. It will, always, be, **undefined** behavior, as it is what is mandated by the standard. Different compilers, versions of the same compiler, etc., might produce different results, but behavior itself, is, always, undefined. – Algirdas Preidžius Oct 03 '17 at 16:03
  • Sorry, yes, I did mean that the exhibit behaviour would depend on the compiler. I'll rephrase. – Steve Oct 03 '17 at 16:04