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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top