156k views
3 votes
Write an application that prompts a user for the number of years the user has until retirement and then the amount of money the user can save annually. If the user enters 0 or a negative number for either value, reprompt the user until valid entries are made. Assume that no interest is earned on the money. Display the amount of money the user will have at retirement.

User CanCeylan
by
5.8k points

1 Answer

3 votes

// variables to save fields given by the user

int years_left =0;

int amount=0;

int flag =0; // to check if both fields are valid

// while loop

while ( years_left >0 && amount >0){

// taking years left as input

cout<<" Enter the years left for your retirement: ";

cin>>years_left;

// if valid add in flag variable

if(years_left>0){

flag +=1;

}

// input amount saved annually

cout<<" Enter the amount of money saved annually : ";

cin>> amount;

// if valid flag ++

if(amount>0)

flag+=1;

// if both fields are vaild, out put them

if(flag==2)

{

cout<<" Years left are : " << years_left<< endl;

cout<<" Amount saved annually : " << amount <<endl;

}

flag = 0;

}

User Ed Orsi
by
5.0k points