#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;
}
Pass by reference error
Page 1 of 17 Replies - 134 Views - Last Post: 18 February 2013 - 10:03 PM
#1
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;
Replies To: Pass by reference error
#2
Re: Pass by reference error
Posted 18 February 2013 - 10:20 AM
So what exactly do you think the following line will produce?
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, .
Jim
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
#3
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?
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
#5
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:
I'm getting the address the pointers are referencing, I believe.
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.
#6
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
#7
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.
In your example, it would be
and similarly you need to do for volume as well.
regards,
Raghav
(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
#8
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
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote





|