Loop Condition

Hi,
I'm not understanding how to state what I need this to do in my condition statement. Could you offer insight?

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string name;
	int age, option, salary, retireDif, retireAge, totalEarned;
	double totalSaved, savingsRate;
	
	cout << "What is your name? ";
	getline(cin, name);

	cout << "How old are you? ";
	cin >> age;
	if (age >= 60)
	{
		retireAge = 65;
	}
			
	if ((age >= 50) && (age < 60))
	{
		retireAge = 67;
	}
		
	if ((age >= 40) && (age < 50)) 
	{
		retireAge = 70;
	}
		
	if (age < 40)
	{
		retireAge = 72;
	}
	retireDif = retireAge - age;	
	cout << "Please select an option: " << endl;
	cout << "\t (1)		Number of years to retirement" << endl;
	cout << "\t (2)		Amount earned between now and retirement" << endl;
	cout << "\t (3)		Amount saved at retirement" << endl;
	cout << "\t (4)		Exit (do nothing)" << endl;
	cin >> option;
	while (option != 4) // while option is not equal to 4 
	{
		switch(option)
		{
			case 1:
				cout << "You have " << retireDif << " years until retirement." << endl;
				break;
			case 2:
				cout << "How much do you make per week in dollars? ";
				cin >> salary;
				totalEarned = salary * 52 * retireDif;
				cout << "You will earn $" << totalEarned << " between now and retirement." << endl;
				break;
			case 3:
				cout << "How much do you make per week in dollars? ";
				cin >> salary;
				totalEarned = salary * 52 * retireDif;
				cout << "What percentage will you save? ";
				cin >> savingsRate;
				totalSaved = (totalEarned * savingsRate) / 100;
				cout << "You will have " << totalSaved << " saved when you retire." << endl;
				break;
			case 4:
				break;
			default:
				cout << "Please choose a valid option.";
				cout << endl;
				break;
		}
		{ // can't figure out condition. the above should continue to loop until 4 
		// is inputed when 4 is inputed below message should display
		}
}
	cout << endl;
	cout << endl;
	cout << "Thanks for using our program!" << endl;

}
// can't figure out condition. the above should continue to loop until 4
// is inputed when 4 is inputed below message should display

just display line 76 for case 4:

remember that break; will only cause your switch loop to exit, the outer while() loop will still run, and if option != 4; you'd get a nasty infinite loop
closed account (D80DSL3A)
I think you are quite close, but matsom is correct - any value but option = 4 would result in the while loop never exiting. This is because nothing within the loop changes the value of option.

Simply move the code you are using to prompt the user for option (lines 35-40) inside the while loop but before the switch statement (ie between lines 42 and 43). This will allow the user to enter an option repeatedly until he/she enters 4.

You will need to initialize option to some value other than 4 so that the while loop is entered. You can do this where you declare option on line 7.

Also, you need a return 0; before exiting main().

@matsom. Good point regarding relocating line 76. That would work.
It will also work where it is now.

Thank you so much! I used both your advice because I couldn't get one to work without the other. I have another question...when I initialize option to value why doesn't it matter whether I use 1 or 0 or any number except 4?
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string name;
	int age, option = 3, salary, retireDif, retireAge, totalEarned;
	double totalSaved, savingsRate;
	
	cout << "What is your name? ";
	getline(cin, name);

	cout << "How old are you? ";
	cin >> age;
	if (age >= 60)
	{
		retireAge = 65;
	}
			
	if ((age >= 50) && (age < 60))
	{
		retireAge = 67;
	}
		
	if ((age >= 40) && (age < 50)) 
	{
		retireAge = 70;
	}
		
	if (age < 40)
	{
		retireAge = 72;
	}
	retireDif = retireAge - age;	
	while (option != 4) 
	{
		
		cout << "Please select an option: " << endl;
		cout << "\t (1)		Number of years to retirement" << endl;
		cout << "\t (2)		Amount earned between now and retirement" << endl;
		cout << "\t (3)		Amount saved at retirement" << endl;
		cout << "\t (4)		Exit (do nothing)" << endl;
		cin >> option;
		switch(option)
		{
			case 1:
				cout << "You have " << retireDif << " years until retirement." << endl;
				cout << endl;
				break;
			case 2:
				cout << "How much do you make per week in dollars? ";
				cin >> salary;
				totalEarned = salary * 52 * retireDif;
				cout << "You will earn $" << totalEarned << " between now and retirement." << endl;
				cout << endl;
				break;
			case 3:
				cout << "How much do you make per week in dollars? ";
				cin >> salary;
				totalEarned = salary * 52 * retireDif;
				cout << "What percentage will you save? ";
				cin >> savingsRate;
				totalSaved = (totalEarned * savingsRate) / 100;
				cout << "You will have " << totalSaved << " saved when you retire." << endl;
				cout << endl;
				break;
			case 4:
				cout << endl;
				cout << endl;
				cout << "Thanks for using our program!" << endl;
				break;
			default:
				cout << "Please choose a valid option." << endl;
				cout << endl;
				break;
		}
		
	}
}
Topic archived. No new replies allowed.