using If statements

Hey there guys, im new to the forum and new to the c++ language or any programming language for that matter. Was wondering if you could help me out.

Writing this program here to display payLump if the hours input are greater than 40.

I was also wondering why i have to place the 3 braces at the bottom of the code block? The book im using has an example without having those 3 at the bottom of the code block. Thanks in advance.

Here is what i have so far:

#include <iostream>
using namespace std;

int main ()

{
double hours;
double payLump;
double payHrly;

cout << "Please advise hours worked this week: ";
cin >> hours;

if (hours > 40)
{
payLump = 320.00 + (12 * hours - 40);
cout << "Your pay totals to be: " << payLump << endl;
}

else
{
payHrly = 8.00 * hours;
cout << "Your pay totals to be: " << payHrly << endl;
{
system ("pause");

return 0;

}
}
}

You need to use them to ensure the correct matching takes place for your if statement.
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
#include <iostream>
using namespace std;

int main ()

{
double hours;
double payLump;
double payHrly;

cout << "Please advise hours worked this week: ";
cin >> hours;

if (hours > 40)
{ 
payLump = 320.00 + (12 * hours - 40);
cout << "Your pay totals to be: " << payLump << endl;
}

else
{
payHrly = 8.00 * hours;
cout << "Your pay totals to be: " << payHrly << endl;
}                            // For corresponding opening else brace
system ("pause");

return 0;

}         //For int main() 
closed account (z05DSL3A)
Your 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
#include <iostream>
using namespace std;

int main ()
{
    double hours;
    double payLump;
    double payHrly;

    cout << "Please advise hours worked this week: ";
    cin >> hours;

    if (hours > 40)
    { 
        payLump = 320.00 + (12 * hours - 40);
        cout << "Your pay totals to be: " << payLump << endl;
    }
    else
    {
        payHrly = 8.00 * hours;
        cout << "Your pay totals to be: " << payHrly << endl;
        {
            system ("pause");
            return 0;
        }
    }
}


The block, lines 22-25, are in the wrong place, that is why there is the need for the extra braces.

To make sure your hours are calculated proerly you need parenthesis aroulnd the hour calculation:

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
#include <iostream>
using namespace std;

int main ()
{
    double hours;
    double payLump;
    double payHrly;

    cout << "Please advise hours worked this week: ";
    cin >> hours;

    if (hours > 40)
    { 
        payLump = 320.00 + (12 * (hours - 40));
        cout << "Your pay totals to be: " << payLump << endl;
    }
    else
    {
        payHrly = 8.00 * hours;
        cout << "Your pay totals to be: " << payHrly << endl;
    }
    system ("pause");
    return 0;
}


Edit: I should check to see it there is a reply when I've been on the phone mid typing...
Last edited on
Topic archived. No new replies allowed.