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