Nyp0ns0pp0saurus
Nyp0ns0pp0saurus

Reputation: 83

Iteration loop in visual studio 2013 C++

I need code that calculates user chosen values (amount saved per year, amount to reach and interest in %).

My best guess is that this could be solved with a loop of some sort? So 500 per year * 1.05 (in interest is 5%) = 525 + 500 = 1025 * 1.05.. And so on.

Might not be that hard... But I also need to know how many years it would take to reach a goal, so how many times it would need to loop to reach >= 50 000 for example. I'v started doing some coding but this is my second thing after "Hello world". Any help with this at all would be amazing!

#include <iostream>

using namespace std;

int main() {
    float save, goal, intrest;

    cout << "save: ";
    cin >> save;
    cout << "goal: ";
    cin >> goal;
    cout << "intrest: ";
    cin >> intrest;

    float intrest1(ranta / 100 + 1);
    float sum = save * intrest1;

    while (sum < goal) {
        sum + 500 = 
    }

    cout << "summa: " << sum;
    system("pause");
}

Upvotes: 1

Views: 119

Answers (1)

jcarpenter2
jcarpenter2

Reputation: 5468

I think you're right about using a loop to do the calculation, and you're also right that it's not that hard :).

I just edited your code a little, and made it output the total amount you save as well as how many years it takes to reach your goal.

Each iteration, I'm incrementing the number of years and then assigning sum to be the amount saved after that year.

Hopefully this helps you get to where you're going.

#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
    float save, goal, interest;

    cout << "save: ";
    cin >> save;
    cout << "goal: ";
    cin >> goal;
    cout << "intrest: ";
    cin >> interest;

    float interest1 = interest / 100 + 1;
    float sum = 0;
    int years = 0;

    while (sum < goal) {
      years = years + 1;
      sum = sum * interest1 + save;
    }

    cout << "sum: " << sum << "\n";
    cout << "years: " << years << "\n";
    system("pause");
}

Upvotes: 1

Related Questions