Box Dimensions Functions Problem

Hi I'm having some trouble with my functions. I'm getting an unreferenced local variable for length, width, height, volume, and area. Also this error as well. 'BoxDimensions' : 'void' function returning a value.

Any help would be great.

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
#include<iostream>
#include<iomanip>
using namespace std;

void DisplayData (double length, double width, double height, double volume, double area);

void BoxDimensions (double length, double width, double height, double volume);



void main()
{
	//variable declaration
	double width, length, height, volume, area;
	char repeat='y';

	//user instructions
	cout<<"This program will accept four inputs."<<endl;
	cout<<"For the measurement you would like too find, "<<endl;
	cout<<"please enter 0."<<endl;


	do//do-while 
	{
		
		//variable declaration
		double width, length, height, volume;

		//intro input
		cout<<"Please enter the width of your box";
		cin>>width;
		cout<<"Please enter the length of your box";
		cin>>length;
		cout<<"Please enter the height of your box";
		cin>>height;
		cout<<"Please enter the volume of your box";
		cin>>volume;

			//rerun the program
		cout<<"Enter y or Y to rerun the program, anything else to quit ";
		cin>>repeat;
	}while (repeat=='y'||repeat=='Y');//end do-while

}//end main

void BoxDimensions(double length, double width, double height, double volume)

{
//function calls and variables
		if(volume==0)
		{
			volume=width*length*height;
		}
		else if(width==0)
			{
				width=volume/(height*length);
			}
		else if(length==0)
				{
					length=volume/(height*width);
				}
					 else(height==0);
					{
						height=volume/(length*width);

						return main();
					}
}//end BoxDimensions

void DisplayData(double length, double width, double height, double area, double volume)
{
	//set precision
	cout.setf(ios::fixed, ios::floatfield);
	cout.setf(ios::showpoint);
	cout<<setprecision(2);

	cout<<"The width of your box is "<<setw(21)<<width<<endl;
	cout<<"The length of your box is "<<setw(20)<<length<<endl;
	cout<<"The height of your box is "<<setw(20)<<height<<endl;
	cout<<"The volume of your box is "<<setw(20)<<volume<<endl;
	cout<<"The area of your box is "<<setw(22)<<area<<endl;
}//end Displaydata 
Last edited on
First in a C++ program you can't call main() in any function, including main(). You also can't return main(). Also main() should be defined to return an int.
1
2
3
4
5
int main()
{

   return(0);
}


As far as the rest of your errors, post the complete error message exactly as it appears in your development environment.
Last edited on
warning C4101: 'length' : unreferenced local variable
warning C4101: 'width' : unreferenced local variable
warning C4101: 'height' : unreferenced local variable
warning C4101: 'volume' : unreferenced local variable
warning C4101: 'area' : unreferenced local variable

Are these the error messages you wanted?
Also;

When I click Find Message In Code it points me to line 14
At lines 14 and 27, variables with the same names are declared. The compiler may issue a warning rather than an error message. Nevertheless, you should heed the message and remove one of the sets of variables.

This code has a couple of errors, in addition to the problem with return main()
1
2
3
4
5
    else(height==0);
    {
        height=volume/(length*width);
        // return main();  // Not allowed
    }

1. this should read else if, not just else
2. the semicolon should be removed from the end of the first line
Last edited on
Alright that cleared up those problems but gave me a few more. Now when I remove the ; from line 62, It says it is missing the semicolon (this is only with else, not if else; on the same line).
I'm not clear on what changes were made.
This is the version which I tried:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void BoxDimensions(double length, double width, double height, double volume)
{
    if (volume==0)
    {
        volume = width*length*height;
    }
    else if (width==0)
    {
        width = volume/(height*length);
    }
    else if (length==0)
    {
        length = volume/(height*width);
    }
    else if (height==0)
    {
        height=volume/(length*width);
    }
}


I'm considering the program as a "work in progress" as it still has a number of things which need to be done to make it fully-functioning.

One thing which I think could be very relevant here, is passing parameters by reference versus passing by value.
http://www.cplusplus.com/doc/tutorial/functions2/
Alright, here it is currently. But what is not being shown is the output of the answers like "your length is blah blah blah".

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
#include<iostream>
#include<iomanip>
using namespace std;

void DisplayData (double length, double width, double height, double volume, double area);

void BoxDimensions (double length, double width, double height, double volume);



int main()
{
	//variable declaration
	double width, length, height, volume;
	char repeat='y';

	//user instructions
	cout<<"This program will accept four inputs."<<endl;
	cout<<"For the measurement you would like too find, "<<endl;
	cout<<"please enter 0."<<endl;


	do//do-while 
	{

		//intro input
		cout<<"Please enter the width of your box";
		cin>>width;
		cout<<"Please enter the length of your box";
		cin>>length;
		cout<<"Please enter the height of your box";
		cin>>height;
		cout<<"Please enter the volume of your box";
		cin>>volume;

			//rerun the program
		cout<<"Enter y or Y to rerun the program, anything else to quit ";
		cin>>repeat;
	}while (repeat=='y'||repeat=='Y');//end do-while

}//end main

void BoxDimensions(double length, double width, double height, double volume)

{
//function calls and variables
		if(volume==0)
		{
			volume=width*length*height;
		}
		else if(width==0)
			{
				width=volume/(height*length);
			}
		else if(length==0)
				{
					length=volume/(height*width);
				}
					else(height==0);
					{
						height=volume/(length*width);
					}
}//end BoxDimensions

void DisplayData(double length, double width, double height, double area, double volume)
{
	//set precision
	cout.setf(ios::fixed, ios::floatfield);
	cout.setf(ios::showpoint);
	cout<<setprecision(2);

	cout<<"The width of your box is "<<setw(21)<<width<<endl;
	cout<<"The length of your box is "<<setw(20)<<length<<endl;
	cout<<"The height of your box is "<<setw(20)<<height<<endl;
	cout<<"The volume of your box is "<<setw(20)<<volume<<endl;
	cout<<"The area of your box is "<<setw(22)<<area<<endl;
}//end Displaydata 
Look at line 59 of your latest code.
As I stated earlier,
1. this should read else if, not just else
2. the semicolon should be removed from the end of the line

But what is not being shown is the output of the answers like "your length is blah blah blah".


Inside function main() you need to add calls to your other functions.
Last edited on
That didn't work, the else is underlined and says "expected a (".
But when I run it the errors on the bottom say: error C2059: syntax error : 'else'
and "error C2143: syntax error : missing ';' before '{'"

1
2
3
4
if else(height==0)
		{
			height=volume/(length*width);
		}
Last edited on
I said to put "else if" (already stated this three or four times!).

... not "if else"

Look again at this post:
http://www.cplusplus.com/forum/beginner/96165/#msg516270
Last edited on
Ok, ok, I finally got it. Still having problems with the DisplayData function as it doesn't want to show. It jumps to the while loop
Still having problems with the DisplayData function as it doesn't want to show. It jumps to the while loop

I don't see a while loop in the function DisplayData().

Have you added in function main() a call to the other functions?
Topic archived. No new replies allowed.