Must use functions

Help me change this into functions...

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
#include <iostream>
using namespace std;
int main() {
// declare constant variable PI
const float PI = 3.14159;
//variables required for the area calculation
int selection=0;
float radius=0.0;
float side=0;
float area=0.0;
float height=0.0;
float base=0.0;
cout<<"Program to calculate areas of objects:\n" ;
// run until 4 is selected.
Do
{
cout<<"\n1.Square \n2.Circle \n3.Rigth Triangle \n4.Quit";
cout<<"\nEnter your choice:";
cin>>selection;
if(selection==1)
{
cout<<"\nLength of side:";
cin>>side;
area=side*side;
cout<<"\nArea of Square:"<<area;
}
else if(selection==2)
{
cout<<"\nRadius of circle:";
cin>>radius;
area=PI*radius*radius;
cout<<"\nArea of circle:"<<area;
}
else if(selection==3)
{
cout<<"\nHeight of Triangle:";
cin>>height;
cout<<"\nBase of Triangle:";
cin>>base;
area=0.5*height*base;
cout<<"\nArea of Triangle:"<<area;
}
1. That is not a functional program.

2. Read: http://www.cplusplus.com/doc/tutorial/functions/
The first examples are very simple functions. Just the kind you are expected to write.

3. Indentation, please. It really helps reading the code. Sometimes even reveals syntax errors.
Hello garza07,

First I would start with making your code easier to read. It would make it easier to see that you are missing two closing "}"s. It would also allow you to see that you are missing the while condition of the do/while loop. Compiling the program would have alerted you to this unless it was a copy and paste problem.

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
#include <iostream>

using namespace std;

int main()
{
	// declare constant variable PI
	const float PI = 3.14159;
	//variables required for the area calculation
	int selection = 0;
	float radius = 0.0;
	float side = 0;
	float area = 0.0;
	float height = 0.0;
	float base = 0.0;

	cout << "Program to calculate areas of objects:\n";

	// run until 4 is selected.
	Do
	{
		cout << "\n1.Square \n2.Circle \n3.Rigth Triangle \n4.Quit";
		cout << "\nEnter your choice:";
		cin >> selection;
		if (selection == 1)
		{
			cout << "\nLength of side:";
			cin >> side;
			area = side*side;
			cout << "\nArea of Square:" << area;
		}
		else if (selection == 2)
		{
			cout << "\nRadius of circle:";
			cin >> radius;
			area = PI*radius*radius;
			cout << "\nArea of circle:" << area;
		}
		else if (selection == 3)
		{
			cout << "\nHeight of Triangle:";
			cin >> height;
			cout << "\nBase of Triangle:";
			cin >> base;
			area = 0.5*height*base;
			cout << "\nArea of Triangle:" << area;
		}
	}  // <--- missing closing of do.
} // <--- Missing closing of main 


Looking at the code this way it helps to what could be in a function. I would also consider using a switch/case in place of all the if/else statements.

Hope that helps,

Andy
1
Last edited on

How are my functions?

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
#include <iostream>


using namespace std;

const double PI = 3.14;

void displayMenu();
void findSquareArea();
void findCircleArea();
void findTriangleArea();

int main()
{
int choice;
while(true)
{
displayMenu();
cout << "Enter your choice: ";
cin >> choice;

if(choice == 1)
findSquareArea();

else if(choice == 2)
findCircleArea();

else if(choice == 3)
findTriangleArea();

else if(choice == 4)
break;

else
cout << "Invalid Input\n";
}
}

void displayMenu()
{
cout << "\n1.Square Area\n2.Circle Area\n3.Triangle Area\n4.Quit\n";
}
void findSquareArea()
{
double side, area;

cout << "\nEnter side: ";
cin >> side;

area = side*side;

cout << "Area of Square: " << area << endl;
}

void findCircleArea()
{
double radius, area;

cout << "\nEnter radius: ";
cin >> radius;

area = PI*radius*radius;

cout << "Area of Circle: " << area << endl;
}

void findTriangleArea()
{
double width, height, area;

cout << "\nEnter width: ";
cin >> width;
cout << "Enter height: ";
cin >> height;

area = 0.5*width*height;

cout << "Area of Triangle: " << area << endl;
}



also... can i add these, they taught me these but i wasnt sure to put them in or not

1
2
3
4
#include <fstream>
#include <string>
#include <cstring>
#include <limits.h 
closed account (E0p9LyTq)
@garza07,

Do you know how to write and call custom functions?

You have your code for each selection contained in your if blocks, simply declare and define a function for each selection:
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
#include <iostream>

using namespace std;

double SelOne();
void SelFour();

int main()
{
   // declare constant variable PI
   const double PI = 3.14159;
   //variables required for the area calculation
   int selection = 0;
   double area = 0.0;
   double radius = 0.0;
   double height = 0.0;
   double base = 0.0;

   cout << "Program to calculate areas of objects:\n";

   // run until 4 is selected.
   while (true)
   {
      cout << "\n1.Square \n2.Circle \n3.Rigth Triangle \n4.Quit";
      cout << "\nEnter your choice:";
      cin >> selection;
      if (selection == 1)
      {
         area = SelOne();
         cout << "\nArea of Square:" << area << '\n';
      }
      else if (selection == 2)
      {
         cout << "\nRadius of circle:";
         cin >> radius;
         area = PI * radius*radius;
         cout << "\nArea of circle:" << area;
      }
      else if (selection == 3)
      {
         cout << "\nHeight of Triangle:";
         cin >> height;
         cout << "\nBase of Triangle:";
         cin >> base;
         area = 0.5*height*base;
         cout << "\nArea of Triangle:" << area;
      }
      else if (selection == 4)
      {
         SelFour();
         break;
      }
      else
      {
         std::cout << "Invalid selection!\n";
      }
   }
}

double SelOne()
{
   double side = 0.0;
   double area = 0.0;

   std::cout << "\nLength of side:";
   cin >> side;
   area = side * side;
   return area;
}

void SelFour()
{
   std::cout << "\nGood bye!\n\n";
}

Program to calculate areas of objects:

1.Square
2.Circle
3.Rigth Triangle
4.Quit
Enter your choice:1

Length of side:12.5

Area of Square:156.25

1.Square
2.Circle
3.Rigth Triangle
4.Quit
Enter your choice:5
Invalid selection!

1.Square
2.Circle
3.Rigth Triangle
4.Quit
Enter your choice:4

Good bye!

You should be able to figure out how to write the other functions you need.
too many else ifs. should use case switch there really.
closed account (E0p9LyTq)
too many else ifs. should use case switch there really.

That can be looked at after the OP rewrites the code to work with functions. That is what they need first.
Topic archived. No new replies allowed.