Help with my do while loop

I am writing an application on the weekly income of a person and now it says that I also need to enter the yearly salary in my code. I am a very novice beginner at the world of C++, or any coding language and thought this site could help with this code. My code is below and any help would be greatly appreciated. Thank You guys.



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
  #include <iostream>
#include <iomanip>
const float over = 1.5; // I chose float because all I need are two spots after decimal point
int main()
{
    using namespace std;
    double hours, hwage;
    float gpay, year;
    do
    {
    cout << "Please enter the number of hours worked in a week: ";
    cin >> hours;
    cout << "Please enter the hourly wage: ";
    cin >> hwage;

    if (hours <= 40)
        gpay = hours * hwage;
    else
        gpay = (40 * hwage) + ((hours - 40) * hwage * over);

    std::cout << std::fixed;
    std::cout << std::setprecision(2); // two numbers after the decimal point
    cout << "Your weekly gross pay is: " << gpay << "\n";

    float total;
    total = gpay * 4;
    cout << "Your total pay for four weeks of work is: " << total << endl;
    while(float year <= 52, year * 4, year++);
    cout << "Your yearly income is: " << year << endl;
    return 0;
}
closed account (48T7M4Gy)
It's not clear what your while loop is supposed to do, so I left it out. Your program only needed a few (suggested) changes:

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
#include <iostream>
#include <iomanip>

using namespace std; // if you use this you don't need std::

const double overtime_rate = 1.5;

int main()
{
    double hours = 0, hwage = 0, gpay = 0; // use double & initialize variables
    
    cout << "Please enter the number of hours worked in a week: ";
    cin >> hours;
    
    cout << "Please enter the hourly wage: ";
    cin >> hwage;
    
    if (hours <= 40)
        gpay = hours * hwage;
    else
        gpay = (40 * hwage) + ((hours - 40) * hwage * overtime_rate);
    
    cout
    << fixed << std::setprecision(2) << right
    << "                Your weekly gross pay is: $" << setw(10) << gpay << '\n'
    << "Your total pay for four weeks of work is: $" << setw(10) << gpay * 4 << '\n'
    << "                   Your yearly income is: $" << setw(10) << gpay * 52 << '\n';
    
    return 0;
}


Please enter the number of hours worked in a week: 46
Please enter the hourly wage: 12.25
                Your weekly gross pay is: $    600.25
Your total pay for four weeks of work is: $   2401.00
                   Your yearly income is: $  31213.00
Program ended with exit code: 0


Thank You Very Much Kemort,
But my class assignment is to include a loop for the 52 weeks. How would I go about doing that, if at all. It is also stated that we can use the do while statement for this assignment but I'm not sure if that would be sufficient or is this just going over my head. I figured that all I needed to do was just gpay * 52 but I can't help feel as if I need something more.
Once again thank you for the quick response and I look forward to hearing from you again.
Last edited on
closed account (48T7M4Gy)
Maybe something like this you can build on?
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
42
43
#include <iostream>
#include <iomanip>

using namespace std; // if you use this you don't need std::

const double overtime_rate = 1.5;

int main()
{
    double hours = 0, hwage = 0, gpay = 0; // use double & initialize variables
    double total = 0;
    
    int week = 0;
    
    do{
        week++;
        
        cout
        << "===================================================\n"
        << "Week " << week << '\n'
        
        << "Please enter the number of hours worked in a week: ";
        cin >> hours;
        
        cout << "Please enter the hourly wage: ";
        cin >> hwage;
        
        if (hours <= 40)
            gpay = hours * hwage;
        else
            gpay = (40 * hwage) + ((hours - 40) * hwage * overtime_rate);
        
        total += gpay;
        
        cout
        << fixed << std::setprecision(2) << right
        << "                Your pay this week is: $" << setw(10) << gpay << '\n'
        << "Your total pay for the year so far is: $" << setw(10) << total << '\n';
    }
    while(week <= 52);
    
    return 0;
}
closed account (48T7M4Gy)
===================================================
Week 1
Please enter the number of hours worked in a week: 20
Please enter the hourly wage: 45
                Your pay this week is: $    900.00
Your total pay for the year so far is: $    900.00
===================================================
Week 2
Please enter the number of hours worked in a week: 45
Please enter the hourly wage: 20
                Your pay this week is: $    950.00
Your total pay for the year so far is: $   1850.00
===================================================
Week 3
Please enter the number of hours worked in a week:


PS. This is a sample, you can do the 52 weeks if you like :)
closed account (48T7M4Gy)
Also, if you have to calculate monthly (every 4 weeks?) pays,
as a hint week % 4 == 0, or could be week-1 % 4 and monthly sub-total is set to zero at the start of each 4 weeks.
Thank you so very much kemort,

I am ecstatic about this program. I just wanted to say thank you very much all it took was just a couple of minor changes to your code and it is working exactly how I want it to work. Once again thank you very much and I can't wait to hear from you again.
Topic archived. No new replies allowed.