Reputation: 37
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;
}
Upvotes: 0
Views: 140
Reputation: 68
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;
Upvotes: 1
Reputation: 11430
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;
Upvotes: 1