Value-Returning Function not working?

For some reason i keep getting a error for line 29 saying Error expected a ";" and I don't understand what the problem is. The purpose of the program is to display a menu, let the user choose and then display the area's of the shape chosen using a value-return function.

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
 #include <iostream> //cout and cin
#include <conio.h> //getch
#include <iomanip> //used for better format

using namespace std;

//Function Declaration:
double AreaofRectangle(int width, int height)

int main()
{

	//Constants for the Menu
	const int RECTANGLE_CHOICE = 1;
	const int TRIANGLE_CHOICE = 2;
	const int CIRCLE_CHOICE = 3;

	//Variables 
	int choice;

	//Display Menu
	cout <<"\n\tArea Calculator Menu\n\t" << endl;
	cout << "1.Rectangle" << endl
		 << "2.Triangle"  << endl
		 << "3.Circle"    << endl
		 << "Enter in your choice:" << endl;
	cin >> choice;

	if(choice == 1)
	{
		double AreaofRectangle(int width, int height)
		{ //Problem Occurs here it says that it expects a ;//
			double resultRect;
			resultRect = width * height;
			return resultRect;
		}
	}
}

Last edited on
Well it is looking at line 28 and expecting that to be a call to a function... that function doesn't exist but besides that point.
Last edited on
You must review and complete your code. Here are my suggestions for now:
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
#include <iostream> //cout and cin
#include <conio.h> //getch
#include <iomanip> //used for better format

using namespace std;

//Function Declaration:
double AreaofRectangle(double, double);

int main()
{
	//Variables 
	int choice;
	double height, width;
	
	//Stuff for Triangle and Circle: base, radius...
	
	//Display Menu
	cout <<"\n\tArea Calculator Menu\n\t" << endl;
	cout << "1.Rectangle" << endl
		 << "2.Triangle"  << endl
		 << "3.Circle"    << endl
		 << "Enter in your choice:" << endl;
	cin >> choice;

	if(choice == 1)
	{
		cout << "Rectangle: enter width and height: \n";
		cout << "Width: ";
		cin >> width;
		cout << "Height: ";
		cin >> height;
		cout << "RectangleArea = " << AreaofRectangle(width, height);
	}
/*	
	else if(choice == 2)
	{
		//Code for Triangle
	}
	else if(choice == 3)
	{
		//Code for Circle
	}
*/
	getch(); 
	return 0;
}
double AreaofRectangle(double width, double height)
{ 
	return width * height;
}
	Area Calculator Menu
	
1.Rectangle
2.Triangle
3.Circle
Enter in your choice:
1
Rectangle: enter width and height: 
Width: 123
Heighth: 321
RectangleArea = 39483
Last edited on
Thanks for the reply, when I use your modifications I can't insert a do while loop, and isn't the does the value-return function create variable double AreaofRectangle and double width, double height?
Last edited on
line 31 needs a semicolon:
change
double AreaofRectangle(int width, int height)

to

double AreaofRectangle(int width, int height);
canucksfan1

What do you mean by in line 16 //stuff for triangle, circle?

stuff for triangle and circle: of course the data for these shapes: the base and height of the triangle, circle radius, #pi...what else?! :D...
For area circle and triangle and rectangle I have to use a value-return function, and I don't know what they are used for
OK! Then in those functions must be entered those data concerning rectangle, triangle and circle as I said before despite you have to use a value-return function!..I've seen in your code double AreaofRectangle(int width, int height) you don't enter those values but you pass them from nowhere! That's all!
closed account (o3hC5Di1)
Hi there,

Let's clear a few things up (I'm using your original code):

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
 #include <iostream> //cout and cin
#include <conio.h> //getch
#include <iomanip> //used for better format

using namespace std;

//Function Declaration:
double AreaofRectangle(int width, int height); //<- need a semicolon here, syntax error

int main()
{

	//Constants for the Menu
	const int RECTANGLE_CHOICE = 1;
	const int TRIANGLE_CHOICE = 2;
	const int CIRCLE_CHOICE = 3;

	//Variables 
	int choice;

	//Display Menu
	cout <<"\n\tArea Calculator Menu\n\t" << endl;
	cout << "1.Rectangle" << endl
		 << "2.Triangle"  << endl
		 << "3.Circle"    << endl
		 << "Enter in your choice:" << endl;
	cin >> choice;

	if(choice == 1)
	{
		//Can't define a function within another function (main() in this case)
		double AreaofRectangle(int width, int height)
		{ //Problem Occurs here it says that it expects a ;//
			double resultRect;
			resultRect = width * height;
			return resultRect;
		}
	}
}

//so move it here
double AreaofRectangle(int width, int height)
{ 
    double resultRect;
    resultRect = width * height;
    return resultRect;
}



I think a small explanation of the difference between declaration is in order:

7
8
//Function Declaration:
double AreaofRectangle(int width, int height);


This tells the compiler "Hey compiler, please take note that I intend to use a function called AreaofRectangle, which takes two ints as arguments, the definition of what it does will follow later, but in the meanwhile, don't freak out if you encounter the name "AreaofRectangle", mkay?".

1
2
3
4
5
6
		double AreaofRectangle(int width, int height)
		{ //Problem Occurs here it says that it expects a ;//
			double resultRect;
			resultRect = width * height;
			return resultRect;
		}


This is the definition, it tells the compiler what your function actually does, so it can make sure this code is performed the function is called.

On a small sidenote, switch statements are usually preferred for these kinds of menu's, because they make these things more readable:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch (choice)
{
    case 1:
        rectangle_processing();
        break;

    case 2:
        square_processing();
        break;

    case 3:
        triangle_processing();
        break;

    default:  //any input we did not expect
        //reprint menu and /or error message
}



Value return functions are, well, functions which return a value. In your example:

1
2
3
4
5
6
double AreaofRectangle(int width, int height)
{ 
    double resultRect;
    resultRect = width * height;
    return resultRect;
}


This function returns a double, so you could do, for instance:
double result = AreaofRectangle(5, 10);
result will be assigned the value returned by the function.


Value return functions are opposed to void functions, which just "do stuff", without returning any results. Note that you could call return in a void function, but it will just end the function, rather than return an actual value. An example of a void function which could be applicable to your program:

1
2
3
4
5
6
7
8
9
10
11
void process_rectangle()
{
		int width, height;

		cout << "Rectangle: enter width and height: \n";
		cout << "Width: ";
		cin >> width;
		cout << "Height: ";
		cin >> height;
		cout << "RectangleArea = " << AreaofRectangle(width, height);
};




Hope that makes sense and helps, please do let us know if you require any further help.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.