MHB Computing the number of months to save $1000

  • Thread starter Thread starter MMOne
  • Start date Start date
  • Tags Tags
    Computing
AI Thread Summary
The discussion revolves around a programming challenge where a beginner is trying to calculate how many months it will take to save $1000 with a monthly deposit and a 1% interest rate. The initial code provided fails to account for interest earned in the first month and produces excessive output for each month. Suggestions for improvement include restructuring the code to calculate initial interest and total savings before entering the loop, as well as moving output statements outside the loop to reduce clutter. The revised code correctly handles the calculations and provides a clearer output, ensuring that if the deposit is already $1000 or more, it immediately reflects that without unnecessary iterations.
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.
 
Technology 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:
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top