Please tell me whats wrong with this code. It keeps crashing. The purpose is giv
ID: 3828588 • Letter: P
Question
Please tell me whats wrong with this code. It keeps crashing. The purpose is given in the comments
//A financial services company guarantees a 1% monthly compounded return on your investment.
//You want to see how long it takes to reach a total of $1,000,000 (million dollars) in investment plus return.
//Write a program that allows the user to enter a continuous monthly investment (Example - $500 every month) or ($1,000 every month).
//The program should call a recursive method that returns and displays the total months needed to reach the $1,000,000 goal.
//Example - At the 1 % compounded monthly rate, and a $1, 000 monthly investment, the first month should yield $1, 010.00,
//the second month $2, 030.10, the third month $3, 060.40, the fourth month $4, 101.01, etc.
//Display how many total months it takes the user to reach the million dollar goal.
//Remember, the user should be able to input the monthly continuing investment.
//The rate is always 1 % and compounded monthly.
#include "stdafx.h"
#include <iostream>
using namespace std;
int millions(double, int); ////Function Prototype
int main()
{
double amount = 0; //variable to hold users input
int months = 0; //variable to hold number of months it takes
double account = 0;
int count = 0;
cout << "Input the amount you intend to deposit each month, the program will then return the number of months it will take to reach $1000000 with a 1% compounding interest. "; //Explanation of use
cout << " Monthly investment : "; //Prompt for input
cin >> amount; //Input of value to amount by user
months = millions(amount, months);
cout << months;
return 0;
}
int millions(double amount, int months)
{
double account = 0;
if (account == 1000000)
return account;
else
months = months++;
account = (millions(amount, months) + amount)*1.01;
return account;
}
Explanation / Answer
Your requirement is to return number of months and here you are returning account which is wrong.
so in if change return account to return months and in final return also return months.
also you need are trying to execute two statement in else so you need to keep them under angular brackets { }
else
{
months = months++;
millions((amount+amount*0.01),months);
}
Also here we need to send recursively the increasing amount hence amount + 1% of amount