this is my code which is not performing the for loop!

This is a discussion on this is my code which is not performing the for loop! within the C++ Programming forums, part of the General Programming Boards category; 14. Write a program that calculates how much money you'll end up with if you invest an amount of money ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    8

    this is my code which is not performing the for loop!

    14. Write a program that calculates how much money you'll end up with if you invest an amount of money at a fixed interest rate, compounded yearly. Have the user furnish the initial amount, the number of years, and the yearly interest rate in percent. Some interaction with the program might look like this:
    15. Enter initial amount: 3000
    16. Enter number of years: 10
    17. Enter interest rate (percent per year): 5.5
    At the end of 10 years, you will have 5124.43 dollars.
    At the end of the first year you have 3000 + (3000 * 0.055), which is 3165. At the end of the second year you have 3165 + (3165 * 0.055), which is 3339.08. Do this as many times as there are years. A for loop makes the calculation easy.
    Code:
    #include<iostream.h>
    #include<conio.h>
    int main()
    {
    clrscr();
    int  i,y;
    char ch;
    float r,ans,n;
    cout<<"enter the initial amount:";
    cin>>i;
    cout<<"enter the no of years:";
    cin>>y;
    cout<<"enter the rate of interest (percent per year):";
    cin>>r;
    
    for(ans=1;ans<=i;ans++)
     {
    ans=i+(i*r/100);
    cout<<"at the end of the year the balace willbe:"<<ans;
    cout<<"do you want to continue(y/n)";
    cin>>ch;
      }
    cout<<endl;
    getch();
    return 0;
    }
    Last edited by Salem; 12-09-2006 at 02:17 PM. Reason: fixed code tags - but not the indentation

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    1) Wrong forum
    2) Maybe some, say, NEWLINES!!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Moved to c++ forum
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > this is my code which is not performing the for loop!
    Because ans is being used to control the loop AND perform the calculations.

    Besides, shouldn't you be looping for the number of years, not the initial amount.

    You know, using meaningful variables like initialAmount and years rather than cryptic single letters would make the code read all wrong from the outset.

    for(ans=1;ans<=initialAmount ;ans++)
    would simply make no sense as you wrote it.

    for(ans=1;ans<=years;ans++)
    would seem far more plausable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    i will try this out thanks a lot!

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    8
    for(ans=1;ans<=years;ans++)
    i have tried this too but not working.actually the loop is not repeating

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Post all of your latest code, not just random single lines.

    Are you STILL using ans inside the loop for something else as well?
    Because that will mess you up.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    why do you mess with "ans" inside the loop?


    Code:
    ans=i+(i*r/100)

  9. #9
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    I guess that is what you are looking for:

    Code:
    int main()
    {
    
    int  i,y,x;
    char ch;
    float r,ans,n;
    cout<<"enter the initial amount:";
    cin>>i;
    cout<<"enter the no of years:";
    cin>>y;
    cout<<"enter the rate of interest (percent per year):";
    cin>>r;
    ans=i;
    for(x=1;x<=y;x++)
    {
    
    ans=ans+(ans*r/100);
    cout<<"at the end of the year the balace will be:"<<ans << endl;
    
    }
    cout<<endl;
    
    return 0;
    }

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    6
    My suggestion

    Name your variables something useful like: iamount,years,interest,balance

    This is going to help you find errors in your code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 11:38 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21