Call-By-Values

hello, I recently just got some of the understanding of call-by-reference values. Now im trying to use a call-by-value and cannot get it to work for my life. I know im doing something wrong but how wrong is the problem. Ive read on it for an hour and half, hopefully someone can help me. the call-by-value im using is calcSqFt. im trying to set it and then call it in the main.
Any help would be useful, thanks guys :)

 
look at code on post 6.


EDIT: still not solved.
Last edited on
What are you trying to do with line 50? it does nothing
I was trying to call the calcSqFt, i had the values width, height, and length in there because they are not identified inthe void, which is one of the issues im having.
Yeah but you aren't doing anything to the values in the function did you mean to pass by reference and modify the values outside the function?
in function calcSqFt there are no variables defined as length and width. So how can you use them on lines 23-25!!
It is not supposed to be a call-by-reference value.
Here is the description of the function from teacher

"This function receives the values for length and width, and calculates the square footage including the ceiling.

The function returns an integer to main."

I did more research and made some changes to the code which looks more right but not completed.

now my length and width are undefined.

EDIT: I changed it so my length and width are defined, but now it is not collecting and storing the values.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

	const int	height = 8,
				can = 250;
	const double	price = 12.99;
	int		length,
			width;
	double	sqft,
			cost,
			ttlcan;

void displayTitle(){
	cout << "\n           Chesapeake Remodeling Company";
	cout << "\n                    Paint Costing";
	cout << "\n        -------------------------------------";
}

void getData(int& length, int& width){
	cout << "\n\n    Enter length in feet (0 to stop)...";
	cin >> length;
	if (length >0){
	cout << "    Enter width in feet................";
	cin >> width;
	}
}

double calcSqFt(double sqft){
	return sqft = (2 * (length * height)) + (2 * (width * height)) + (width * length);
}

int main ()
{
	
	displayTitle();

	getData(length, width);


while (length > 0){

	calcSqFt(sqft);

	ttlcan = sqft / can;

	cost = ttlcan * price;

	cout << "\n     Square Footage = " << sqft;
	cout << "\n     Cans           = " << ttlcan;
	cout << "\n     Cost           = " << cost;

	cout << "\n\n      Thank you for your purchase.";

/*	if (cans <=3)
		{
			cout << "\n      Gift = Free Paint Brush";
		}
	elif (7 >= cans >= 4)
		{
			cout << "\n      Gift = Free Paint Tray";
		}
	else (cans >= 8)
		{
			cout << "\n      Gift = $10 Gift Card";
		}
		*/
	getData(length, width);
}
	system("pause>nul");
	return 0;
}
Last edited on
Yeah now you are returning a value. Before you weren't returning anything that is what threw me off.
the code runs correctly, but the value of sqft is 0 when outputted. also, should i have my ints and doubles inside my main and keep the constants outside the main?
and the return isnt setup correctly i dont think.

EDIT: still not working
Last edited on
You should have all those variables in your main function never use globals. Also I'm not sure why you have sqft as a parameter. You should pass length , height , and width as parameters and do something like

1
2
3
4
int clacSqFt( const int height , const int length , const int width ) //all 3 are ints so it should reutrn int
{
    return( ( 2 * length * height ) + ( 2 * width * height ) + ( width * height ) );
}
I figured it out, i created a new integer to equal the equation, then called the new int. thanks for the help. :)
Topic archived. No new replies allowed.