7 Replies - 261 Views - Last Post: 18 February 2013 - 10:03 PM Rate Topic: -----

#1 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Pass by reference error

Posted 18 February 2013 - 10:05 AM

The following code compiles but the output is not correct. I'm not seeing where the mistake is, though I believe it's here : cout << name << " has a house with " << (x, y) << " square feet that contains " << (x, y, z) << " cubic feet." << endl;

#include <iostream>
using namespace std;

char name[] = "Joe Blow"; //this is a character array holding my name

void area(int& len, int& wid) {
	int Area; 
	Area = len * wid;
}

void volume(int& len, int& wid, int& hgt) {
	int Volume;
	Volume = len * wid * hgt;
	
}

int main() {
	int x;
	int y;
	int z;

	cout<< "Enter the length of your house: " << endl;
	cin>> x;
	cout<< endl;
	cout<< "Enter the width of your house: " << endl;
	cin>> y;
	cout<< endl;
	cout<< "Enter the height of your house: " << endl;
	cin>> z;
	cout<< endl;
	area(x, y);
	volume(x, y, z);
	cout << name << " has a house with " << (x, y) << " square feet that contains " << (x, y, z) << " cubic feet." << endl;
	
	return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Pass by reference error

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Pass by reference error

Posted 18 February 2013 - 10:20 AM

So what exactly do you think the following line will produce?

   cout << name << " has a house with " << (x, y) << " square feet that contains " << (x, y, z) << " cubic feet." << endl;


If you expect the output to be "Name has a house with LENGTH, WIDTH square feet..." then you will need to print the x and y values separated by the insertion operator<< not the comma operator, .

cout << name << " has a house with " << x << "," << y << ...



Jim
Was This Post Helpful? 0
  • +
  • -

#3 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Pass by reference error

Posted 18 February 2013 - 10:43 AM

Well, I want it to calculate the area and the volume then print said calculations to the screen.

If I wrote << x * y << and << x * Y * z << I would get the right values, but that's not using pass-by-reference, right?

This post has been edited by iburres: 18 February 2013 - 10:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2910
  • View blog
  • Posts: 10,075
  • Joined: 08-August 08

Re: Pass by reference error

Posted 18 February 2013 - 10:57 AM

Look at your area() function. What do you think it does? Why is it void?
Was This Post Helpful? 0
  • +
  • -

#5 iburres  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 148
  • Joined: 05-September 12

Re: Pass by reference error

Posted 18 February 2013 - 11:12 AM

It calculates the area, len * wid which should have the values for x and y passed to them. It's void because it doesn't return a value.

If I put this:

cout << name << " has a house with " << area << " square feet that contains " << volume << " cubic feet." << endl



I'm getting the address the pointers are referencing, I believe.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4002
  • View blog
  • Posts: 12,346
  • Joined: 25-December 09

Re: Pass by reference error

Posted 18 February 2013 - 11:16 AM

Quote

It's void because it doesn't return a value.

Why doesn't it return a value?

Why are you passing the input values by reference?

Do you really want this function to modify those values, or do you perhaps want to return the volume?

Jim
Was This Post Helpful? 0
  • +
  • -

#7 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Pass by reference error

Posted 18 February 2013 - 09:56 PM

Well, your program will compile without any syntax error but you will certainly have a logical error as follows.

(x,y) -> what this does is the value returned is the value of y

(x,y,z) -> the value returned is value of z.

This is happening because of the comma operator so since it is left to right associativity, the value of the rightmost operand is returned.

What you probably want is your function to be called...and a function call is done like this.
function_name( parameters_if_any);


In your example, it would be
 area(x,y);

and similarly you need to do for volume as well.

regards,
Raghav
Was This Post Helpful? 0
  • +
  • -

#8 raghav.naganathan  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • Posts: 1,440
  • Joined: 14-September 12

Re: Pass by reference error

Posted 18 February 2013 - 10:03 PM

Also, why have you defined your functions to return nothing, when you need the area and volume values? You need to change them appropriately to return int values as your variables are all int.

Well, one more thing...I feel it would be better if you could assign the values returned by the functions to separate variables and then use those variables in your cout statement.

Edit : Sorry for the double post...for some reason, my edit button wasn't responding in my above post.

regards,
Raghav

This post has been edited by raghav.naganathan: 18 February 2013 - 10:05 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1