Need a second set of eyes.

Discussion in 'OT Technology' started by Disguy, Dec 5, 2009.

  1. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,060
    Likes Received:
    0
    Turns out it was a typo.

    I'm doing this simple project to teach myself c++ and I can't figure out where I'm doing something wrong. Everything works technically but for some reason I'm getting an incorrect answer.

    Its an interest calculator. Interest is paid annually.

    Get deposit, get rate, calc the amounts for year 1, 2 and 3.

    I enter in 100 for the amount, interest rate 1 (which changes to .01%).

    I get 1101, 2102.01, 3103.03 for the years.

    Somehow the program is adding 1000 to the amount each year. I don't see where I made a mistake.


    Maybe I'm tired or whatever but I don't see it.

    Code:
    {
        int deposit;
        double rate;
        double year1 =0 , year2 =0 , year3 =0;
        int good =1;
        
        do
        {
       
        cout << "Enter deposit amount:" ;
        cin >> deposit;
        cout << "\nEnter Annual Interest Rate: ";
        cin >> rate;
        
        cout << deposit;
        cout << " ";
        cout << rate;
            cout << " ";
        cout << year1;
            cout << " ";
        cout << year2;
            cout << " ";
        cout << year3;
        
                cout << "\n ";    
        rate = 1+(rate/100);
        cout << rate;
        rate = rate;
        cout << " ";
        cout << rate;
        year1 = deposit*rate;
        year2 = year1*rate;
        year3 = year2*rate;
        
        cout << "\nBalence after Year 1";
        cout << year1;
        cout << "\nBalence after Year 2";    
        cout << year2;
        cout << "\nBalence after Year 3";
        cout << year3;
        cout << "\n";
        
        cout << "Good?";
    
        cin >> good;
        } while (good == 1);
     
    Last edited: Dec 5, 2009
  2. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    Code:
    cout << rate;
    rate = rate;
    cout << " ";
    cout << rate;
    
    what is that supposed to be doing..
     
  3. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,060
    Likes Received:
    0
    I was printing to the screen to see if where the error occurs. I had something else there before but took it out.
     
  4. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    hahahahhaa
     
  5. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    I think I figured it out
     
  6. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    the calculated numbers are fine. you're reading the 1, 2, and 3 from this

    Code:
    cout << "\nBalence after Year [color=red]1[/color]";
    cout << year1;
    cout << "\nBalence after Year [color=red]2[/color]";    
    cout << year2;
    cout << "\nBalence after Year [color=red]3[/color]";
    cout << year3;
    
     
  7. 5Gen_Prelude

    5Gen_Prelude There might not be an "I" in the word "Team", but

    Joined:
    Mar 14, 2000
    Messages:
    14,519
    Likes Received:
    1
    Location:
    Vancouver, BC, CANADA
  8. Disguy

    Disguy Batman > Moon Knight OT Supporter

    Joined:
    Oct 26, 2004
    Messages:
    1,060
    Likes Received:
    0
    that's not even funny... I missed a goddamn space.
     
  9. deusexaethera

    deusexaethera OT Supporter

    Joined:
    Jan 27, 2005
    Messages:
    19,712
    Likes Received:
    0
    Actually it's incredibly funny. :rofl:

    Don't worry, you're only the billionth programmer to fuck up on something simple like this. It's always the tiny bugs that are hard to catch.
     
  10. HY

    HY New Member

    Joined:
    Dec 10, 2006
    Messages:
    57
    Likes Received:
    0
    your logic is right, you must not be running what you are compiling.
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
        int deposit = 100;
        double year1, year2, year3, rate =1; 
        rate = 1+(rate/100);
        year1 = deposit*rate;
        year2 = year1*rate;
        year3 = year2*rate;
        cout << "year1\t" << year1 << "\tYear2\t" << year2 << "\tyear3\t" << year3 << endl;
    }
    
    or
    Code:
    #include <iostream>
    #include <math.h>
    using namespace std;
    int main()
    {
        int deposit = 100;
        double rate = 1;
        rate = 1+(rate/100);
        for(int year=1;year <= 3;year++)
            cout << "Year " << year << ":\t" << deposit* pow(rate,year) << endl;
    }
    
     
  11. White Stormy

    White Stormy Take that, subspace!

    Joined:
    Sep 17, 2002
    Messages:
    85,486
    Likes Received:
    69
    Location:
    Sparkopolis
    should include a colon as well
     

Share This Page