Please help with calculating area of shapes

Hi everyone I desperately need help with an assignment to calculate the area of shapes using functions. PLEASE be patient as I am very new and I am doing my best and can't seem to figure it out. I just need a helping hand in the right direction! Here is my code and again I'm new and I know it's a mess..

#include<iostream>
#include<iomanip>

using namespace std;

//function prototypes
float c(float);
int s(int);
int r(int);
void showWelcome();
void showMenu();
//main function
int main()
{
//declare variables as int. //menu choice from user
int x;
int choice;
int a,r,l,w;
float area;
const double PI = 3.14;
const int circle=1,
rectangle=2,
square=3,
quit=4;
cout << fixed << showpoint << setprecision(1); //sets to 1 decimal place
do
{
showWelcome(); // Show Welcome screen
showMenu(); // Display Menu
cin >> choice;

//Validate menu selection
while (choice < circle || choice > quit)
{
cout << "Please enter a valid menu choice: ";
cin >> choice;
}

//If user does not want to quit, proceed.
if (choice != quit)
{
switch (choice)
{
case circle:
cout << "Please enter the radius. ";
cin >> r;
float c;
break;


case square:
cout << " Please enter the base. " ;
int s;
break;

case rectangle:
cout << " Please enter the length. " ;
int r;
break;
}

}
} while (choice != quit);
return 0;
}
//Welcome Function
void showWelcome()
{
cout << "Welcome to calculating the area of shapes!" << endl << endl;
system ("pause");
}
//Menu Function

void showMenu()
{
cout << "Please choose a shape to calculate the area " << endl << endl
<< "1. Circle" << endl
<< "2. Square" << endl
<< "3. Rectangle" << endl
<< "4. Quit"<< endl << endl;
}
//functions


float c(int x) //circle function
{
float area;
area=3.14*x*x;
return(area);
}

int s(int x) //square function
{
int area;
area=x*x;
return(area);
}

int r(int x,int y) //rectangle function
{
int area;
area=x*y;
system("pause");
return(area);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

double area_of_circle(double radius);

int main()
{
    double r;
    std::cout << "Enter the radius: ";
    std::cin >> r;

    double area = area_of_circle(r);

    std::cout << "The area of a circle with radius " << r << " is " << area << ".\n";
}

double area_of_circle(double radius)
{
    const double pi = 3.14159;
    return radius * radius * pi;
}


Call your functions and use the return values.

Exactly what problems are you having?

Line 47: What is the purpose of declaring variable c? Note that you also have a function by this name. Naming functions and variables the same is a poor practice. Very confusing to the reader. Use more meaningful names for your functions.

What I think you want here is:
1
2
3
4
5
6
 
  case circle:
    cout << "Please enter the radius. ";
    cin >> r;
    cout << "The area of the circle is " << circle_area (r) << endl;
    break;    


Your other cases would be similar.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Thank you! Yeah sorry if it posted weird I was in a bit of a panic and posted on my phone, I should be on a computer soon to test these out. Thanks again
okay guys I revamped it a bit and I think I possibly made it worse, lol, it seems I can't use "area" for each shape?

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<iostream>
#include<iomanip>

using namespace std;

//function prototypes
double area_of_circle(double radius);
double area_of_square(double base);
double area_of_rectangle(double length);
void showWelcome();
void showMenu();
//main function
int main()
{
//declare variables as int. //menu choice from user
int choice;
int a,r,l,b;
const double PI = 3.14;
const int circle=1,
          rectangle=2,
          square=3,
          quit=4;
cout << fixed << showpoint << setprecision(1); //sets to 1 decimal place
do
    {
         showWelcome(); // Show Welcome screen
         showMenu(); // Display Menu
         cin >> choice;
         
         //Validate menu selection
         while (choice < circle || choice > quit)
         {
               cout << "Please enter a valid menu choice: ";
               cin >> choice;
         }
         
         //If user does not want to quit, proceed.
         if (choice != quit)
         {
                    switch (choice)
                    {
                           case circle:
                                	double r;
                                    cout << "Enter the radius ";
                                    cin >> r;
                                    double area = area_of_circle(r);
                                    cout << "The area of the circle with radius " << r << " is " << area << " . \n";
                                break;
                            case square:
                   	        double b;
                            cout << " Enter the base ";
                            cin >> b;
                           double area = area_of_square(b);
                           cout << "The area of the square with base " << b << " is " << area << " . \n";
                           break;
                           case rectangle:
                                double l;
                                cout << " Please enter the length. " ;
                                cin >> l;
                                double area = area_of_rectangle(l);
                                break;
                    }
                    
         }
    } while (choice != quit);
      return 0;
}
//Welcome Function
void showWelcome()
{
     cout << "Welcome to calculating the area of shapes!" << endl << endl;
     system ("pause");
}
//Menu Function

void showMenu()
{    
    cout << "Please choose a shape to calculate the area " << endl << endl
         << "1. Circle" << endl
         << "2. Square" << endl
         << "3. Rectangle" << endl
         << "4. Quit"<< endl << endl;
} 
double area_of_circle(double radius)
{
	const double pi = 3.14;
	return radius * radius * pi;
}
double area_of_square(double base)
{
       return base * base
}
double area_of_rectangle(double length)
{
       return l * l
}


appreciate any and all help, once again!
Last edited on
You have been asked to use code tags. PLEASE DO SO.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
I will not respond further until you apply code tags.
i apologize
line 46, 53, 60: You're creating multiple defintions of area. A case branch does not create a new scope.

You have two choices.
1) Move the declaration of area outside the switch statement. Line 46 then becomes:
 
   area = area_of_circle(r);


