While Loop

Hi, I'm trying to edit below using while loop to allow user to keep selecting choices until exit (option 4) is selected. This is my first while loop program and I've been trying to figure it out for a while, so it really bugs me to not be able to figure it out. I know it is probably really simple too! Any help or insight would be greatly appreciated.

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
#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)
	{
		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 enter a valid option.";
				break;
		}
		
		{
		cout << endl;
		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;	
		}
	}
	cout << endl;
	cout << "Thanks for using our program!" << endl;
	
}


What is your name? Busy Bee
How old are you? 42
Please select an option:
(1) Number of years to retirement
(2) Amount earned between now and retirement
(3) Amount saved at retirement
(4) Exit (do nothing) 
1
You have 28 years until retirement.

Please select an option:
(1) Number of years to retirement
(2) Amount earned between now and retirement
(3) Amount saved at retirement
(4) Exit (do nothing)
2 
How much do you make per week in dollars? 998
You will earn $1453088 between now and retirement.

Please select an option:
(1) Number of years to retirement
(2) Amount earned between now and retirement
(3) Amount saved at retirement
(4) Exit (do nothing)
3
:: executes case 3::

Please select an option:
(1) Number of years to retirement
(2) Amount earned between now and retirement
(3) Amount saved at retirement
(4) Exit (do nothing)
4


Thanks for using our program!
Last edited on
1
2
3
4
while (option < 4)
{
cout << "Please select an option: " << endl; \\ This doesn't work, but tried it
} 


This will spam you with "Please select an option: \n" for all eternity.
Am I on the right track with placement of while loop and closure?
Well, 2 things:

One line comments are double slashes //, not backslashes \\.

You need to include the following switch into your while block, and you also need to include code that actually prompts the user to enter an option..
Figured it out.... whooo hooo thanks for the pointer in the right direction
Topic archived. No new replies allowed.