954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Call by reference question?

I have to ge t the result using call by reference. I'm receiving 4 errors???
from my understanding, I have 'declared' total before main. Quite frankly, I'm not getting why not declared it on display_output nonetheless??
and why total is still not being recognized??

Thanks in advance for the help!
eddy
____________________________________

//This program allows the user to enter 3 integers
//then computes their sum and outputs the result.

#include <iostream>
using namespace std;

 void get_input (int& num1, int& num2, int& num3);
 int  find_sum  (int& num1, int& num2, int& num3);
 void display_output(int& num1, int& num2, int& num3, int& total);

int main()
{

	int num1, num2, num3, sum, total;
	char answer;

// get input from user
	do 
	{
get_input (num1, num2, num3);

		sum= find_sum(num1, num2, num3);

display_output(num1, num2, num3, total);
	
		cout << "Do you want to continue? (Enter Yes or No)"<<endl;
		cin >> answer;
	}
	while (answer != 'n' && answer !='N');

	return 0;
}
 void get_input(int a, int b, int c)
 {
	cout << "Enter the first number" <<endl;
	cin >> a;
	cout << "Enter the second number" <<endl;
	cin >> b;
	cout << "Enter the third number" <<endl;
	cin >> c;
 }

 int find_sum  (int a, int b, int c)
 {	 
	return a+b+c;
 }

 void display_output(int a, int b, int c, int total)
 {
	 cout << "The sum of" << a << b << c << " is "
		  << total << endl;
 }
eddy518
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

>> I'm receiving 4 errors
please post them.

my guess about the errors: The parameters on line 45 must match exactly line 6. Same with line 5 and 40.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

You forgot to add the reference symbol in front of your variable names on lines 30, 40 and 45.

FerrousRex
Newbie Poster
17 posts since Sep 2007
Reputation Points: 10
Solved Threads: 2
 

>> I'm receiving 4 errors please post them.

my guess about the errors: The parameters on line 45 must match exactly line 6. Same with line 5 and 40.


Thanks for the inputAncient Dragon..I will work on that
here's the 4 errors that I keep getting, (hope that helps)

error LNK2001: unresolved external symbol "void __cdecl display_output(int &,int &,int &,int &)" (?display_output@@YAXAAH000@Z)

error LNK2001: unresolved external symbol "int __cdecl find_sum(int &,int &,int &)" (?find_sum@@YAHAAH00@Z)

error LNK2001: unresolved external symbol "void __cdecl get_input(int &,int &,int &)" (?get_input@@YAXAAH00@Z)

fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

4 error(s), 0 warning(s)

eddy518
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

Those errors mean that you prototyped the functions using the reference operator, but the actual functions get the paramets by value, not by reference. The functions, even though they have the same name, are not the same. Fix the problem as already pointed out a couple times before.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
Those errors mean that you prototyped the functions using the reference operator, but the actual functions get the paramets by value, not by reference. The functions, even though they have the same name, are not the same. Fix the problem as already pointed out a couple times before.

Thanks Guys..
I fixed the problem like mentioned..
line 11 kinda confused.. my confusion is this..
If I understand, the call by ref. I only need to call total when it passed by ref.
and not to identify total as an int in main. am i wrong??

(my stupidity on all the lines on call by reference)

ok so far so good, still working on it! I only have 1 error, 0 warningserror C2065: 'total' : undeclared identifier


_______________________________________

//This program allows the user to enter 3 integers
//then computes their sum and outputs the result.

#include <iostream>
using namespace std;

 void get_input (int& num1, int& num2, int& num3);
 int  find_sum  (int& num1, int& num2, int& num3);
 void display_output(int& num1, int& num2, int& num3, int& total);

int main()
{

	int num1, num2, num3, sum;
	char answer, result;

// get input from user
	do 
	{
get_input (num1, num2, num3);

		sum= find_sum(num1, num2, num3);

display_output(num1, num2, num3, total);
	
		cout << "Do you want to continue? (Enter Yes or No)"<<endl;
		cin >> answer;
	}
	while (answer != 'n' && answer !='N');

	return 0;
}
 void get_input(int& a, int& b, int& c)
 {
	cout << "Enter the first number" <<endl;
	cin >> a;
	cout << "Enter the second number" <<endl;
	cin >> b;
	cout << "Enter the third number" <<endl;
	cin >> c;
 }

 int find_sum  (int& a, int& b, int& c)
 {	 
	return a+b+c;
 }

 void display_output(int& a, int& b, int& c, char result)
 {
	 cout << "The sum of" << a << b << c << " is "
		  << result << endl;
 }
eddy518
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

>>am i wrong??
Yes, you are wrong. You ALWAYS have to declare variables before they can be used. And on line 24 total is no different. pass by reference means that the receiving function ( display_output in your program) will have direct access to the referenced variables which must be declared somewhere in your program. So if display_output changes the value of total then that change will be visible when display_output returns to its caller -- in your program main().

And in case you have not figured it out yourself by now, line 45 is incorrect. You forgot the last parameter -- total.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

First, I'm not understanding why are you all being so weird here. There totally no of TOTAL Just refer to your code.

display_output(num1, num2, num3, sum);
void display_output(int& a, int& b, int& c, int& result)


And you might wanna declare result as integer too. That is all there is. No other errors.

ChaseVoid
Junior Poster
116 posts since Aug 2007
Reputation Points: 40
Solved Threads: 12
 

>I have 'declared' total before main
You have never declared total before main(), you only declared the function. The proper declaration of it would be int total; where ever you want to declare it or with which ever scope.

ChaseVoid
Junior Poster
116 posts since Aug 2007
Reputation Points: 40
Solved Threads: 12
 
First, I'm not understanding why are you all being so weird here. There totally no of TOTAL Just refer to your code.

Look who's calling the kettle black -- you can't write a sentence that makes any sense. :)

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>you can't write a sentence that makes any sense.
Sorry for not being a scholar in English. >_>

ChaseVoid
Junior Poster
116 posts since Aug 2007
Reputation Points: 40
Solved Threads: 12
 

Eddy518,
Logic-wise, it looks like you want to feed the result of find_sum() into the fourth parameter of display_output(). Get rid of 'total' altogether and say 'display_output(num1, num2, num3, sum). Some would just say 'display_output(num1, num2, num3, find_sum(num1, num2, num3))' but that is not much fun to look at or debug...

Change the char back to &total to match your decl.

z80-2-Risc
Newbie Poster
5 posts since Aug 2004
Reputation Points: 10
Solved Threads: 1
 
>you can't write a sentence that makes any sense. Sorry for not being a scholar in English. >_>

We don't expect you to be. Just write sentences that people can understand -- grammer doesn't have to be perfect, just understandable. I still have not figured it out

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Special thanks to all the posters for ALL the input! I got it to work!!
I will continue to browse this GREAT forum for knowledge. In the meantime, I will be working on my next Programming Assignment. If I have any question, I will post them. Wish me Luck!

eddy518
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 
We don't expect you to be. Just write sentences that people can understand -- grammer doesn't have to be perfect, just understandable. I still have not figured it out


> There totally no of TOTAL Just refer to your code.

This is the part you were referring to, right? Well, I just went back a re-read it. LOL it surely is weird. I didn't realize so many typ-o's. Well, What I wanted to say was, There was no need to use "total". Just refer to the code below :

xD I got you'll more confused that the others >

ChaseVoid
Junior Poster
116 posts since Aug 2007
Reputation Points: 40
Solved Threads: 12
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You