0

I am writing a program in c++ that calculates overtime hours worked. For some reason the calculation of overtime_hours_worked is way off. I tried initializing variable and negation. I inputted 48 hours for hours_worked_inweek and by my formula I should be getting 8 as the answer. Instead I am getting -40. I am currently learning.

#include<iostream>

using namespace std;

int main(){
    
    int hours_worked_inweek=0;
    int dependents;
    int union_dues;
    double federal_tax_witholding;
    double state_tax_witholding;
    double social_security_tax_witholding;
    double gross_pay;
    double net_pay;
    double hourly_rate;
    double overtime_rate;
    int overtime_hours_worked=0;
    
    overtime_rate = 1.5*overtime_hours_worked;
    hourly_rate = 16.76;
    union_dues = 10;
    overtime_hours_worked = hours_worked_inweek-40;
    
    cout << " How many hours have you worked in a week ? " << endl;
    cin >> hours_worked_inweek;
    cout << "Wow ! You worked "<<hours_worked_inweek<<" this week"<<endl;
    
    if (hours_worked_inweek>40){
        cout<<"It looks like you also worked some overtime hours this week! Your Overtime hours are : "<<endl;
        cout<<hours_worked_inweek<< "-" << "40" << " Which is equal to " << overtime_hours_worked<<endl;
    }
    else{
        cout<< " You did not work any overtime hours this week !"<<endl;
    }
    
    cout<< "How many dependents do you have : "<<endl;
    cin>>dependents;

    return 0;
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 2
    `overtime_hours_worked = hours_worked_inweek-40;` needs to be after `cin >> hours_worked_inweek;` As you have it now you're subtracting 40 from 0, so -40. – Retired Ninja Aug 25 '21 at 21:01
  • 1
    Lot of variables in there defined but not initialized. Make sure they are all set to something useful before you use them, but prefer to not define them until you have their initial value where possible. Not having uninitialized variables lying around prevents a lot of trivial bugs – user4581301 Aug 25 '21 at 21:02
  • 1
    When you ask a question on Stack Overflow, you should reduce your program to a [mre]. Don't post the entire code; post only what is needed to demonstrate the issue. In this case, you need only 6 lines, don't you? (`int hours_worked_inweek=0;` `int overtime_hours_worked=0;` `overtime_hours_worked = hours_worked_inweek-40;` `cout << " How many hours have you worked in a week ? " << endl;` `cin >> hours_worked_inweek;` `cout< – JaMiT Aug 25 '21 at 21:21
  • Do these answer your question? [How to output a double that is the value of a number multiplied by another variable in C++?](https://stackoverflow.com/questions/46125996) OR [Easy calculator](https://stackoverflow.com/questions/55042810) OR [What is the length of my array?](https://stackoverflow.com/questions/29859261) OR [Why can this code not return the bpEffect variable with the value 505.5?](https://stackoverflow.com/questions/40198524) *(I don't see a high-rated "master" question, though.)* – JaMiT Aug 25 '21 at 21:29
  • 2
    I would throw in a comment about addressing compiler warnings, but my guess is you already did so. I notice that the only two variables you initialize are the ones used in formulas. Did your compiler try to warn you that your setup is wrong (using uninitialized variables), but you silenced it by initializing to zero instead of fixing the real error? – JaMiT Aug 25 '21 at 21:35
  • No, I just thought because I did not initialize, the program gave a value for 0 so 0-40=-40 – Intisar Ratul Aug 26 '21 at 22:09

2 Answers2

1

You cannot do it in this order:

overtime_hours_worked = hours_worked_inweek-40;

cout << " How many hours have you worked in a week ? " << endl;
cin >> hours_worked_inweek;

This is executed first: overtime_hours_worked = hours_worked_inweek-40; and uses the value for hours_worked_inweek at that time.

Simply switch it around:

cout << " How many hours have you worked in a week ? " << endl;
cin >> hours_worked_inweek;

overtime_hours_worked = hours_worked_inweek-40;
Jeffrey
  • 11,063
  • 1
  • 21
  • 42
  • 1
    Side note: If you define variables close to where you start using them it makes it easier to spot mistakes like this. Mind you it also helps to understand that C++ doesn't work like a spreadsheet. Results not recomputed when variables used in a computation are subsequently changed. – user4581301 Aug 25 '21 at 21:12
1

Look at this line overtime_hours_worked = hours_worked_inweek-40; And stop for a moment. at that moment of time variable hours_worked_inweek is equal to 0, as you initialized it in the very first line. But you want to subtract 40 from user's input, so simply move this line after you get an input here cin >> hours_worked_inweek;

HappyLemon
  • 78
  • 7
  • 1
    I think many new people believe the assignment establishes a mathematical relationship where you can put the formulas at the beginning of your code and any time you ask for the value any formulas related to that variable are reevaluated. `c++` does not work that way. Instead the assignment happens in the order that it is in the code and there is no reevaluation. – drescherjm Aug 25 '21 at 21:13
  • Yeah, maybe, but no language operates like that, does it? It reminds me of a story where someone wanted to code on pure english :) – HappyLemon Aug 25 '21 at 21:20
  • 1
    There are languages that do it, but they're rare if you don't count spreadsheets as programming. Smurfed if I can remember the name of this. Reactive? – user4581301 Aug 25 '21 at 21:31
  • @HappyLemon *"but no language operates like that, does it?"* -- this is a nicely rational approach, but unfortunately humans tend to be irrational. Arguing that a certain misunderstanding should not happen will not cause it to not happen. ;) – JaMiT Aug 25 '21 at 21:40
  • Confirmed: [reactive](https://en.wikipedia.org/wiki/Reactive_programming) – user4581301 Aug 25 '21 at 22:56