Monday, May 24, 2010

C ++ help please!?

I am stuck with this program which should calculate the overtime (if any) and the gross pay.





it just showing me the gross pay in both, the gross output and overtime output. what am i doing wrong? please help me!


this is my code:





#include %26lt;iostream%26gt;


#include %26lt;iomanip%26gt;





using std::cout;


using std::cin;


using std::endl;


using std::fixed;


using std::setprecision;





int main()


{


//declare variables


double hours = 0.0;


double rate = 0.0;


double gross = 0.0;


double overtime = 0.0;





//enter input


cout %26lt;%26lt; "Enter the hours worked: ";


cin %26gt;%26gt; hours;


cout %26lt;%26lt; "Enter the pay rate: ";


cin %26gt;%26gt; rate;





//calculate overtime pay and gross pay


if (hours %26gt; 40.0 || hours %26lt;= 40.0)


{


overtime = (hours * rate) + overtime;


gross = hours *rate;


} //end if


//display output


cout %26lt;%26lt; "Overtime: " %26lt;%26lt; overtime %26lt;%26lt; endl;


cout %26lt;%26lt; "Gross pay: " %26lt;%26lt; gross %26lt;%26lt; endl;


return 0;


} //end of main function





Can someone please help me! this is the first time i am using C++

C ++ help please!?
You have some logic errors in your if statement.





Should be something like this.








if(hours %26gt; 40){


overtime = (hours-40) * rate * 1.5;


gross = 40 * rate + overtime;


}


else{


gross = rate * hours;


}
Reply:if (hours %26gt; 40.0 || hours %26lt;= 40.0)


{


overtime = (hours * rate) + overtime;


gross = hours *rate;


} //end if


has a problem





You should specify the overtime rate say twice regular rate.


int overtimeFactor = 2;


int overtimeRate;


if(hours %26lt;= 40)


{


gross = hours * rate;


}


else


{


overtimeRate = overtimeFactor * rate;


overtime = (hours - 40) * overtimeRate;


gross = (40 * rate) + overtime;


}


No comments:

Post a Comment