Write an application that calculates the amount of money earned onan investment,
ID: 3608912 • Letter: W
Question
Write an application that calculates the amount of money earned onan investment, based on an 8% annual return. Prompt the user toenter an investment amount and the number of years for theinvestment. Do not allow the user to enter a number of years lessthan 1 or more than 30. Display the total amount (balance) for eachyear of the investment.Please help me with the second part, I seem to have difficultieswith nested loops.
this is what i got so far:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int investment;
const double rate = .08;
int years;
int count = 1;
cout<<"Please enter the amount you wish toinvest. ";
cin>>investment;
cout<<"Please enter the number of yearsthat you wish to invest. ";
cin>>years;
while(count<=years)
{
cout<<"At the start ofthe year "<<count<<", you have $"<<investment<<endl;
investment = investment +investment * rate;
++count;
}
system("pause");
}
Explanation / Answer
Your problem is that you are displaying the amount ofinvestment at the beginning of the year then calculating theinterest. With your current code for the last year you arenot outputting the final amount of investemnt. for example if the user enters 100 for the initial investmentand 1 for the number of years when we enter the while loop theexpression is evaluated with count as 1. So because count = 1 andyears =1 the expression count <= years evaluates to true and theprogram enters the loop. it then outputs "At the stoart ofthe year 1 you have $100" it then calculates investment as $108.count is then incremented to 2. now it tries to reevaluate count<= years and it finds that 2 is not <= 1 so it breaks fromthe while loop and goes on to system pause. If you want todisplay the final amount of money, which I assume you do, youeither need to add an output after the while loop, extend the loopto count <=years + 1 or change your output to at the end of theyear you have x investment and place it after the investmentcalculation. also because the requirement to verify the input number ofyears as between 1 and 30, you should add another while loop afterthe line that accepts the years. This while loop shouldexecute as long as the input is invalid and ask the user to tryagain. such as while( years > 30 || years < 1) { cout << "Enter a valid number ofyears" << endl; cin << years; }Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.