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

//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
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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

//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.