Computing the number of months to save $1000

  • Topic:
  • Thread starter Thread starter MMOne
  • Start date Start date
  • Tags Tags
    Computing
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
1 reply · 2K views
MMOne
Messages
3
Reaction score
0
Hello everyone!
I am very new to programming and am beginning to struggle in studies.

I have been working on this same problem for about 5 hours and cannot for the life of me get this thing to compile correctly. Any assistance or guidance for what I should do would be greatly appreciated.

Ask the user for an amount to save each month. Assuming a 1% per month interest rate, how many MONTHS will it take the user to save \$1000. Add the savings at the END of each month.

Code:
#include <iostream>
using namespace std;
int main (){
   
   const double interest = 0.01;
   int deposit = 0;
   int months = 0;
   double totalSaved = 0.0;
   double interestEarned = 0.0;
    cout << "How much do you want to save each month? ";
    cin >> deposit;
      cout << deposit << endl;
   
   while (totalSaved <= 1000){ 
      ++ months;
    
      interestEarned = totalSaved * interest;
      
      cout << "Month " << months << ": $" << totalSaved; 
      
      totalSaved = totalSaved + interestEarned;
      
      cout << " deposited " << deposit <<  " interest earned is " << interestEarned << endl;
          
      if (totalSaved >= 1000){
      cout << "It took " << months << " months, and you now have $" << totalSaved << endl;
   }
   }
   return 0;
}

The outcome doesn't add the interest on the first month but waits for the second and messes up total count calculations.
 
Physics news on Phys.org
Re: Computing the number of months to save \$1000

I think your problem is that you begin your calculations in the loop and don't account for initial month. I rewrote some of your program. It will also be easier to read if you put your cout statements outside of your loop so if your input is very small, it wouldn't produce like a thousand couts for each month.
Code:
#include <iostream>
using namespace std;
int main (){
    
    const double interest = 0.01;
    int deposit = 0;
    int months = 0;
    double totalSaved = 0.0;
    double interestEarned = 0.0;
    cout << "How much do you want to save each month? ";
    cin >> deposit;

    // if the deposit is already 1000 aka no looping required
    if (deposit >= 1000){
    cout << "month: " << months << endl;
    cout << "interest you earned is: " << interestEarned << "$" << endl;
    cout << "now you have: " << totalSaved << "$" << endl;
    }
    // initial interest earning calculations
    interestEarned = deposit * interest;
    totalSaved = deposit + interestEarned;
    // looping for total saved
    while (totalSaved <= 1000){
        interestEarned = totalSaved * interest;
        totalSaved = totalSaved + interestEarned;
        months++;
    }
    // couts for how much you earned in how long
    cout << "month: " << months << endl;
    cout << "interest you earned is: " << interestEarned << "$" << endl;
    cout << "now you have: " << totalSaved << "$" << endl;
    return 0;
}
 
Last edited: