C++ Pass by Ref In Functions

How to pass by reference in functions

Page 1 of 1

2 Replies - 793 Views - Last Post: 02 November 2009 - 04:32 AM Rate Topic: -----

#1 DrolArumil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-November 09

C++ Pass by Ref In Functions

Posted 02 November 2009 - 02:36 AM

I am really stuck and cant seem to find an answer online that I can understand. I I need to declare my prototypes, call them in main, get the information from user, and assign it to the function (not sure how to word it) so i can "return" multipule data types at a time. Yes this is homework I feel like im getting close (I missed this part of class due to work)

if anyone can help please do i been working on this for houuurs maybe just show me how a proper program would look like with what im trying to do or something. also now I keep getting an error that says "to few arguments in getData(double&, double&) to function.

Thank you!

#include <iostream>
using namespace std;

void getData(double& rate, double& loan);
void calcInterest(double &rate, double &loan, double &monthly);
void displayResult(double &rate, double &loan, double &monthly);



int main()
{
	double rate;
	double loan;
	
	getData();
	calcInterest();
	displayResult();

	return 0;

}

void getData(double& rate, double& loan)
{
	rate = 0;
	loan = 0;
   	cout << "Please Enter The Annual Interest Rate: ";
	cin >> rate;
	cout << "\nPlease Enter The Loan Ammount: ";
	cin >> loan;
	cout << endl;
}

void calcInterest(double &rate, double &loan, double &monthly)
{
	monthly = ((rate/100) * loan)/12;
}

void displayResult(double& rate, double& loan, double& monthly)
{
	cout << "The First Months interest on the loan amount of " << loan << " at " << rate << " equals " << monthly;
}

	
	


Heeeelp heres my errors
/home/chaos/Desktop/C++/hw6d/main.cpp||In function ‘int main()’:|
/home/chaos/Desktop/C++/hw6d/main.cpp|4|error: too few arguments to function ‘void getData(double&, double&)’|
/home/chaos/Desktop/C++/hw6d/main.cpp|14|error: at this point in file|
/home/chaos/Desktop/C++/hw6d/main.cpp|5|error: too few arguments to function ‘void calcInterest(double&, double&, double&)’|
/home/chaos/Desktop/C++/hw6d/main.cpp|15|error: at this point in file|
/home/chaos/Desktop/C++/hw6d/main.cpp|6|error: too few arguments to function ‘void displayResult(double&, double&, double&)’|
/home/chaos/Desktop/C++/hw6d/main.cpp|16|error: at this point in file|
/home/chaos/Desktop/C++/hw6d/main.cpp|12|warning: unused variable ‘rate’|
/home/chaos/Desktop/C++/hw6d/main.cpp|13|warning: unused variable ‘loan’|
||=== Build finished: 6 errors, 2 warnings ===|


This post has been edited by DrolArumil: 02 November 2009 - 04:09 AM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ Pass by Ref In Functions

#2 galorin  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 86
  • Joined: 17-February 09

Re: C++ Pass by Ref In Functions

Posted 02 November 2009 - 04:19 AM

View PostDrolArumil, on 2 Nov, 2009 - 01:36 AM, said:

I am really stuck and cant seem to find an answer online that I can understand. I I need to declare my prototypes, call them in main, get the information from user, and assign it to the function (not sure how to word it) so i can "return" multipule data types at a time. Yes this is homework I feel like im getting close (I missed this part of class due to work)

if anyone can help please do i been working on this for houuurs maybe just show me how a proper program would look like with what im trying to do or something. also now I keep getting an error that says "to few arguments in getData(double&, double&) to function.

Thank you!


Well, you defined your functions to take in addresses of a double, I think... not used to seeing & used in a function prototype, usually it's *. When you call your functions, you aren't giving them the information they need, i.e. the addresses where the doubles are stored.

Basically, your function calls do not meet the defined requirements for your functions, hence they won't work.
Was This Post Helpful? 0
  • +
  • -

#3 DrolArumil  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-November 09

Re: C++ Pass by Ref In Functions

Posted 02 November 2009 - 04:32 AM

Woot thanks, I wasnt thinking. Its fixed! heres the working code!:)


#include <iostream>
#include <iomanip>
using namespace std;

void getData(double& rate, double& loan);
void calcInterest(double &rate, double &loan, double &monthly);
void displayResult(double rate, double loan, double monthly);


int main()
{
	double rate;
	double loan;
	double monthly;

	cout << setprecision(2) << showpoint << fixed;

	getData(rate, loan);
	calcInterest(rate, loan, monthly);
	displayResult(rate, loan, monthly);
	return 0;

}

void getData(double& rate, double& loan)
{
	rate = 0;
	loan = 0;
   	cout << "Please Enter The Annual Interest Rate: ";
	cin >> rate;
	cout << "\nPlease Enter The Loan Ammount: ";
	cin >> loan;
	cout << endl;
}

void calcInterest(double &rate, double &loan, double &monthly)
{
	monthly = ((rate/100) * loan)/12;
}

void displayResult(double rate, double loan, double monthly)
{
	cout << "The First Months interest on the loan amount of " << loan << " at " << rate << " equals " << monthly;
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1