3 Replies - 161 Views - Last Post: 07 February 2012 - 09:16 AM Rate Topic: -----

Topic Sponsor:

#1 NerfherderR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-February 12

"inf" error when computing payment

Posted 07 February 2012 - 09:01 AM

I'm new here, but still a beginner in C++. Posted is the program I'm working on.

Assignment:
"In this assignment, you are to write a program that will compute a monthly payment. You are to have, at least, 1 function ( other than main() ) in your program."

http://www.comsc.uco...se-payment.html

Problem:
I keep getting "inf" when I run the program. I mean, it's obvious that the main function is okay since it does ask for the input and displays something, so there must be a problem with the calculation function, but I'm not sure what the problem is.

#include <iostream>
#include <math.h>
using namespace std;
float calculation(float p, float yir, float y)
   {
      float mir, payment, a, b, c, d, e, f;

      mir = yir / 1200;
      a = 1 + mir;
      b = 1 / a;
      c = 12 * y;
      d = pow(b, c);
      e = 1 - d;
      f = p * mir;
      payment = f / e;

      return payment;
   }
int main()
   {
      float p, yir, y, x;
      x = calculation(p, yir, y);

      cout << "Input principal amount" << endl;
      cin >> p;
      cout << "Input yearly interest rate" << endl;
      cin >> yir;
      cout << "Input term in years" << endl;
      cin >> y;

      cout << x << endl;
   }


Any help would be grateful.

Is This A Good Question/Topic? 0
  • +

Replies To: "inf" error when computing payment

#2 ishkabible  Icon User is online

  • spelling expert
  • member icon



Reputation: 1141
  • View blog
  • Posts: 4,781
  • Joined: 03-August 09

Re: "inf" error when computing payment

Posted 07 February 2012 - 09:13 AM

Quote

x = calculation(p, yir, y);


your passing uninitialized variables to your function. why did you place that line there? the varibles p, yir, and y need to be initialized before you use them. if you placed that after you request input from the user then these variables will have defined values and you can pass them to your function.
Was This Post Helpful? 1
  • +
  • -

#3 jimblumberg  Icon User is offline

  • member icon

Reputation: 1892
  • View blog
  • Posts: 5,681
  • Joined: 25-December 09

Re: "inf" error when computing payment

Posted 07 February 2012 - 09:13 AM

You are calling your function before you have anything to calculate. Wouldn't it be better to call this function after you have the required information?

Also you should be using descriptive variable and function names. What calculation is calculation preforming? In your function parameters, what do the variables p, yir, y actually hold? In that function you have several variables (a,b,c,etc) why not give these variable names a name that shows what value they are holding?

Jim
Was This Post Helpful? 0
  • +
  • -

#4 NerfherderR  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-February 12

Re: "inf" error when computing payment

Posted 07 February 2012 - 09:16 AM

Thanks. I was thinking that just before I posted it, but stupid me decided not to try it and post anyway..

Thanks though. 'preciate it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1