# having a real tough time with functions..

• 04-30-2007
simstim99
having a real tough time with functions..
I'm not sure why but some of the variable don't seem to pick up values. I'm having a hard time with functions, can't seem to grasp how pass values back and forth from main to the functions. sorry for the mess of a code and thanks for any help.

Code:

```#include <iostream> #include <iomanip> using namespace std; // function one, getting input void input(double &, double &, double &); // function two, calculate interest rate and exits if the payment isn't enough void interestRate (double &, double &, double &, double &, double &); // function three, the main working function, calculates the total interest, the final payments and the amount of months it takes.  void calculations (int &, double &, double &, double &, double &, double &, double &); // function four, display the results void results (int &, double &, double &); int main()  // all main does is call on the functions {         double money, interest, desiredPayment, realRate, totalInterest, finalPayment, monthlyInterest, minPay;         int months;                         input(money, interest, desiredPayment);                  interestRate(realRate, money, interest, desiredPayment, minPay);         calculations(months, monthlyInterest, totalInterest, money, realRate, finalPayment, desiredPayment);         results(months, totalInterest, finalPayment);         return 0; } // this function gets the input from the user and puts it into reference variables. void input (double &money, double &interest, double &desiredPayment) {           cout << "How much do you want to borrow? \$" ;         cin >> money ;                        while (money <= 0)                 {                                cout << "You must enter a positive number!\n";                         cout << "How much do you want to borrow? \$";                         cin >> money ;                 }                 cout << "What is the annual interest rate expressed as a percent? ";                 cin >> interest;                 while (interest <= 0)                 {                         cout << "You must enter a positive number!\n";                         cout << "What is the annual interest rate expressed as a percent? ";                         cin >> interest;                 }                 cout << "What is the monthly payment amount? \$";                 cin >> desiredPayment;                 while (desiredPayment <= 0)                 {                         cout << "You must enter a positive number!\n";                         cout << "What is the monthly payment amount? \$";                         cin >> desiredPayment;                 } } // this function calculates the interest rate, and notifies if the minimum monthly payment isn't sufficient. void interestRate (double &desiredPayment, double &money, double &interest, double &realRate, double &minPay ) {         realRate = money *(interest/1200.0); // for some reasong realRate stays 0                 if (desiredPayment <= realRate)         {                 minPay = realRate + 1 ;                 cout << fixed << showpoint << setprecision(2);                 cout << "You must make  payments of at least \$" << minPay << endl;                 cout << "Because your monthly interest is \$" << realRate;                 cout << endl;                 cout << endl;                 cout << "** Don't get overwhelmed with debt! **\n\n";                 exit(0);         }         else                 realRate = money *(interest/1200.0);                 } // This is the workhorse function, it calculates how many months it will take to pay off the loan, // what the final payment will be, // how much interest was payed over time, void calculations (int &months, double &monthlyInterest, double &totalInterest, double &money, double &realRate, double &finalPayment, double &desiredPayment) {         months = 1 ;                 while (desiredPayment <= money)                 {                         monthlyInterest = money * realRate ;                         totalInterest = monthlyInterest + totalInterest ;                         money = money - desiredPayment ;                         months ++ ;                 }                 money = finalPayment ; } // This function displays the results void results (int &months, double &totalInterest, double &finalPayment) {         cout << fixed << showpoint << setprecision(2);         cout << "Your debt will be paid off after "<< months << "months, with a final payment of just " << finalPayment ;         cout << " The total amount of interest you will pay during that time is " << totalInterest << "\n\n";         cout << "** Don't get overwhelmed with debt! **\n\n"; }```
• 04-30-2007
notice the parameters you pass when you call interestRate:
Code:

`interestRate(realRate, money, interest, desiredPayment, minPay);`
and notice how the parameters are defined in the function:
Code:

`void interestRate (double &desiredPayment, double &money, double &interest, double &realRate, double &minPay )`
if after fixing this you still have a problem, check to see if you did something similar in another function.
• 04-30-2007
simstim99
do you mean that I need to correct their order? or that I need to put & in front of them?
• 04-30-2007
Shapper
fix the order
• 04-30-2007
yes, you need the correct order.

take a look at this simple program:
Code:

```#include <iostream> using namespace std; void myFunc(int a, int b) {     cout << "a = " << a << endl;     cout << "b = " << b << endl << endl; } int main() {     int a = 1;     int b = 2;     myFunc(a,b);     myFunc(b,a);   return 0;  }```
when you call the function, the first variable that you pass will be accepted as the first parameter in the funcion, etc. it wont 'match' the variable names.

hope this helps.
• 04-30-2007
brewbuck
Quote:

Originally Posted by simstim99
do you mean that I need to correct their order? or that I need to put & in front of them?

Do you understand what the '&' is doing? If not, you probably don't need it.
• 04-30-2007
simstim99
it worked, thanks for your help!!!

also, I understand what the "&" means but thanks for your input as well.