having a real tough time with functions..

This is a discussion on having a real tough time with functions.. within the C++ Programming forums, part of the General Programming Boards category; I'm not sure why but some of the variable don't seem to pick up values. I'm having a hard time ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    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";
    }

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    do you mean that I need to correct their order? or that I need to put & in front of them?

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    fix the order

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    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.
    Last edited by nadroj; 04-30-2007 at 01:25 AM.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,270
    Quote Originally Posted by simstim99 View Post
    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.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    it worked, thanks for your help!!!

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Running Debug in Real Time, possible?
    By pliang in forum Game Programming
    Replies: 4
    Last Post: 04-21-2005, 08:29 AM
  2. Real time clock
    By caduardo21 in forum C++ Programming
    Replies: 5
    Last Post: 03-19-2005, 12:22 PM
  3. Web script: real time clock
    By alphaoide in forum Tech Board
    Replies: 6
    Last Post: 04-05-2004, 07:32 PM
  4. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM
  5. time functions
    By sw9830 in forum C Programming
    Replies: 1
    Last Post: 02-28-2003, 02:55 PM

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