Pass by value, reference

closed account (2EURX9L8)
This is just not sinking in no mater how much I go over my text and this site.
I am completely lost using these, I need to write 4 functions including main to display a report with the size of room in square, feet cost of carpet,cost to install the carpet.

This is my code so far, could someone take a glance and maybe help me understand this concept better?


#
Last edited on
Is your question about how the code works, or about the difference between pass by value and pass by reference?
closed account (zb0S216C)
I take it you don't understand references?

Simply, a reference is NOT! an object, but just an alias. When you initialise a reference to object (a reference MUST! be initialised), the reference becomes an alternative name for the assigned object. For example:

1
2
int object( 10 );
int &ref( object );

In this code, ref becomes another name for object. So any changes made to ref affect object. The object referred to is called a referent.

When a reference has been initialised, the reference cannot be re-assigned to another object. Effectively, it's similar to this pointer declaration: int * const ptr( &... ). Note that a pointer IS NOT! a reference.

Now, when references are used as parameters, the same thing happens as the above code. For example:

1
2
3
4
5
6
7
8
9
10
void basic_function( const int &ref_param )
{ 
    ... 
}

int main( )
{
    int object( 10 );
    ::basic_function( object );
}

When object is passed to basic_function( ), ref_param refers to object. Specifically, ref_param refers to the argument you give it.

Note that a constant reference, like the parameter in the above code, can refer to literals (magic constants), such as 10, 'A', 3.14, and "String". These are called symbolic constants.

Wazzak
Last edited on
while(width,length, carpetCost<=0)

That effectively evaluates to:

while ( carpetCost <=0 )

You want:

while (width <=0 || length <=0 || carpetCost <=0)

By the same token:

cin>>width, length, carpetCost;}

is the same as

(cin >> width), length, carpetCost;

You probably want something closer to:

cin >> width >> length >> carpetCost;
closed account (2EURX9L8)
Thank you for your input guys, I do appreciate it but I am am still lost.

How do you use pass by value/reference with multiple values and used by multiple functions then report single values from those multiple value functions in main?????
Last edited on
you delete the code?
closed account (2EURX9L8)
some of it, I just have been going at it for a couple of hours changing it trying other things, then changing back, then back again ect...


Last edited on
btw, your function could be more simpler:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void getInput(double &width, double &length, double &carpetCost)
		{	//double width, length, carpetCost;
	
			cout<<"please enter the width: "<<endl;
			cin>>width; 
			while(width<=0)
				{cout<<"Error, please enter a width with a postive value: ";
				cin>>width;}
		
			cout<<"please enter the length: "<<endl;
			cin>>length;
			while(length<=0)
				{cout<<"Error, please enter a length with a postive value: ";
				cin>>length;}
			
			cout<<"please enter the price of the carpet: "<<endl;
			cin>>carpetCost;
		    while(carpetCost<=0)
				{cout<<"Error, please enter a price with a postive value: ";
				cin>>carpetCost;}
			
			return;
		
		}


turns like:
1
2
3
4
5
6
7
8
9
10
11
12
13
void getInput(double &width, double &length, double &carpetCost) {
	//double width, length, carpetCost;
	do {
		cout << "please enter a positive values for all the variables...";
		cout << "enter widhth: ";
		cin >> width;
		cout << "enter length: ";
		cin >> length;
		cout << "enter the carpet cost: ";
		cin >> carpetcost;
	} while (width <= 0 || length <= 0 || carpetcost <= 0);
	return;
}


"trimmed" my way haha...
Last edited on
closed account (2EURX9L8)
yeah I changed it and I think I copied and pasted an old version. Thanks though. What about the functions? how do I un cluster $@# it and get it to work?
i am just going to use your own code to show you your mistake
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
#include <iostream>
#include <iomanip>

