Trouble with if-else statements

Feb 7, 2013 at 4:22am
I'm making a program to compute hourly pay. If you work less than or equal to 40 hours your pay = 12 per hour. Every hour after 40 hours you earn $17 dollars an hour. My program keeps running the else statement even though they are false. Any help is appreciated, here is my code



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

int main()
{
double hOurs, pAy;

cout << "How many hours have you worked this week?: ";
cin >> hOurs;

if (hOurs <= 40)
{pAy = hOurs*12.0;
cout << "Your weekly pay is $" << pAy << ".";}

else (hOurs > 40);
{pAy = 480+((hOurs-40)*17);
cout << "Your weekly pay is $" << fixed << setprecision(2) << pAy << "." << endl;}


system("pause");
}
Feb 7, 2013 at 4:25am
BTW the program functions but ill get 2 results back even if the hOurs entered in are less than 40
Feb 7, 2013 at 4:29am
Your problem is the following line:
 
else (hOurs > 40);

What is after the else is a no-op. Remove it.
1
2
3
4
5
6
7
8
if (hOurs <= 40)
{ pAy = hOurs*12.0;
   cout << "Your weekly pay is $" << pAy << ".";
}
else 
{ pAy = 480+((hOurs-40)*17);
  cout << "Your weekly pay is $" << fixed << setprecision(2) << pAy << "." << endl;
}

PLEASE USE CODE TAGS (the <> formatting button) when posting code.


Feb 7, 2013 at 4:30am
you should leave that semicolon on else statement
Feb 7, 2013 at 4:32am
Thank you very much.

Sorry about the formatting, that was my first post. Will not happen again
Feb 7, 2013 at 4:32am
Your else isn't supposed to use a condition.

What you're trying to say is that if hours worked is less than 40, calculate pay like this, else calculate your pay like this. You don't need a condition for the else because you either worked 40 hours or you didn't. Change your if statement to:

1
2
3
4
5
6
7
8
if ( hours <= 40 ) {
 pay = hours*12.0;
}
else
{
 pay = 480 + (hours*17);
}


Topic archived. No new replies allowed.