### Claculation issue

I have written this code and for some reason when the user inputs hours less than 40 i get different answers. I have done this type of code before and can see where i made a syntax error. Any help to see where I went wrong. For hours over 40 the code compiles fine,

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main ()
{
//variable section
double hoursWorked = 0.00; //hours worked during the week.
double hourlyRate = 0.00; //hourly rate of pay
double weeklyWage = 0.00; //weekly wage

cout << "Program to calculate weekly gross pay for hourly employees" << endl << endl;

while(true)
{
//prompt user to enter hours and hourly rate
cout << "Enter hours: ";
cin >> hoursWorked;
cout << "Enter hourly rate: ";
cin >> hourlyRate;

//calculate weekly wage
if (hoursWorked <= 40.0)
{
weeklyWage = hoursWorked * hourlyRate;
}
else (hoursWorked > 40.0);
{
weeklyWage = (hoursWorked - 40) * (hourlyRate * 1.5) + 40 * hourlyRate;
}

//print pay information
cout << fixed << setprecision(2) << showpoint;
cout << "Gross Weekly Wage" << setw(8) << weeklyWage << endl;
cout << endl;
}
return 0;
} //end main
I may be a beginner but I think I can spot your bug. I dont know for others but it helps that the code to be in the source code:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````#include #include #include using namespace std; int main () { //variable section double hoursWorked = 0.00; //hours worked during the week. double hourlyRate = 0.00; //hourly rate of pay double weeklyWage = 0.00; //weekly wage //print heading cout << "Program to calculate weekly gross pay for hourly employees" << endl << endl; while(true) { //prompt user to enter hours and hourly rate cout << "Enter hours: "; cin >> hoursWorked; cout << "Enter hourly rate: "; cin >> hourlyRate; //calculate weekly wage if (hoursWorked <= 40.0) { weeklyWage = hoursWorked * hourlyRate; } else (hoursWorked > 40.0); { weeklyWage = (hoursWorked - 40) * (hourlyRate * 1.5) + 40 * hourlyRate; } //print pay information cout << fixed << setprecision(2) << showpoint; cout << "Gross Weekly Wage" << setw(8) << weeklyWage << endl; cout << endl; } return 0; } //end main ``````

Now, do you get the answer: 0.00?

Or something else?
Last edited on
Delete `(hoursWorked > 40.0);` after else. It's breaking your program logic.
Topic archived. No new replies allowed.