Need Help with Function; program not working

Hello,
Could someone direct me to the build a better understanding of functions. I need to create a program that computes the area of different shapes. My functions need to be validated

#include <iostream>
using namespace std;
/* this program will present the user with a menu and allow the user
to calculate different type of geometric shape */


const double PI=3.1416;
const double AREC (int length)(int width);
const double ACIRCLE= PI( radius*radius);
const double ATRIANGLE= (height*base)/2;


int main()
{
int option,length,width,radius,height,base;


do
{

cout << "\n1- Compute the area of a Rectangle : "
<< "\n2- Compute the area of a Circle: "
<< "\n3- Compute the area of a Triangle : "
<<"\n4- Exit Program " << endl;

cout << "\n\tPlease select an option : ";
cin >> option;

if(option == 1)
{
cout << "\nPlease enter length of the Rectangle : ";
cin >> length;

cout << "\nPlease enter the width of the Rectangle : ";
cin >> width;

cout << "The area of the Rectangle is : " << AREC() ;

}
else if(option == 2)
{
cout << "\nPlease enter the radius of the Circle: "
cin>> radius ;

cout <<"\n The area of the Cirlce is: "
<< ACIRCLE();
}
else if(option == 3)
{
cout<<" In order to calculate the Area of the triangle;"
<< " we must input the base of the triangle, followed by the height of the triangle"
<< "\nPlease enter the base of the triangle";
cin >> base;

cout << "Now, please enter the Height of the triangle: ";
cin >> height;

cout << " The area of the Triangle is: "<< ATRIANGLE();
}
else if(option == 4)
{
cout << "Terminating Program" << endl;
}
else
{

cout << "Invalid Option entered" << endl;
}
}
while(option != 4);
return 0;
}
Last edited on
http://www.cplusplus.com/doc/tutorial/functions/

You already know how to write functions. You write at least one every program. That function is named main, and it looks like this:
1
2
3
int main() { 
   // ...some code...
}


Other functions will look similar.
Hi mbozzi,

Thank you for your response. I know we create function just by typing int main () but how does it work before int main (). Because I seem to understand why my program is not running. Should I established the variable before ?
Your program doesn't compile because of syntax errors.
Did you read the tutorial? Did you read your compiler's output?

how does it work before int main ()

Be more specific. What is "it"?
Last edited on
Hello mbozzi ,
I read the tutorial. I still do not understand how to established a function before the main (). I would like to create a program, that ask the user for an input and from the input the program could solve the area of each shape. But I need to solve ensure the user only input positive number not negative.
My guess is that const double AREC (int length)(int width); is supposed to be a function which returns the area of a rectangle.

Let's think mathematically for a second. A function is (ideally) nothing more than a mapping between inputs and exactly one output.
In this case, you want to program a function which accepts the dimensions of a rectangle and returns it's area.

There are four things you need to do:
Decide on names which make the purpose of your function clear
Decide what kind of inputs you want to accept
Decide what kind of output you want to produce
Write the function body.

You want a function which computes the area of a rectangle.
rectangle_area()
You need two inputs -- the width and the height.
rectangle_area(width, height)
Probably both dimensions are real numbers, and even if not, perhaps you should allow for that possibility. Therefore we'll be using double precision floating-point, which can represent a decent approximation of most reasonable inputs.
rectangle_area(double width, double height)
If we will allow users to compute the area of a rectangle whose dimensions are real, we'll need to output a real-number too:
double rectangle_area(double width, double height)
The part of the program that computes the area from the width and height goes between braces:
1
2
3
double rectangle_area(double width, double height) { 

}

And we know that the area of a rectangle is the product of its dimensions:
1
2
3
double rectangle_area(double width, double height) { 
  width * height;
}

Finally, we need to give the result of width*height back to the caller -- the person who's asking for it:
1
2
3
double rectangle_area(double width, double height) { 
  return width * height;
}


Then you could compute the area of a rectangle with dimensions 2.5 and 3.5 by writing a complete program which looks like this:
1
2
3
4
5
6
7
8
# include <iostream>
double rectangle_area(double width, double height) { 
  return width * height;
}

int main() { 
  std::cout << rectangle_area(2.5, 3.5);
}


Can you follow a similar process for computing the area of a circle with some radius? Show us your attempt.

Also, please move this post to the Beginner's board.
Last edited on
I was under the impression it needed to be constant because the formula would not change. My other question, can i type all three formula and have it return area?
Functions cannot be constant; you can't change the behavior of a function.

The result of our function call is an prvalue, which cannot be modified. You can bind the result of a function call expression to a constant if you choose, though, but that doesn't affect the function: the equals-sign does not represent equality; it is an assignment.
1
2
3
4
double const area = rectangle_area(2.5, 3.5);
// area = rectangle_area(1, 2); // not valid -- we can't change the value of a constant.  
double area2 = rectangle_area(3, 4);
area2 = rectangle_area(5, 6); // is valid -- this is assignment: no effect on rectangle_area.   


Regarding "marking the function constant": you can ask the compiler (given C++11 or later support) to allow computation on constant expressions, and you can mark the parameters constant to prevent accidentally changing them - to help avoid programming errors:
1
2
3
constexpr double rectangle_area(double const width, double const height) { 
  return width * height;
}

But I was trying to keep things simple.

can i type all three formula and have it return area?

You will need to follow a similar process as I did above. Try to define a function which computes the area of a circle with some radius. Assuming you make a second function named circle_area, you could get the area of a circle by asking for it:
double area = circle_area(2);
Not sure if you got the validation question answered but you could validate the data as the user inputs it like:

cin >> data;
while( data < 0)
{
cout<< "Invalid data, please enter a number larger than 0";
cin >> data;
}
Also your data should be a double or float when possibly dealing with decimals. (Which will happen with radius and PI) The choice should stay an int.
Topic archived. No new replies allowed.