newbie question

This is a discussion on newbie question within the C++ Programming forums, part of the General Programming Boards category; This is a two part question: 1st, How do I use a negative value in an equation? I tried just ...

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    Smile newbie question

    This is a two part question: 1st, How do I use a negative value in an equation? I tried just putting a "-" in front of the number, but my calculations are not coming out correctly. 2nd, My error could be related to "pow"... I am using an equation for each argument, and I'm wondering if that might be what is throwing the whole thing off... Any help would be greatly appreciated!!! Thank you!!

    okay, here's the code:
    Code:
    {
            
            float L;                                                        //amount of loan
            float R;                                                        //annual interest rate
            float r = (R * 0.01) / 12;                                      //monthly interest rate
            int i;                                                          //length of loan in years
            double P = L * (r / 1 - pow( (1 + r), (-i * 12) ) );              //monthly payment
            double total = P * (i * 12);                                    //total amount paid to bank
    
            cout << setw(7);
            cout << setiosflags(ios::fixed | ios::showpoint);
            cout << setprecision(2);
            cout << "\nEnter the loan amount: ";
            cin >> L;
            cout << "\nEnter the annual interest rate: ";
            cin >> R;
            cout << "\nEnter the length of the loan in years: ";
            cin >> i;
    
            cout << "\n\nLoan: $" << L << endl;
            cout << "Interest-rate: " << R << " %\n";
            cout << "Years: " << i << endl;
            cout << "Monthly Payment: $" << P << endl;
            cout << "\nTotal-paid: $" << total << endl;
    }
    Last edited by antheia; 07-18-2003 at 06:59 AM.

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    edited original post to include code...

    Thanks for checking out my question. I edited my first post and included the code...

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    You are trying to initialise variables with other variables who's value you have not read yet, thus they maybe zero, or rubbish depending on your compiler. Instead of...
    Code:
    {
            
            float L;                                                        //amount of loan
            float R;                                                        //annual interest rate
            float r = (R * 0.01) / 12;                                      //monthly interest rate
            int i;                                                          //length of loan in years
            double P = L * (r / 1 - pow( (1 + r), (-i * 12) ) );              //monthly payment
            double total = P * (i * 12);                                    //total amount paid to bank
    
            cout << setw(7);
            cout << setiosflags(ios::fixed | ios::showpoint);
            cout << setprecision(2);
            cout << "\nEnter the loan amount: ";
            cin >> L;
            cout << "\nEnter the annual interest rate: ";
            cin >> R;
            cout << "\nEnter the length of the loan in years: ";
            cin >> i;
    
            cout << "\n\nLoan: $" << L << endl;
            cout << "Interest-rate: " << R << " %\n";
            cout << "Years: " << i << endl;
            cout << "Monthly Payment: $" << P << endl;
            cout << "\nTotal-paid: $" << total << endl;
    }
    ... use...
    Code:
    {
            
            float L;                                                        //amount of loan
            float R;                                                        //annual interest rate
            float r;                    //monthly interest rate
            int i;                                                          //length of loan in years
            double P = L * (r / 1 - pow( (1 + r), (-i * 12) ) );              //monthly payment
            double total = P * (i * 12);                                    //total amount paid to bank
    
            cout << setw(7);
            cout << setiosflags(ios::fixed | ios::showpoint);
            cout << setprecision(2);
            cout << "\nEnter the loan amount: ";
            cin >> L;
            cout << "\nEnter the annual interest rate: ";
            cin >> R;
    
            r = (R * 0.01) / 12;
    
            cout << "\nEnter the length of the loan in years: ";
            cin >> i;
    
            cout << "\n\nLoan: $" << L << endl;
            cout << "Interest-rate: " << R << " %\n";
            cout << "Years: " << i << endl;
            cout << "Monthly Payment: $" << P << endl;
            cout << "\nTotal-paid: $" << total << endl;
    }
    ... for example. There are others.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    4
    Thanks Adrian! I moved the variables r, P, and total down after the input, and the output is much better! Now I just have to figure out what I did wrong with my equation because even though the result finally shows up as a value, it's not the correct value...

    here's the updated code: (I'm working on simplifying the equation)
    Code:
    {
    
    	float L;								//amount of loan
    	float R;								//annual interest rate
    	int i;								//length of loan in years
    
    	cout << setw(7);
    	cout << setiosflags(ios::fixed | ios::showpoint);
    	cout << setprecision(2);
    	cout << "\nEnter the loan amount: ";
    	cin >> L;
    	cout << "\nEnter the annual interest rate: ";
    	cin >> R;
    	cout << "\nEnter the length of the loan in years: ";
    	cin >> i;
    
    
    	int m = i * 12;							//calculates length of loan in months
    	float r = (R * 0.01) / 12;					//calculates monthly interest rate
    	double P = (r / 1 - pow( ++r, -m ) ) * L;			//calculates monthly payment    
    	double total = P * (i * 12);					//calculates total amount paid to bank
    
    
    	cout << "\n\nLoan: $" << L << endl;
    	cout << "Interest-rate: " << R << " %\n";
    	cout << "Years: " << i << endl;
    	cout << "Monthly Payment: $" << P << endl;
    	cout << "\nTotal-paid: $" << total << endl;
    
    
    }
    Last edited by antheia; 07-18-2003 at 08:46 AM.

  5. #5
    Registered User
    Join Date
    Jul 2003
    Posts
    4

    update: it's fixed!! :D

    I finally figured out what was wrong... I just broke down the equation into smaller parts. It's working great!! Thank you!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stupid Newbie question
    By TimL in forum C++ Programming
    Replies: 4
    Last Post: 07-22-2008, 05:43 AM
  2. C prog newbie question
    By Draginzuzu in forum C Programming
    Replies: 1
    Last Post: 02-03-2003, 06:45 PM
  3. a stupid question from a newbie
    By newcomer in forum C++ Programming
    Replies: 4
    Last Post: 01-11-2003, 04:38 PM
  4. confusion with integers (newbie question)
    By imortal in forum C Programming
    Replies: 7
    Last Post: 12-06-2002, 04:09 PM
  5. newbie class templates question
    By daysleeper in forum C++ Programming
    Replies: 2
    Last Post: 09-18-2001, 10:50 AM

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