Problem with While Loops

I'm currently taking an introduction to C++ class online and my professor is unavailable at the moment for help. I was hoping someone could help me with a problem I'm having with While loops.

The assignment was to add on to a previous program allowing users to keep on selecting choices until they exit. There should also be a message "Thanks for using our program!" added to the end. This is the program I have built so far, it runs and all but I just have no idea where to start in terms of adding While loops into the mix. Please someone help!

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string name;
int weeks, age, input, salary, savings, earnTotal, retireAge, option;
double savingsTotal;
weeks = 52;
cout << "What is your name?";
getline (cin,name);
cout << "What is your age?";
cin >> input;
age = input;
cout << endl;
if (age < 40)
{
retireAge = 72;
}
else
{
if ((age >= 40) && (age < 50))
{
retireAge = 70;
}
else
{
if ((age >=50) && (age < 60))
{
retireAge = 67;
}
else
{
(age >= 60);
retireAge = 65;
}
}
}
cout << "Please select an option: " << endl;
cout << "\t1. Number of years to retirement" << endl;
cout << "\t2. Amount earned between now and retirement" << endl;
cout << "\t3. Amount saved at retirement" << endl;
cout << "\t4. Exit (do nothing)"<< endl;
cin >> option;
switch (option)
{
case 1:
cout << "You have " << (retireAge - age) << " years until retirement." << endl;
break;
case 2:
cout << "How much do you make per week in dollars?";
cin >> input;
salary = input;
earnTotal = ((retireAge - age) * (weeks * salary));
cout << "You will earn $" << earnTotal << " between now and retirement." << endl;
break;
case 3:
cout << "How much do you make per week in dollars?";
cin >> input;
salary = input;
cout << "What percentage will you save?";
cin >> input;
savings = input;
earnTotal = ((retireAge - age) * (weeks * salary));
savingsTotal = earnTotal * (.01 * savings);
cout << "You will have saved $" << savingsTotal << " when you retire." <<endl;
break;
case 4:
cout << option;
break;
case 5:
default:
cout << "Invalid option." << endl;
}
}


A while loop is for doing something while some condition is true.

Start by clearly identifying what is it that you want to do while some condition is true, and what that condition is.
Last edited on
You actually need a loop(while or for), which has to be executed and only when a user want to exit (or when a user select the 4th option from your menu ), the program will do so.
closed account (zwA4jE8b)
http://cplusplus.com/doc/tutorial/control/
Topic archived. No new replies allowed.