Thread: Help with a small equation and loop

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    25

    Help with a small equation and loop

    I need to calculate how much time it will take to pay off a credit card. I'll take any help.


    Code:
    #include <iostream>
    
    using namespace std;
    
    
    
    int main()
    
    
    {
    
    	 float bal,ir,payment,year, newbal,newbal2;
    
    
    cout << "Enter credit card Balance" << endl;
    cin >>bal;
    
    cout <<"Enter Intrest rate" << endl;
    cin >>ir;
    
    cout << "Enter years:" << endl;
    cin>> year;
    for( newbal =1 ; newbal <=12 ; newbal++)
    {
    
    // Formula
    payment = ((bal*ir)/year)+10; 
    
    cout << " Payment each month:"<<  payment<< endl;
    
    
    
    newbal = bal + ir - payment;
    
    
    
    cout << newbal<<endl;
    newbal2 = (newbal-(payment*12)+ir);
    cout << newbal2<< endl;
    
    
    cout <<"total amount months to pay of debt " <<newbal2<< endl;
    
    
    
    	
    }
    
    system("pause");
    	return 0;
    Last edited by Zerohero11; 09-30-2005 at 11:37 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Think about what controls your loop, and what calculates a new balance

    > for( newbal =1 ; newbal <=12 ; newbal++)
    > newbal = bal + ir - payment;
    These are inconsistent.
    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.

  3. #3
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Well, for starters. Your math is wrong.

    Code:
    newbal = bal + ir - payment;
    Will not calculate the new balance properly.

    Interest rate is a %. 6% of 100 is different that 6% of 1000.

    So, divide you interest rate by one hundred.
    Then the interest acquired is the balance times that interest rate divided by 100.

    example:

    5.98765% interest of 123456 dollars.
    5.98765 / 100 = 0.0598765
    123456 + (123456 * 0.0598765) = 130848.11


    Also, you can if you chose to do this:

    Code:
     newbal2 = (newbal-(payment*12)+ir);
    Is fine, but you do NOT have to have a new variable every time you change something.
    The following is just as valid:

    Code:
     newbal = (newbal-(payment*12)+ir);
    There are perfectly legit reasons to use the method you are using, I am just letting you know this incase you do not know.

    Ok, now onto your program.
    Play with the math on some paper and pencil and calculator. There is a simpler way of doing this than the method you are trying.

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    25
    Alright, well I figured I need to change the loop, and x the percent by 100 so far. Still working on everything else.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in evaluating postfix
    By P4R4N01D in forum C Programming
    Replies: 23
    Last Post: 12-24-2008, 10:47 PM
  2. Please check this loop
    By Daesom in forum C++ Programming
    Replies: 13
    Last Post: 11-02-2006, 01:52 AM
  3. Nested Loop with add-on Equation..?? Seems Impossible!
    By AssistMe in forum C Programming
    Replies: 3
    Last Post: 03-11-2005, 12:28 PM
  4. Nested loop frustration
    By caroundw5h in forum C Programming
    Replies: 14
    Last Post: 03-15-2004, 09:45 PM
  5. iterative equation function
    By whatman in forum C Programming
    Replies: 6
    Last Post: 07-31-2003, 08:43 AM