While loop pausing, or ignoring the loop condition all together.

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;

while (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";
system("PAUSE");
}


Also, I know I shouldn't use system("PAUSE") but it seems to be ignoring cin.get() and just ends.

Any hints or help would be greatly appreciated.
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.
1
2
while (n == false)
{
Last edited on
For bonus marks, avoid using float types in financial applications; the imprecision causes the loss/gain of money, and accountants get very upset.
Fixed up some of your code to make it a bit more readable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>

using namespace 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();
}
*Facepalm* Alright, thank you all so much. It works now.

Also, sorry for formatting, or the lack thereof to be more precise. Will fix that next time.
Topic archived. No new replies allowed.