Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok now I got this part working how should I make this to work using a defined function? and what items should I use to make it a myfuncion I haves tried by the earlier posts and as you can see it is not working so I went back to the basic and now need help figuring out how to implement and create my own function to do this calculation. thanks for the help
C++
#include <iostream>
#include <string>
const int SQUARE = 1;
const int TRIANGLE = 2;
const int RECTANGLE = 3;
const int CIRCLE = 4;
using namespace std;
int main()
{
    // Declare Variable
    int choice;
    int width;
    int height;
    double base;
    int area;
    int distance;
        cout << "What is the area of a square, triangle, rectangle, or circle?" ;
    // Display Choice
        cout << SQUARE << " = Square:  " << endl;
        cout << TRIANGLE << " = Triangle:  " << endl;
        cout << RECTANGLE << " = Rectangle: " << endl;
        cout << CIRCLE << " = Circle:  "  << endl;
        cout << "Make your choice? ";
        cin >> choice ;
        // Display Proper feedback based on users choice
        switch (choice)
        {
            case SQUARE:
        // Get Square information
            cout << "Enter the width of the square: " << endl;
            cin >> width;
            cin >> width;
            area = width * width;
            cout << "The area of the square is: " << area << endl;
                break;
            case TRIANGLE:
        // Get Triangle information
                cout << "Enter the base: " << endl ;
                cin >> base;
                cout << "Enter the height: " << endl ;
                cin >> height;
                area = (base * 2) * height;
                cout << "The area of a triangle is " << area << endl;
                break;
            case RECTANGLE:
        //Get the Rectangle inforamtion
                cout <<"Enter the height: " << endl;
                cin >> height;
                cout << "Enter the width: " << endl;
                cin >> width;
                area = height * width;
                cout << "The area of a rectancle is: " << area << endl;
                break;
            case CIRCLE:
        //Get the circle circumference
                cout << "Enter the distance: " << endl;
                cin >> distance;
                area = distance * 3.14;
                cout << "The circumference of a circle is: " << area << endl;
                break;
        }
system("pause");
return 0;
Posted
Updated 1-Nov-10 1:16am
v2
Comments
Fredrik Bornander 1-Nov-10 7:16am    
Added source formatting.

1 solution

well you can create your own function for calculation of each shape.
rather you can also create functions with the same name but with different signatures(or parameters) known as Overloaded function :

here is the function for Circle :

C#
function ComputeArea(Double distance)
{
 double area;
 area=distance*3.14;
 cout<<"Circumference of a circle is : "<<area;
}



here you have to pass a parameter "distance" which will be used to compute area of circle.

Similiarly for Rectangle :

function ComputeArea(integer height,integer width)
{
integer area;
area=height*width;
cout<<"Area of rectangle is: " <<area;
}


As you can see, i have created another function with the same name but with different parameters and also more importantly of different type. this is the concept of overloaded function:

similiarly you can create it for Triangle and Square.
and then in the switch code :

C#
case "CIRCLE" :
ComputeArea(2.45);


and the same for others.

Hope it helps! :)
 
Share this answer
 
v2
Comments
EDITH GINGRAS 1-Nov-10 19:53pm    
Ok I changed the wording and now I am getting
1>c:\users\edith\documents\visual studio 2010\Projects\hwweek9\Debug\hwweek9.exe : fatal error
3 of these in a row how do I fix this? thanks

#include<iostream>
using namespace std;
//Function Header
int myarea(int width,int height,int length);
int mytriangle(double base);
int mycircle (int dist);
int mychoice (int square,int rectangle,int triangle, int circle);
//Main function
int main(){
int choice;
int square =1,rectangle=2,triangle=3,circle=4;
cout << "Enter 1 for square " << square << endl;
cout << "Enter 2 for rectangle " << rectangle << endl;
cout << "Enter 3 for triangle " << triangle << endl;
cout << "Enter 4 for circle " << circle << endl;
cin >> choice;

return choice;
int width,height,length,base,dist;

cout << "Enter the width: ";
cin >> width;
cout << "Enter the height : ";
cin >> height;

if (width == length){
cout << "The area of a square is "<<myarea(width,height,length) <<endl;
}else if (length == height){
cout << "The area of a rectangle is " << myarea(width,height,length) << endl;
}else if (( base * .5) * height){
cout << "The area of a triangle is " << mytriangle(base) << endl;
}else if (dist * 3.14){
cout << " The circumference of a circle is " << mycircle(dist) << endl;


system ("pause");
return 0;
}
//End of program

//Call the function
int myarea(int width,int height,int length);
int mytriangle(int base);
int mycircle (int dist);
int mychoice (int square,int rectangle,int triangle, int circle);

int number;
number = (width * height) || (length * height) || ((base *.5) * height) || (dist * 3.14);
return number;
}
//End of function
Tarun.K.S 2-Nov-10 8:13am    
what is this? you haven't defined your function for triangle,circle etc..you need to get your C++ basics right. i don't know what those if conditions are trying to do. The if conditions are totally wrong. define the functions like i did above in my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900