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.
#include <iostream>
#include <string>
usingnamespace 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!