Daniel Bäck Daniel Bäck -4 years ago 95
C++ Question

calculate x years for savings with deposition and revenue C++

I'm sitting with a challenging homework in C++ and would be really thankfull for some help here!

My program need to calculate how many years it will take for an optional yearly deposition with revenue to reach a specific savings limit.

I just can't see what's wrong and have tried debugging with no help.
It doesn't really help neither that i'm totally new to C++ and MVS 2015.

I don't know if it's the math or the programming itself that is wrong.
Static typing is foreign to me since i usually use python.

Also VS don't give much information and the program just stops after asking for revenue input.

Any suggestions?

#include "stdafx.h"
#include<iostream>
#include<string>
#include<cmath>
using namespace std;


int main()
{


int deposit;
int max_savings;
double revenue;
double change_factor;
double year = 0;
double geometric_sum;

cout << "Choose a yearly deposition:\n";
cin >> deposit;
cout << "Set your max saving-goal:\n";
cin >> max_savings;
cout << "set a revenue in percent:\n";
cin >> revenue;

change_factor = 1 + (revenue / 100);
geometric_sum = ((double)deposit * (pow(change_factor, year) - 1)) / (change_factor - 1);

while (geometric_sum < max_savings)

year++;

cout << "Your saving-goal will be in " << year << " years!" << endl;
cout << "Your account balance will then be " << geometric_sum << " dollars!" << endl;




return 0;
}

Answer Source

pow(change_factor, year) - 1

year is set to 0. Any value at the power of 0 is 1. 1 - 1 = 0. Basically you are multiplying with 0.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download