Hi, at the moment I am doing a independent study to learn C++ and right now I am stumped by a financial planning assignment. I've asked my teacher and I've looked over my code, tried using for loops, while loops, do while loops and when I compile and run the program it either stops at the loop and never leaves or it ignores the loop conditions and goes through it once and ends.
Here's the assignment:
Project 8-3 ~ Fiancial Planning
Write a program that calculates the amount of time necissary to reach a certain financial goal by consisrtently deposting the same amount of money into tan interest-bearing account eah month. the account is compounded monthly.
#include <cstdlib>
#include <iostream>
using namespace std;
int main()
{
float deposit, goal, interest, balance, month;
balance = 0;
month = 0;
bool n;
n = false;
cout << "What are you depositing each month?\n";
cin >> deposit;
cout << "What is your goal?\n";
cin >> goal;
cout << "What is the monthly interest rate? In percents.\n";
cin >> interest;
I've not had a chance to study this closley, but it maybe that you're dividing by an int to calculate the new balance and it never gets as high as the value in goal.
Try using floats in the division and see if it works. balance = (balance * (1.0 + (interest / 100.0)));
oh, I've just noticed that you have a ; after the while statment, remove it and it should work.
#include <iostream>
usingnamespace std;
int main()
{
double deposit,
goal,
interest,
balance = 0,
month = 0;
bool n = false; // Maybe use a different var name like runtime or hasReachedGoal?
cout << "What are you depositing each month?\n";
cin >> deposit;
cout << "What is your goal?\n";
cin >> goal;
cout << "What is the monthly interest rate? In percents.\n";
cin >> interest;
while (!n) // You had an odd semicolon here
// (!n) is equivalent to (n == false)
{
balance += deposit;
balance = (balance * (1 + (interest / 100)));
month++;
if (balance >= goal)
n = true;
cout << "Balance: " << balance << '\n';
cout << "Month: " << month << '\n';
}
cout << "It would take " << month << " months to reach your goal.\n";
cin.ignore();
cin.get();
}