Another C++ Related Problem

Discussion in 'OT Technology' started by L Jay, Mar 7, 2009.

  1. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    Alright so I'm working on a program that takes user inputted deposits and then uses that information to calculate trivial things such as interest earned, and average ect.

    Everything was going well till I got to the point in the assignment in which I'm asked to invest the average amount generated from my first loop into a account earning interest.

    The problem I'm having is I'm not sure how to setup the loop to calculate interest.

    Here is the problem I was given:
    "You want to invest in a new account, depositing this average amount
    once each year. The money earns interest, compounded annually, and
    you want to know how much interest will be earned and the final balance."


    Here is the example provided that I'm trying to re-create:

    "Calculate the final balance amount using a current balance of 1000 at 2.5%, you will have at the:

    end of year 1: 1000.00 + (1000.00 * 0.025) which is 1025.00
    end of year 2: 1025.00 + (1025.00 * 0.025) which is 1050.63
    end of year 3: 1050.63 + (1050.63 * 0.025) which is 1076.89 "

    Here is my code for the first part as well as second:

    Code:
    
    #include<iostream>
    #include<iomanip>
    
    using namespace std;
    
        int main()
    
        {
    
            int deposit = 0;
            char choice= 0;
            int count = 0;
            int total = 0;
            char SPACE = ' ';
    
    
    
            cout << "Would you like to make a deposit? Enter Y or N" << endl;
    
            cin >> choice;
    
    
        
            while ( choice == 'Y' || choice == 'y')
            {
                cout << "Enter ammount to be deposited" << endl;
                
                cin >> deposit;
    
                (total = total+deposit);
            
                    count++;
    
                cout << "Would you like to make a deposit? Enter Y or N" << endl;
    
                cin >> choice;
    
            }
    
            float average = (total / count);
    
    
                cout << setw (25) << "Total deposits $ " << total << endl
                     << setw (25) << "Number of deposits : " << count << endl
                     << setw (25) << "Average deposit $ " << average << endl
                     << SPACE << endl;
    
                
                char choice2 =0;
                int years = 0;
                float interest =0;
                float interestRate = (interest * 0.01) ;
                float investamt = average;
                float total2 =(investamt * interestRate);
                int count2 = 0;
                float compinterest = 0;
    
                cout << "Do You want to invest in a new account, depositing this average amount" << endl
                     << "once each year?" << endl;
                    
                    cin >> choice2;
    
                if (choice2 == 'Y' || choice2 == 'y')
    
            {
                    
    
                    cout << "Enter number of years to invest" << endl;
                    
                    cin >> years;
                    
                    cout << "Enter interest rate to be used. (Interest should be entered" << endl
                         <<   "in perecent form. For example 8.5" << endl; 
    
                    cin >> interest;
                    
                    while(count2<=years)
                    {
    
                    float compinterest = pow((1+(interestRate / years)),1*10);
    
                    count2++;
    
                   }
            
    
                cout << compinterest << endl;
                
                
            }
        
        return 0;
    
     
    Last edited: Mar 8, 2009
  2. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
  3. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    I knew about this formula, just so I don't sound like a complete idiot :hs:

    [​IMG]



    Though I'm not sure whether or not using the formula would help in the long run. Reason being I have to compile a list. For investment every year for example if it is done for ten years. Which I think is the reason behind him making me use a loop to preform the calculation.

    Enter number of years to invest: 10 Enter annual interest rate (e.g. 8.5): 8.5

    Annual Table
    ------------
    Year 1 542.50
    Year 2 1131.11
    Year 3 1769.76
    Year 4 2462.69
    Year 5 3214.51
    Year 6 4030.25
    Year 7 4915.32
    Year 8 5875.62
    Year 9 6917.55
    Year 10 8048.04

    Number of years invested: 10
    Interest rate: 0.085
    Amount Invested: 5000.00
    Interest Earned: 3048.04
    Final Balance: 8048.04
     
  4. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Well I'm not really sure why you're having difficulty. You haven't even made an attempt at your loop.
     
  5. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    The reason is that I'm not sure how to run a loop for a given amount of time. I need the loop to run the calculation for how ever many years is inputted, which was my original question :)

    I understand how to make a loop I just am not sure on how I would apply loop logic to this type of problem.

    If I was asked to solve this problem with out any restrictions I would simply of used the compound interest formula.
     
  6. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    You cannot be serious here. You've already demonstrated that you know the syntax of variable declaration, assignment and a where loop. Those are more than enough tools to create a loop to "...run the calculation for how ever many years is inputted..."
     
  7. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    My problem is I don't know how to make a loop run cycles for calculations. I have updated my original post. With my attempt at the loop for the final part. It returns a value of zero so I know its not right.

    Though I think I figured out how to run the loop the given number of cycles that I want it to by using the count2.
    as a way of defining the loop.

    I have a week to get this working though I'm trying to get it done before then. I'd appreciate any help you could offer.
     
  8. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Wrong.

    Your problem is you've never developed the ability to think and reason for yourself. If you have a formula to solve the problem, then you're good or if you have the answer, you'll make an attempt and if you're wrong, you'll correct your work later.

    You have a week and you're already asking for help. Meanwhile you possess ALL of the tools necessary to solve this problem on your own. You're just being lazy.

    If you want a career as a programmer, you're going to have to let go of the idea that the answer lies outside of you or your abilities to reason.

    My suggestion: break down your solution even further. You need to think at a much more granular level. What should happen during each iteration of your loop?

    My other suggestions are to use the code tags around your code and use proper programming style to improve the readability of your code. The code tags are
    Code:
     and [/code!] with the ! removed. 
    Here's another guide:
    http://en.wikipedia.org/wiki/Programming_style
     
  9. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
     
  10. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    I disagree. You just sound lazy and think that making mistakes is a waste of your time. I could be wrong but I used to be the same way and I would say the same things....in reality, I just wanted to be told what to do instead of figuring it out for myself.

    Here's the shocker, you will learn a LOT from going down the wrong road. In fact, you may learn even more than going down the right road.

    Something you'll find when you leave school is that you'll be thrown into problems that you've never seen before and you don't immediately know how to solve.

    What do you think is more appealing to an employer: A person that has to be told exactly how to solve problems or a person that will reason them out for themselves?

    You have a week....sleep on it and try again in the morning.
     
  11. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    I completely understand where you are coming from with basically all of your examples, a majority of where you try and judge my character based on a simple question on how to control a loop.

    At no point did I ask for someone to simply give me the solution to my problem. I was merely looking for a example or text to read regarding what I was trying to solve.
     
    Last edited: Mar 8, 2009
  12. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    Funny that you think this is some how different than looking up answers in the back of a book.

    I said before, I could be wrong. If I misjudged you, I apologize but don't think I've taken the time to respond because I enjoy torquing your brain or somehow get a perverse thrill out of being vague. I don't....what I do believe is that it's far more helpful for you to reason this out on your own....something which you clearly don't want to do. I'm done....have a good night.
     
  13. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    I guess I should re-phrase my question as well, my problem is not the loop but putting the actual compound interest formula into C++

    So far I have

    [​IMG]

    which is my formula

    P= the average investment calculated above which will be compounded annually. Used in my program by the name average.

    R= the interest rate in decimal form which would be my variable interestRate
    M= Number of times it is compounded, I used my variable years because it is only compounded once a year. I'm not sure whether or not that is correct

    N= Total amount of time in which I will be compounding. In my program I'm testing with a value of 10.

    No matter how I setup the formula I always get a value of 0 being outputted which is the value I assigned when I initialized the variable.




    Code:
    float compinterest = pow((average)*(1+(interestRate / years),years;
    
     
  14. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    I hope I didn't start some sort of online bickering because that was not my intention.

    I can see where you can draw the conclusion that I came here looking for someone to simply type out the code for me. Leaving the only thing left for me to do is copy and paste it into my compiler and call it a day. That was not my intention. I hoped when I pasted my code into my original post that it would show that I indeed did try it on my own and that someone would be able to point me in the right direction.

    I'm sorry if I came off the wrong way. :)
     
  15. Coottie

    Coottie BOOMER......SOONER OT Supporter

    Joined:
    Jun 6, 2006
    Messages:
    32,407
    Likes Received:
    0
    Location:
    OKC
    No worries mate....good luck.
     
  16. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    Thanks,
     
  17. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    First off, I want to say thanks to Cootie for making me work on this a little more rather than turning to OT for help, I think I have made a real stride forward towards getting the actual solution to the problem. Except I'm having a bit of a problem with my variables more specifically investamt, compinterest, and average.

    I'm not sure why they are still being assigned my initialization value when I have specific instructions within the program to update their values but for some reason they still appear as 0.

    Also on a un-related note, why would Visual Studio 2008 not display my variables as well as their values whe running the debugger. The Locals as well as Auto windows remain blank.

    Thanks,

    Code:
    #include<iostream>
    #include<iomanip>
    #include <cmath>
    
    using namespace std;
    
        int main()
    
        {
    
            int deposit = 0;
            char choice= 0;
            int count = 0;
            int total = 0;
            char SPACE = ' ';
    
    
    
            cout << "Would you like to make a deposit? Enter Y or N" << endl;
    
            cin >> choice;
    
    
        
            while ( choice == 'Y' || choice == 'y')
            {
                cout << "Enter ammount to be deposited" << endl;
                
                cin >> deposit;
    
                (total = total+deposit);
            
                    count++;
    
                cout << "Would you like to make a deposit? Enter Y or N" << endl;
    
                cin >> choice;
    
            }
    
            float average = (total / count);
    
    
                cout << setw (25) << "Total deposits $ " << total << endl
                     << setw (25) << "Number of deposits : " << count << endl
                     << setw (25) << "Average deposit $ " << average << endl
                     << SPACE << endl;
    
                char choice2 =0;
                int years = 0;
                float interest =0;
                float interestrate =(interest /100);
                float investamt = average;
                int count2 = 0;
                float compinterest=0;
        
    
                cout << "Do You want to invest in a new account, depositing this average amount" << endl
                     << "once each year?" << endl;
                    
                    cin >> choice2;
    
                if (choice2 == 'Y' || choice2 == 'y')
    
                {
                    
    
                    cout << "Enter number of years to invest" << endl;
                    
                    cin >> years;
                    
                    cout << "Enter interest rate to be used. (Interest should be entered" << endl
                         <<   "in perecent form. For example 8.5" << endl; 
    
                    cin >> interest;
                }
                    while((count2<=years)-1)
                    {
    
                    float compinterest = investamt * pow( (1 + interestrate), years );
    
    
                    count2++;
    
                   }
            
    
                cout << compinterest 
                    <<endl << investamt
                    <<endl<< count2 
                    <<endl << years
                    <<endl << interestrate <<endl;
                
                
            
        
        return 0;
    }
    
     
  18. antiyou

    antiyou OT Supporter

    Joined:
    Jul 13, 2005
    Messages:
    25,295
    Likes Received:
    0
    Location:
    in ur base
    The code for the interest loop implies that you aren't thinking of the problem correctly so hopefully this will point you in the proper direction.

    given this information (provided I understand it correctly)



    What you want to do inside of that loop is compute and print the value at the end of the year for each year and print it in the specified format. So one iteration of your loop should print

    end of year var1: var2 + (var3 * var4) which is var5

    Figure out which variables in your current code correspond to var1 - var4 and figure out how to compute var5. Then determine which if any need to be updated and how.
     
  19. Peyomp

    Peyomp New Member

    Joined:
    Jan 11, 2002
    Messages:
    14,017
    Likes Received:
    0
    Are you asking how to control a counter for a limited number of loops?

    DECLARE counter;

    while COUNTER < limit
    {

    DO STUFF;

    INCREMENT COUNTER;
    }

    Is that what you were asking?
     
  20. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    Code:
                    float compinterest=0;
                    ...
                    while((count2<=years)-1)
                    {
    
                    float compinterest = investamt * pow( (1 + interestrate), years );
    
    
                    count2++;
    
                   }
    
    Scope.
    (also, check the parenthesis in your while statement)
     
  21. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    Nope.

    Thanks though.
     
  22. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    I ended up re writing the hole second part. It was just too messy and un organized for me to fix.
     
  23. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    But, do you understand your mistake?
     
  24. L Jay

    L Jay OT Supporter

    Joined:
    Apr 4, 2007
    Messages:
    6,748
    Likes Received:
    0
    Location:
    Chicago, IL
    I've been working on what you are saving.

    I think I'm getting their but I keep running into the same problem.

    My loop is

    Code:
    
        while((years<= count2)-1)        
            
            {
            float interestDec = (interestPerc / 100);
    
            float    interestEarned1 = (investamt * interestDec)+ (investamt);
            
        
    
            
            count2++;
            
            
            
                cout << interestEarned1 << endl;
            }
            
    
    What I'm trying to figure out is how to assign the value that it calculated for interestEarned1 to another variable and make it keep running and listing the values that it calculates for how ever many years I choose.

    Hope that makes sense. I think it is a really easy problem with a solution that I just can't see right now. :hs:
     
  25. Krakerjak

    Krakerjak Active Member

    Joined:
    Jul 7, 2003
    Messages:
    8,288
    Likes Received:
    0
    Location:
    Edmonton eh
    I don't understand why you are declaring variables within the loop.
    You should be declaring them beforehand and using them within the loop
     

Share This Page