inserting a switch for my menu

inserting a switch for my menu instead of using while (option == 2) and also the program is supposed to re run for the number of workers you put in.


#include <iostream>
using namespace std;

int main()
{
double workers;
double option;
double hours;
double wage;
double sales;
double items;
double salary;
int i;

cout << "Enter the number of workers that will be included in the weekly salary calculation: ";
cin >> workers;
cout << endl;
cout << " ** PAYROLL MENU **" << endl;
cout << "1 - Manager\n";
cout << "2 - Salary\n";
cout << "3 - Commision Worker\n";
cout << "4 - Piece worker\n";
cout << endl;
cout << "Select the type of worker to pay: ";
cin >> option;
while (option == 1) {
salary = 1875.00 * workers;
cout << "The current weekly salary is: $" << salary;
break;
}
while (option == 2) {
cout << "Enter the amount of hours worked: ";
cin >> hours;
if (hours > 40) {
cout << endl;
cout << "enter the wage per hour: ";
cin >> wage;
cout << endl;
salary = (40 * wage) + ((hours - 40) * (wage * 1.5));
cout << "The current weekly salary is: $" << salary; // IDK if i did the overtime part correctly but it works
if
}
cout << endl;
cout << "enter the wage per hour: ";
cin >> wage;
cout << endl;
salary = hours * wage * workers;
cout << "The current weekly salary is: $" << salary;
break;
}
while (option == 3) {
cout << "Enter the gross weekly sales: ";
cin >> sales;
cout << endl;
salary = 250 + (.0057 * sales) * workers;
cout << "The current weekly salary is: $" << salary;
break;
}
while (option == 4) {
cout << "enter the number of items produced this week: ";
cin >> items;
cout << endl;
salary = items * 6.89 * workers;
cout << "The current weekly salary is: $" << salary;
break;
}

return 0;

}
Please edit your post for readability.
https://www.cplusplus.com/articles/jEywvCM9/
Use code tags when posting code so that the code is readable and formatted.

As formatted:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
using namespace std;

int main() {
	double workers;
	double option;
	double hours;
	double wage;
	double sales;
	double items;
	double salary;
	int i;

	cout << "Enter the number of workers that will be included in the weekly salary calculation: ";
	cin >> workers;
	cout << endl;
	cout << " ** PAYROLL MENU **" << endl;
	cout << "1 - Manager\n";
	cout << "2 - Salary\n";
	cout << "3 - Commision Worker\n";
	cout << "4 - Piece worker\n";
	cout << endl;
	cout << "Select the type of worker to pay: ";
	cin >> option;

	while (option == 1) {
		salary = 1875.00 * workers;
		cout << "The current weekly salary is: $" << salary;
		break;
	}

	while (option == 2) {
		cout << "Enter the amount of hours worked: ";
		cin >> hours;
		if (hours > 40) {
			cout << endl;
			cout << "enter the wage per hour: ";
			cin >> wage;
			cout << endl;
			salary = (40 * wage) + ((hours - 40) * (wage * 1.5));
			cout << "The current weekly salary is: $" << salary; // IDK if i did the overtime part correctly but it works if
		}

		cout << endl;
		cout << "enter the wage per hour: ";
		cin >> wage;
		cout << endl;
		salary = hours * wage * workers;
		cout << "The current weekly salary is: $" << salary;
		break;
	}

	while (option == 3) {
		cout << "Enter the gross weekly sales: ";
		cin >> sales;
		cout << endl;
		salary = 250 + (.0057 * sales) * workers;
		cout << "The current weekly salary is: $" << salary;
		break;
	}

	while (option == 4) {
		cout << "enter the number of items produced this week: ";
		cin >> items;
		cout << endl;
		salary = items * 6.89 * workers;
		cout << "The current weekly salary is: $" << salary;
		break;
	}

	return 0;
}

Not sure what you after re while, option 2 etc, but possibly something like this:

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
using namespace std;

int main() {
	unsigned option {};

	do {
		unsigned workers {};

		cout << "\nEnter the number of workers that will be included in the weekly salary calculation: ";
		cin >> workers;

		cout << "\n ** PAYROLL MENU **\n";
		cout << "1 - Manager\n";
		cout << "2 - Salary\n";
		cout << "3 - Commission Worker\n";
		cout << "4 - Piece worker\n";
		cout << "5 - Exit\n";
		cout << "\nSelect the type of worker to pay: ";
		cin >> option;

		switch (option) {
			case 1:
				cout << "The current weekly salary is: $" << 1875.00 * workers << '\n';
				break;

			case 2:
				{
					double hours {}, wage {}, salary {};

					for (unsigned w {}; w < workers; ++w) {
						cout << "Worker " << w + 1 << ". Enter the amount of hours worked : ";
						cin >> hours;

						cout << "Enter the wage per hour: ";
						cin >> wage;

						salary += hours > 40 ? (40 * wage) + ((hours - 40) * (wage * 1.5)) : hours * wage;
					}

					cout << "\nThe current weekly salary is: $" << salary << '\n';
				}
				break;

			case 3:
				{
					double sales {};

					cout << "Enter the gross weekly sales: ";
					cin >> sales;

					cout << "The current weekly salary is: $" << 250 + (.0057 * sales) * workers << '\n';
				}
				break;

			case 4:
				{
					unsigned items {};

					cout << "Enter the number of items produced this week: ";
					cin >> items;

					cout << "\nThe current weekly salary is: $" << items * 6.89 * workers << '\n';
				}
				break;

			case 5:
				break;

			default:
				std::cout << "Invalid option\n";
				break;
		}
	} while (option != 5);
}

Topic archived. No new replies allowed.