2) You can create a local scope for each case branch by using {}.
1
2
3
4
5
6
7
8
  case circle:
    {  double r;
        cout << "Enter the radius ";
        cin >> r;
        double area = area_of_circle(r);
        cout << "The area of the circle with radius " << r << " is " << area << " . \n";
    }
        break;


Lines 91 and 95 are missing ;

Line 95: l is not defined. Your argument name is length.



For the rectangle, I'd collect two parameters, width and length. You're just getting the length now.
Thanks, I did option 2 lol..

Two things.. It compiles fine now, which is great, but any idea as to why it would crash when square or rectangle is selected as a choice? Also, part two of this assignment is after it's up and running to then convert the code to have the variables "pass by reference." Honestly, I get the gist, but I've come this far and screwing something up this close to class lol, would the variables basically look like for example?

double area_of_circle(&&double radius)

anyway, this is my code right 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include<iostream>
#include<iomanip>

using namespace std;

//function prototypes
double area_of_circle(double radius);
double area_of_square(double base);
double area_of_rectangle(double length);
void showWelcome();
void showMenu();
//main function
int main()
{
//declare variables as int. //menu choice from user
int choice;
const double PI = 3.14;
const int circle=1,
          rectangle=2,
          square=3,
          quit=4;
cout << fixed << showpoint << setprecision(1); //sets to 1 decimal place
do
    {
         showWelcome(); // Show Welcome screen
         showMenu(); // Display Menu
         cin >> choice;
         
         //Validate menu selection
         while (choice < circle || choice > quit)
         {
               cout << "Please enter a valid menu choice: ";
               cin >> choice;
         }
         
         //If user does not want to quit, proceed.
         if (choice != quit)
         {
                    switch (choice)
                    {
                           case circle:
                                	double r;
                                    cout << "Enter the radius ";
                                    cin >> r;
                                    double area = area_of_circle(r);
                                    cout << "The area of the circle with radius " << r << " is " << area << " . \n";
                                    system("pause");
                                    }
}
                                break;
                                switch (choice)
                                {
                            case square:
                   	        double b;
                            cout << " Enter the base ";
                            cin >> b;
                           double area = area_of_square(b);
                           cout << "The area of the square with base " << b << " is " << area << " . \n";
                           system("pause");
}
                           break;
                           switch (choice)
                           {
                           case rectangle:
                                double l;
                                cout << " Please enter the length. " ;
                                cin >> l;
                                cout << " Please enter the width. " ;
                                double area = area_of_rectangle(l);
                                cout << "The area of the rectangle with length" << l <<  "is " << area << " . \n";
                                system("pause");
                                break;
                    }     
         }
while (choice != quit);
      return 0;
}
//Welcome Function
void showWelcome()
{
     cout << "Welcome to calculating the area of shapes!" << endl << endl;
     system ("pause");
}
//Menu Function

void showMenu()
{    
    cout << "Please choose a shape to calculate the area " << endl << endl
         << "1. Circle" << endl
         << "2. Square" << endl
         << "3. Rectangle" << endl
         << "4. Quit"<< endl << endl;
} 
double area_of_circle(double radius)
{
	const double pi = 3.14;
	return radius * radius * pi;
}
double area_of_square(double base)
{
       return base * base;
}
double area_of_rectangle(double length)
{
       return length * length;
}

sorry for being annoying, this is the closest i've had to a tutor since the semester started lol, I really appreciate everyone's help
You need only one switch (choice)
Your switch statement at line 39 is now messed up.

Here's what your switch statement should look like using option #2.
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
            switch (choice)
            {
            case circle:
            {  double r;
                cout << "Enter the radius ";
                cin >> r;
                double area = area_of_circle(r);
                cout << "The area of the circle with radius " << r << " is " << area << " . \n";
                system("pause");
                break;
           }

            case square:
            {	double b;
                cout << " Enter the base ";
                cin >> b;
                double area = area_of_square(b);
                cout << "The area of the square with base " << b << " is " << area << " . \n";
                system("pause");
                break;
            }    	

            case rectangle:
            {	double l;
                cout << " Please enter the length. " ;
                cin >> l;
                cout << " Please enter the width. " ;
                double area = area_of_rectangle(l);
                cout << "The area of the rectangle with length" << l <<  "is " << area << " . \n";
                system("pause");
                break;
            }     
        }


Note that area_of_rectangle still only accepts one argument. rectangles have length and width. You prompt for width at line 68, but never input it or pass it to your area_of_rectangle function.

would the variables basically look like for example?

To correct your example:
 
double area_of_circle(const double & radius)


However, passing a simple variable (radius) by reference is somewhat pointless if you're not going to modify it. A better example would be:
 
void area_of_circle (double radius, double & area)

Last edited on
Topic archived. No new replies allowed.