Logic errors...

This is a discussion on Logic errors... within the C++ Programming forums, part of the General Programming Boards category; Here is a copy of my program: Code: #include <iostream> using namespace std; int main() { double initial_amount; // beginning ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    29

    Logic errors...

    Here is a copy of my program:

    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    
    {
    double initial_amount;  // beginning amount $240,000
    double interest_rate6;  // 6% interest applied
    double interest_rate7;   // 7% interest applied
    int monthly_payment;     // amount paid each month $1500
    int payment_period;  // 12 months per year
    int total_months; // total number of months to pay for house
    double down_payment;     //$40,000
    double loan_balance; // amount currently owed
    double payment_amount;  // how much has been paid total
    double years; // years
    double left_over; //amount left over
    
    	cout << "Enter the initial price of the house (eg. xxxxxx.xx): ";
    	cin >> initial_amount;
            cout << "Enter the down payment for the house (eg. xxxxx.xx): ";
            cin >> down_payment;
    	cout << "Enter the annual interest rate for the first 3 years in decimal form (6% equals 1.06 eg. x.xx): ";
    	cin >> interest_rate6;
    	cout << "Enter the annual interest rate following the first 3 years in decimal form (7% equals 1.07 eg. x.xx): ";
    	cin >>  interest_rate7;
            cout << "Enter your monthly payment (eg. xxxx.xx): ";
            cin >> monthly_payment;
    	cout << "Enter the amount of annual payment periods (if once a month, press 12): ";
            cin >> payment_period;
    
    loan_balance = (initial_amount - down_payment) - (monthly_payment*payment_period);
    
    years = payment_period*12;
    
    while (payment_amount > loan_balance)
    {
    if (years > 3)
    {
            loan_balance = (payment_amount*interest_rate6) - loan_balance;
    }
    else
            loan_balance = (loan_balance*interest_rate7) - loan_balance;
    }
    
    left_over = initial_amount - payment_amount;
    
    total_months = (loan_balance/monthly_payment)/12;
    
            cout.setf(ios::fixed);
            cout.setf(ios::showpoint);
            cout.precision(2);
            
            cout << "It will take you " << total_months <<" months to purchase the house." << endl;
            cout << "The final monthly payment for your house is $" << left_over << endl;
    
            return 0;
    }
    In the final output it shows that the total months it would take for the person to purchase the house would be 109,202 and the left over value in the final month is $240,000 (the original price of the house)

    I must be going crazy, but I cannot put my finger on these simple logical errors. Thanks!

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> while (payment_amount > loan_balance)
    At this point in the code, payment_amount is uninitialized (your compiler warnings should have told you that).

    Even if you initialize it, the logic of that loop is off. Does it make sense to run the loop while the payment amount is greater than the loan balance?

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    7
    base on my observation,

    years = payment_period*12; should be years = payment_period/12;
    b'coz if you enter 12 in this line.
    Code:
    cout << "Enter the amount of annual payment periods (if once a month, press 12): ";
    cin >> payment_period;
    the total would be 144 years.

    im not sure about the leftover but i think yoiu have no computation for payment_amount.

    Code:
    total_months = (loan_balance/monthly_payment)/12;
    ex.
    loan_balance = 1000
    monthly payment = 100

    1000/100=10 months (no need to divide it from 12)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. global namespace errors
    By stubaan in forum C++ Programming
    Replies: 9
    Last Post: 04-02-2008, 03:11 PM
  2. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  3. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 12:43 PM

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