using namespace std;
void getInput(double, double&, double&);
double computeArea(double length, double width);
void computeCosts(double& area,double& carpetCost);

	const int INCHESPERSQUARE=144;
	

	int main(){
		
			double width, length, carpetCost,totalArea,totalInstallcost;
			cout<<"\t\tAce Carpet Company Cost estimate\n\n";
			//getInput(width,length,carpetCost);
			cout<<"The area in square feet is: "<<totalArea;
			cout<<"The cost of the carpet is: "<<carpetCost++<<endl;
			cout<<"The cost to install the carpet is "<<totalInstallcost++<<endl;
	
	}		

	void getInput(double &width, double length, double &carpetCost)
		{	//double width, length, carpetCost;
	
			cout<<"please enter the width: "<<endl;
			cin>>width; 
			while(width<=0)
				{cout<<"Error, please enter a width with a postive value: ";
				cin>>width;}
		
			cout<<"please enter the length: "<<endl;
			cin>>length;
			while(length<=0)
				{cout<<"Error, please enter a length with a postive value: ";
				cin>>length;}
			
			cout<<"please enter the price of the carpet: "<<endl;
			cin>>carpetCost;
		    while(carpetCost<=0)
				{cout<<"Error, please enter a price with a postive value: ";
				cin>>carpetCost;
cout<<"please enter the install cost<<install cost--";
cin>>"instal cost"}
while(width<=0,length<=0,carpet cost<=0);
			return(0);
while(width>>0,length>>0,carpetcost>>0);
return(carpet cost);
		
		}

i used pass by reference it is much easier to master and use considering that u will just need to know what you are passing and use some constructor and a destructor in the next class or function u want to call it in
closed account (2EURX9L8)
well thank you all for helping my worn out brain
Last edited on
closed account (zb0S216C)
I've had a go at writing you code. I've omitted the body of computeCosts( ) and computeArea( ) to give you something to chew on.

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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <iomanip>
#include <limits>

//
// P R O T O T Y P E S
//

/* get_input( ): Obtains the users values. */
void get_input( double &width, double &length, double &cost );

/* compute_area( ): Computes the area based on the specified width and length. */
double compute_area( const double &width, const double &length );

/* compute_costs( ): Computes the total amount based on area and cost. */
void compute_costs( const double &area, double &cost );

//
// C O N S T A N T S
//

/* INCHES_PER_SQUARE: Used for the computation of the area. */
const unsigned int INCHES_PER_SQUARE( 144U );

//
// M A I N  -  E N T R Y   P O I N T
//

int main( )
{
    //
    // L O C A L   O B J E C T S
    //

    double width( 0.0 ),
           length( 0.0 ),
           carpet_cost( 0.0 ),
           total_area( 0.0 ),
           total_install_cost( 0.0 );

    /* Display the program's title. */
    std::cout << "\t\tAce Carpet Company Cost estimate\n\n";

    /* Retrieve the user's input. */
    ::get_input( width, length, carpet_cost );

    /* Perform all relevent computations */
    ::compute_costs( ::compute_area( width, length ), carpet_cost );

    /* Display all relevant information regarding area and cost. */
    std::cout << "The area in square feet is: " << total_area << std::endl;
    std::cout << "The cost of the carpet is: " << carpet_cost << std::endl;
    std::cout << "The cost to install the carpet is " << total_install_cost << std::endl;

    /* Give the user a chance to see the result. */
    std::cin.ignore( std::numeric_limits< std::streamsize >::max( ), '\n' );
    return( 0 );
}

/* Definition Of: get_input( )
 *
 * [O] width:  Stores the users requested width.
 * [O] length: Stores the users requested length.
 * [O] cost:   Stores the users requested cost.
 */
void get_input( double &width, double &length, double &cost )
{
    while( width < 1U )
    {
        std::cout << "please enter the width: ";
        std::cin >> width;
    }

    while( length < 1U )
    {
        std::cout << "please enter the length: ";
        std::cin >> length;
    }

    std::cout << "please enter the price of the carpet: ";
    std::cin >> cost;
}

/* Definition Of: compute_area( )
 *
 * [I] width:  The requested width.
 * [I] length: The requested length.
 */
double compute_area( const double &width, const double &length )
{
    return( 0.0 );
}

/* Definition Of: compute_area( )
 *
 * [I] area: The area of the floor.
 * [O] cost: Stores the price per inch.
 */
void compute_costs( const double &area, double &cost )
{
}


Wazzak
Last edited on
Topic archived. No new replies allowed.