For loop repeating troubleshooting

Hey guys, I'm currently learning loops in my C++ class at school. I seem to be pretty stumped on the concept of the for loop, and was wondering if anybody could give me any pointers?
I'll just post the exert of code that I'm working on at the moment. I am doing a payment program that calculates wages and such.
Our instructor told us to use a for loop to ask how many weeks are in the pay week, and then loop the hours worked menu for each week.
So basically I need the for loop to loop x amount of times (x being the user input amount).
If anybody could help, I'd appreciate it!

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  	
	char newpaystub;
	newpaystub = 'y';
	
	while (newpaystub != 'n')
	{
{
	int hoursMenu, payPeriod;
	double worked;
	
	cout << "How many weeks are in the payperiod?\n";
	cin >> payPeriod;

    for (hoursMenu
	cout << " _________________\n"; 
	cout << "|  Hours Worked   |\n";
	cout << "|-----------------|\n";
	cout << "|1) 40            |\n";
	cout << "|2) 35            |\n";
	cout << "|3) 30            |\n";
	cout << "|4) 20            |\n";
	cout << "|5) 15            |\n";
	cout << "|6) Other         |\n";
	cout << "|-----------------|\n";
	cout << "Enter your choice (1 - 6): ";
	cin >> hoursMenu;
	
	switch(hoursMenu)
	{
	case 1:
		worked = 40;
		break;
	case 2: 
		worked = 35;
		break;
	case 3:
		worked = 30;
		break;
	case 4:
		worked = 20;
		break;
	case 5:
		worked = 15;
		break;
	case 6:
		cout << "Please enter the amount of hours worked: \n";
		cin >> worked;
		break;
	default:
		cout << "You have made a wrong choice!\n";
		break;
	}
		{
		  double wage;
  const char* company_Name = "WalMart ";
  double tax_Rate = 0.27;
  bool overTime;
  char name[256];
  
    srand(time(0));
    int random = rand() % (1000 - 100) + 100;
  
  std::cout << std::fixed;
    std::cout << std::setprecision(2);
    std::cout << std::right;
    std::cout << std::setw(10);
  
  std::cout << company_Name << "\n";
  std::cout << "\n";
  std::cout << "Wage Calculator\n";
  std::cout << "\n";
  std::cout << "HR manager, please enter Employees full name: ";
  cin.ignore();
  std::cin.getline (name,256);
  std::cout << "Please Enter the Hourly Wage: $";
  std::cin >> wage;
  std::cout << "\n";
  
  if (worked > 40 && wage > 7.25)
  {
      double overWorked, doublePay, overPay;
      
      overTime = true;
      
      overWorked = worked - 40;
      doublePay = wage * 1.5;
      overPay = overWorked * doublePay;
      wage += overPay;
      
      
  }
  else
  {
      overTime = false;
  }

  std::cout << "Hourly Wage: $" << wage << ".\n";
  
    if (overTime == true)
  {
      cout << "You received overtime pay.\n";
  }
  
  std::cout << "Hours Worked: " << worked << ".\n";
  
  tax_Rate *= 100;
  std::cout << "The Tax rate is " << static_cast <int>(tax_Rate) << ".\n";
  
  std::cout << "\n";
  double gross, net;
  gross = wage*worked;
  net = gross - tax_Rate;
  
  std::cout << company_Name << "Pay Stub\n";
  std::cout << "Employee: " << name << "\n";
  std::cout << "Employee Number: " << random << "\n";
  std::cout << "Gross Pay: $" << gross << "\n";
  std::cout << "Net Pay: $" << net << "\n\n";
  gross = 0;
  net = 0;
  
	}
}
    cout << "Would you like to enter a new paystub?(y/n)\n";
    cin >> newpaystub;
	}


		break;
1
2
3
4
5
6
7
// Loop x times
for (int i = 0; i<x ; ++i)
{

  // do things

}
So should i be my menu variable and x be the user input variable? I'll try and see if I can get that to work, I saw the example on the instructional page but didn't quite understand it.
Okay, I tried working with the code like you showed, and now it acts as if the code isn't even there? I'll send the full code that I'm working with, as it compiles if anybody would like to see how it is compiling.


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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
    int choice;
    
    do
    {

	cout << " _____________________________\n"; 
	cout << "|    Main Menu                |\n";
	cout << "|-----------------------------|\n";
	cout << "|1) Display Job Postings      |\n";
	cout << "|2) Calculate Wages           |\n";
	cout << "|3) Exit                      |\n";
	cout << "|_____________________________|\n";
	cout << "Enter your choice (1 - 3): ";
	cin >> choice;
	
	switch(choice)
	{
	case 1:
	
	char seeAgain;
	seeAgain = 'y';
	
	while (seeAgain != 'n')
	{
	    
		cout << "\n1. Cashier - $7.25 hr\n";
		cout << "2. Accounting - $8.00 hr\n";
		cout << "3. Stocker - $9.00 hr\n";
		cout << "4. Service Desk - $9.00 hr\n";
		cout << "5. Maintenance - $8.00 hr\n";
		cout << "6. Assistant Manager - $10.00 hr\n";
		cout << "7. Floor Associate - $8.00 hr\n\n";
		
		cout << "In-company hiring bonus?(y/n)\n";
		char answer;
		cin >> answer;
		
		if (answer == 'y' || answer == 'Y')
		{
		    cout << "Premium Health Insurance plan included!\n";
		}
		else if (answer == 'n' || answer == 'N')
		{
		    cout << "No bonus added.\n";
		}
		else
		{
		    cout << "Invalid selection!\n";
		}
		    cout << "Would you like to select another job?(y/n)\n";
		    cin >> seeAgain;
	}
		break;
	
	char newpaystub;
	newpaystub = 'y';
	
	while (newpaystub != 'n')
	{
{
	int hoursMenu, payPeriod;
	double worked;
	
	cout << "How many weeks are in the payperiod?\n";
	cin >> payPeriod;

    for (hoursMenu = 0; hoursMenu < payPeriod; ++hoursMenu) 
	cout << " _________________\n"; 
	cout << "|  Hours Worked   |\n";
	cout << "|-----------------|\n";
	cout << "|1) 40            |\n";
	cout << "|2) 35            |\n";
	cout << "|3) 30            |\n";
	cout << "|4) 20            |\n";
	cout << "|5) 15            |\n";
	cout << "|6) Other         |\n";
	cout << "|-----------------|\n";
	cout << "Enter your choice (1 - 6): ";
	cin >> hoursMenu;
	
	switch(hoursMenu)
	{
	case 1:
		worked = 40;
		break;
	case 2: 
		worked = 35;
		break;
	case 3:
		worked = 30;
		break;
	case 4:
		worked = 20;
		break;
	case 5:
		worked = 15;
		break;
	case 6:
		cout << "Please enter the amount of hours worked: \n";
		cin >> worked;
		break;
	default:
		cout << "You have made a wrong choice!\n";
		break;
	}
		{
		  double wage;
  const char* company_Name = "WalMart ";
  double tax_Rate = 0.27;
  bool overTime;
  char name[256];
  
    srand(time(0));
    int random = rand() % (1000 - 100) + 100;
  
  std::cout << std::fixed;
    std::cout << std::setprecision(2);
    std::cout << std::right;
    std::cout << std::setw(10);
  
  std::cout << company_Name << "\n";
  std::cout << "\n";
  std::cout << "Wage Calculator\n";
  std::cout << "\n";
  std::cout << "HR manager, please enter Employees full name: ";
  cin.ignore();
  std::cin.getline (name,256);
  std::cout << "Please Enter the Hourly Wage: $";
  std::cin >> wage;
  std::cout << "\n";
  
  if (worked > 40 && wage > 7.25)
  {
      double overWorked, doublePay, overPay;
      
      overTime = true;
      
      overWorked = worked - 40;
      doublePay = wage * 1.5;
      overPay = overWorked * doublePay;
      wage += overPay;
      
      
  }
  else
  {
      overTime = false;
  }

  std::cout << "Hourly Wage: $" << wage << ".\n";
  
    if (overTime == true)
  {
      cout << "You received overtime pay.\n";
  }
  
  std::cout << "Hours Worked: " << worked << ".\n";
  
  tax_Rate *= 100;
  std::cout << "The Tax rate is " << static_cast <int>(tax_Rate) << ".\n";
  
  std::cout << "\n";
  double gross, net;
  gross = wage*worked;
  net = gross - tax_Rate;
  
  std::cout << company_Name << "Pay Stub\n";
  std::cout << "Employee: " << name << "\n";
  std::cout << "Employee Number: " << random << "\n";
  std::cout << "Gross Pay: $" << gross << "\n";
  std::cout << "Net Pay: $" << net << "\n\n";
  gross = 0;
  net = 0;
  
	}
}
    cout << "Would you like to enter a new paystub?(y/n)\n";
    cin >> newpaystub;
	}
	


		break;

	case 3:
	
		break;
		
	default:
		cout << "\nYou did not select a valid choice!\n";
		break;
	}
    }
    while (choice != 3);
    
    return 0;

}
Last edited on
Your loop counter is compared to the variable hoursMenu to know when it's time to stop looping, but you let the user change that variable inside the loop? WTF? No.

How many times is the for loop supposed to loop?

Our instructor told us to use a for loop to ask how many weeks are in the pay week, and then loop the hours worked menu for each week.


1
2
3
4
5
6
7
8
9
// ask how many weeks are in the pay week
cout << "how many weeks are in the pay week?";
cin >> number_of_weeks_in_the_pay_week;

// then loop the hours worked menu for each week
for (int i = 0; i < number_of_weeks_in_the_pay_week ; ++i)
{
  // do things
}


Last edited on
Uh thanks.
Topic archived. No new replies allowed.