Some problems with a program

So I'm trying to make a program that calculates the hours and pay for the number of shifts you worked but I'm having trouble with it not adding the hours that are put in properly and I'm wondering how to make it apply breaks for every shift separately and not to the total. Pretty much what I'm trying to do is:

Input amount of shifts (lets say 3)
Start of the shift (lets say 12)
End of the shift (lets say 20)
The total hours worked (which would be 8)

Then I want it to make it ask me the same thing the amount of times I put in "shifts" which would be 3 in this case. And while displaying the hours per shift I want it to total my hours as well and display that.
Then subtract the breaks from the individual hours and add the results together in an Actual hour variable. Then that goes on to be multiplied by wage.

So far I've gotten to this. Bare in mind I'm SUPER new to programming so when explaining pretend like I'm 5.

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
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

void main()
{
	const float wage = 10.25;
	float hrStart = 0;
	float hrEnd = 0;
	float breakTime;
	float actualHr = 0;
	float hr;
	int numOfShifts;
	int shift = 0;
	float wageTotal;
	float hrTotal;

		cout << "Enter amount of shifts worked: ";
		cin >> numOfShifts;

		while (shift < numOfShifts) {

		cout << "Enter starting time: ";
		cin >> hrStart;
		cout << "Enter ending time: ";
		cin >> hrEnd;
		
		hr = hrEnd - hrStart;

		shift++;
		
		cout << "\n You have worked: " << hr << " hours. \n\n";

		if (hr==8) { 
			breakTime = 0.75;
		}

		if (hr<8 && hr>4) {
			breakTime = 0.5;
		}

		if (hr==4) {
			breakTime = 0.25;
		}

		actualHr = hr - breakTime;

		cout << "Actual hours you have worked: " << actualHr;
		cout << "\n\n You have earned: " << actualHr * wage << "$ \n\n";

		cout << "Total Hours worked: " << hrTotal

		}
		cout << "\n\n\n Thank You \n";
}
Your breakTime isnt initialised ,and is gonna be if the 8>= hours >= 4 ,what if the employe works more or less than that? if the variable does bot assinged well It will takes 0 or a random value the calculation would be wrong ,so initial it or assign some appropriate values to it , same for wageTotal and hrTotal

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
44
45
46
47
48
49
50
51
52
53
54
# include <iostream>
using namespace std;

int main()
{
	const float wage = 10.25;
	float hrStart = 0;
	float hrEnd = 0;
	float breakTime = 0;
	float actualHr = 0;
	float hr;
	int numOfShifts;
	int shift = 0;
	float wageTotal = 0;
	float hrTotal = 0;

    cout << "Enter amount of shifts worked: ";
    cin >> numOfShifts;

    while(shift < numOfShifts)
    {
        cout << "Enter starting time: ";
        cin >> hrStart;
        cout << "Enter ending time: ";
        cin >> hrEnd;

        hr = hrEnd - hrStart;

        shift++;
        cout << "\n You have worked: " << hr << " hours. \n\n";

        if (hr==8)
            breakTime = 0.75;

        if (hr<8 && hr>4)
            breakTime = 0.5;

        if (hr==4)
            breakTime = 0.25;

        actualHr = hr - breakTime;
        hrTotal += actualHr;
        wageTotal += actualHr * wage;

        cout << "Actual hours you have worked: " << actualHr;
        cout << "\n\n You have earned: " << actualHr * wage << "$ \n\n";
        cout << "Total Hours worked: " << hrTotal << endl << endl;
    }

    cout << "The Total wage is : " << wageTotal << endl;
    cout << "\n\n\n Thank You \n";

    return 0;
}


Hope that helps
Last edited on
yes thank you it works perfectly now!
Topic archived. No new replies allowed.