Click here to Skip to main content
15,868,141 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

CSS
#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 31-Oct-10 13:48pm
v5
Comments
Alain Rist 31-Oct-10 2:55am    
Code formatting
Alain Rist 1-Nov-10 2:38am    
With this new code the whole discussion under is not understandable. Please revert to prior text and ask a new question.

You need to better understand how to use if and else. Look at the code snippet below:

C++
if (mychoice == square)
{
   // Work with square
}
else if (mychoice == rectangle)
{
   // Work with rectangle
}
else
{
   // Not a square nor a rectangle...
}
 
Share this answer
 
Comments
EDITH GINGRAS 31-Oct-10 10:56am    
ok thanks I see what I did there i changed it to else if then the else last
You have twice else (mychoice == XXX), are you sure your compiler can understand that construct?

else (mychoice == rectangle){ // AT THIS POINT THERE IS AN ERROR THAT SAYS EXPECTED ; WHY IS THAT?


If I was a C++ compiler I would expect else; (mychoice == rectangle). Do you understand why?

cheers,
AR

OP: no I do not can you explain this

1. Read the article Control Structures
[^] from top to Iteration structures (loops).

2. Read it again :) looking for the sentence : if (condition) statement1 else statement2.

3. In your code find:
if (mychoice == square){			
    cout << "Enter the width: ";			
    cin >> width;			
    cout << width * 2; 		
}		
else (mychoice == rectangle)


4. From this code identify the parts matching condition, statement1, and statement2 in if (condition) statement1 else statement2.

5. Is (mychoice == rectangle) a statement?

6. The compiler finds a missing statement after else and suggests ; the simplest and shortest possible statement.

What should you insert in else (mychoice == rectangle) to make your compiler happy?
 
Share this answer
 
v2
Comments
EDITH GINGRAS 31-Oct-10 9:54am    
no I do not can you explain this
Alain Rist 31-Oct-10 11:16am    
See my edited answer
EDITH GINGRAS 31-Oct-10 11:25am    
ok i see thanks but now there are even more errors on this. I changed the mychoie to sqaure == square etc for the rest
Nish Nishant 31-Oct-10 11:55am    
My vote of 5. Quality answer.
Alain Rist 31-Oct-10 12:05pm    
Comment the offending parts from bottom to top. When you have a compiling code uncomment one part at time from top to bottom. Use the maximum warning level for your compiler. Don't insert new code as long as your compiler warns. Yes writing code is a painful work :)

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