Computing perimeters,area,Phytagoream Theoram

I need a program that can compute the width,height,lenght of Triangle,Square,Rectangle,Quadrilaterals,circle PERIMETERS FORMULA.Then the next is the width,height,lenght of triangle rectangle square circle parallelogram Using AREA FORMULA.The next is Phytagoream Theorami need to find the angle. This is what i have started just a menu.
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
#include <iostream>
#include <MATH.H>
using namespace std;
int main()
{
		 
		
	int number;
	
	cout<<"[1] Perimeters"<<endl;
	cout<<"[2] Area"<<endl;
	cout<<"[3] Phytagoream Theoram"<<endl;
	cout<<"[4] EXIT"<<endl;
	cout<<"Choose a Number"<<endl;
	
	cin>>number;
{
	if(number==1)
		cout<<"[1] Triangle"<<endl;
		cout<<"[2] Square"<<endl;
		cout<<"[3] Rectangle"<<endl;
		cout<<"[4] Quadrilateral"<<endl;
		cout<<"[5] circle"<<endl;
cin>>number;

if(number==1)
	cout<<"Enter width"<<endl;
	cout<<"Enter height"<<endl;
	cout<<"Enter lenght"<<endl;
	cin>>number;
	
}


return 0;
system("pause");

}
Last edited on
What I got from the preamble is you want to calculate width and height from perimeter but your code is asking for width and height input. And there will be no unique answer to most of them too.
i updated it :) here is it i got problem when i enter number 2 in the perimeters then 2 that is square can you help me pls :) what code should i put to terminate/exit the program after i got the answer from 1 the triangle :)
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
#include <iostream>
#include <MATH.H>
using namespace std;
int main()
{
		 
		
	int number,width,height,lenght;
	
	cout<<"[1] Perimeters"<<endl;
	cout<<"[2] Area"<<endl;
	cout<<"[3] Phytagoream Theoram"<<endl;
	cout<<"[4] EXIT"<<endl;
	cout<<"Choose a Number"<<endl;
	
	cin>>number;
{
	if(number==1)
		cout<<"[1] Triangle"<<endl;
		cout<<"[2] Square"<<endl;
		cout<<"[3] Rectangle"<<endl;
		cout<<"[4] Quadrilateral"<<endl;
		cout<<"[5] circle"<<endl;
cin>>number;
{

if(number==1)
		cout<<"Enter width"<<endl;
		cin>>width;
		cout<<"Enter height"<<endl;
		cin>>height;
		cout<<"Enter lenght"<<endl;
		cin>>lenght;
		cout<<"The Sum is  " <<width + height + lenght<<endl;
		
}
if(number==2){

		cout<<"Enter something"<<endl;
}
}


return 0;
system("pause");

}
Last edited on
Line 17,25: Why is this } here?

Lines 19-23: Only line 19 will be conditional on line 18. Lines 20-23 will execute unconditionally. This is not Python. Scope of a condition is determined by {}, not indentation.

Lines 28-34 ditto.

Line 45: This line will never be reached.
it's not done yet dude that's why im asking for help im making it step by step menu by menu
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
#include <iostream>
#include <MATH.H>
using namespace std;
int main()
{
		 
		
	int number,width,height,lenght;
	
	cout<<"[1] Perimeters"<<endl;
	cout<<"[2] Area"<<endl;
	cout<<"[3] Phytagoream Theoram"<<endl;
	cout<<"[4] EXIT"<<endl;
	cout<<"Choose a Number"<<endl;
	
	cin>>number;
{
if(number==1)
		cout<<"[1] Triangle"<<endl;
		cout<<"[2] Square"<<endl;
		cout<<"[3] Rectangle"<<endl;
		cout<<"[4] Quadrilateral"<<endl;
		cout<<"[5] circle"<<endl;
cin>>number;
}
{

if(number==1)
		cout<<"Enter width"<<endl;
		cin>>width;
		cout<<"Enter height"<<endl;
		cin>>height;
		cout<<"Enter lenght"<<endl;
		cin>>lenght;
		cout<<"The Sum is  " <<width + height + lenght<<endl;
}
{
		
 if (number==2)

		cout<<"Enter theight"<<endl;
		cin>>height;
		cout<<"The total height is  "<<height * 4<<endl;
}


return 0;
system("pause");

}
Last edited on
Here is one way to write a menu loop.

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

using namespace std;


int main() {


    bool exitProgram = false;


    while (!exitProgram) {


        char input;
        cout << "Choose a option" << endl;
        cin >> input;
        cout << "You pressed " << input << ". Press q to quit." << endl;


        if (input == 'q') {
            exitProgram = true;
        }

    }


    cout << "goodbye!" << endl;


    return 0;
}
it's not done yet dude that's why im asking for help im making it step by step menu by menu

So fix the problems I pointed out. Your latest post is really no different than the original. All the problems I pointed out still exist.

Edit: If you don't understand the problems I pointed out, just say so.
Last edited on
I think using switch statements is better in this case.
yup i can't understand what you are saying abstraction :(

i will try using switch statements :)
the problem is i can't cin in the switch statement :(
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
#include<iostream>
using namespace std;

int main()
{
	 int number,width,height,lenght;
	cout<<"[1] Perimeters"<<endl;
	cout<<"[2] Area"<<endl;
	cout<<"[3] Phytagoream Theoram"<<endl;
	cout<<"[4] EXIT"<<endl;
	cout<<"Choose a Number"<<endl;
	cin>>number;
	
	switch(number)
{
	case 1:
		cout<<"[2] Triangle"<<endl;
		cout<<"[3] Square"<<endl;
		cout<<"[4] Rectangle"<<endl;
		cout<<"[5] Quadrilateral"<<endl;
		cout<<"[6] circle"<<endl;
cin>>number;

	break;
	case 2:
		cout<<"Enter width"<<endl;
		cin>>width;
		cout<<"Enter height"<<endl;
		cin>>height;
		cout<<"Enter lenght"<<endl;
		cin>>lenght;
		cout<<"The Sum is  "<<width + height + lenght<<endl;
	break;

	default:
		cout<<"Exit"<<endl;	
			}
system("PAUSE");
return 0;

}
That does look better with the switch statement.

the problem is i can't cin in the switch statement

I don't know what you mean by that. cin is certainly legal inside a switch statement. However, at line 22, you trying to cin number. This is after you branched on it at line 14. It's not a good idea to change your switch index inside your switch statement. That can affect evaluation of subsequent cases (such as line 25). You want to use a different variable at line 22.

Logically you want another switch statement at line 23 to deal with the users input. I consider it poor style to nest switch statements. It makes your code very hard to follow. Instead, I strongly recommend putting lines 17-22 inside a function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void do_perimeter ()
{   int opt2;
    cout<<"[2] Triangle"<<endl;
    cout<<"[3] Square"<<endl;
    cout<<"[4] Rectangle"<<endl;
    cout<<"[5] Quadrilateral"<<endl;
    cout<<"[6] circle"<<endl;
    cin>> opt2;
    switch (opt2)
    {
    case 2:  // deal with perimeter of triangle
                 break;
    case 3: // deal with perimeter of square
                break;
...  // etc
    }
}


Lines 14-24 of main become:
1
2
3
4
5
6
7
8
  switch (number)
  { 
  case 1:  do_perimeter ();
               break;
  case 2:  do_area ();
               break ();
...  // replicate for other attributes
  }


I would suggest prompting for the shape first, then the attribute of the shape. i.e. A square and a circle have different attributes. i.e. For a circle, you want to prompt for the radius, but radius makes no sense for the other types (triangle, square, rectangle, etc).

edit: regarding my original comments:
Line 17,25: You have your {} is strange places. Your original code looks like this:
1
2
3
4
5
{
    if (some_condition)
      statement1;
      statement2;
}

Those {} introduce a local scope. They have no bearing on what is executed depending on the evaluation of the if. Your indentation implies you want want statement1 and statement2 to be conditional on the if. The correct way to do that is as follows:
1
2
3
4
  if (some_condition)
  { statement1;
     statement2;
  }

Without the braces, only statement1 is dependent on the if condition being evaluated as true. statement2 is executed unconditionally.
Last edited on
It seems op is asking how to run a menu loop with a switch but is having issues asking the question. I will chalk it up to being a case of "You don't know what you don't know"

Here is an example switch with a menu loop.

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

using namespace std;


int main() {


    bool exitProgram = false;


    while (!exitProgram) {


        char grade;
        cout << "enter a grade or press q to quit." << endl;
        cin >> grade;

        // http://www.tutorialspoint.com/cplusplus/cpp_switch_statement.htm


        switch (toupper(grade)) // making all input uppercase
        {
            case 'A' :
                cout << "Excellent!" << endl;
                break;
            case 'B' :
            case 'C' :
                cout << "Well done" << endl;
                break;
            case 'D' :
                cout << "You passed" << endl;
                break;
            case 'F' :
                cout << "Better try again" << endl;
                break;
            case 'Q' :
                exitProgram = true;
                break;
            default :
                cout << "Invalid grade" << endl;
        }

        if (!exitProgram)
            cout << "Your grade is " << grade << endl;
        
    }


    cout << "goodbye!" << endl;


    return 0;
}
Last edited on
im done
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include<iostream>
#include<math.h>
using namespace std;

int triangle(int x, int y, int z )
{
  return x+y+z;
}

int square(int x)
{
  return x*4;
}

int rectangle(int x, int y)
{
  return x+y;
}
int quadrilateral(int w, int x, int y, int z)
{
 return w+x+y+z;
}
int pentagon(int v, int w, int x, int y, int z)
{
 return v+w+x+y+z;
}

float areaT(int x, int y, float a)
{
 return a*x*y;
}
int areaS(int x)
{
 return pow(x,2);
}
int areaR(int x, int y)
{
 return x*y;
}
float areaTr(int x, int y, int z, float a)
{
 return a*(x+y)*z;
}
int areaP(int x, int y)
{
 return x*y;
}
float PTa(float x, float y)
{
 return sqrt(pow(x,2)-pow(y,2));
}
float PTb(float x, float y)
{
 return sqrt(pow(x,2)-pow(y,2));
}
float PTc(float x, float y)
{
 return sqrt(pow(x,2)+pow(y,2));
}



int main()

{
 int cho,per,v,w,x,y,z,res,sum;
 float a=0.5;

 menu:
 system("cls");
 cout<<"[1] Perimeter"<<endl;
 cout<<"[2] Area "<<endl;
 cout<<"[3] Phytagorean Theorem"<<endl;
 cout<<"[4] Exit"<<endl<<endl;
 cout<<"Your Choice: ";
 cin>>cho;
 switch(cho)
 {
  
  case 1: 
  {
   system("cls");
   cout<<"PERIMETER"<<endl;
   cout<<"[1] triangle"<<endl;
   cout<<"[2] square"<<endl;
   cout<<"[3] rectangle"<<endl;
   cout<<"[4] quadrilateral"<<endl;
   cout<<"[5] pentagon"<<endl<<endl;
   cout<<"Your choice: ";
   cin>>cho;
   switch(cho)
   {
    case 1:
    system("cls");
    cout<<"TRIANGLE"<<endl;
    cout<<"enter the three sides of the triangle"<<endl;
    cout<<"first side: ";
    cin>>x;
    cout<<endl;
    cout<<"second side: ";
    cin>>y;
    cout<<endl;
    cout<<"third side: ";
    cin>>z;
    cout<<endl;
    cout<<"the perimeter of the triangle is: "<<triangle(x,y,z)<<endl;
    system("pause");
    goto menu;
    break;
    
    case 2:
    system("cls");
    cout<<"SQUARE"<<endl;
    cout<<"enter the side of the square"<<endl;
    cin>>x;
    cout<<"the perimeter of the square is: "<<square(x)<<endl;
    system("pause");
    goto menu;
    break;
    
    case 3:
    system("cls");
    cout<<"RECTANGLE"<<endl;
    cout<<"enter the width and height of the rectangle"<<endl;
    cout<<"width: ";
    cin>>x;
    cout<<"height: ";
    cin>>y;
    cout<<"the perimeter of the rectangle is: "<<rectangle(x,y)*2<<endl;
    system("pause");
    goto menu;
    break;
    
    case 4:
    system("cls");
    cout<<"QUADRILATERAL"<<endl;
    cout<<"enter the four sides of the quadrilateral"<<endl;
    cout<<"first side: ";
    cin>>w;
    cout<<endl;
    cout<<"second side: ";
    cin>>x;
    cout<<endl;
    cout<<"third side: ";
    cin>>y;
    cout<<endl;
    cout<<"fourth side: ";
    cin>>z;
    cout<<endl;
    cout<<"the perimeter of the quadrilateral is: "<<quadrilateral(w,x,y,z)<<endl;
    system("pause");
    goto menu;
    break;
    
    case 5:
    system("cls");
    cout<<"PENTAGON"<<endl;
    cout<<"enter the five sides of the pentagon"<<endl;
    cout<<"first side: ";
    cin>>v;
    cout<<endl;
    cout<<"second side: ";
    cin>>w;
    cout<<endl;
    cout<<"third side: ";
    cin>>x;
    cout<<endl;
    cout<<"fourth side: ";
    cin>>y;
    cout<<endl;
    cout<<"fifth side: ";
    cin>>z;
    cout<<endl;
    cout<<"the perimeter of the pentagon is: "<<pentagon(v,w,x,y,z)<<endl;
    system("pause");
    goto menu;
    break;
    default: 
    cout<<"wrong choice go back to menu!!"<<endl;
    system("pause");
    goto menu;
    break;
   }
   break;
   
  case 2:
   system("cls");
   cout<<"AREA"<<endl;
   cout<<"[1] triangle"<<endl;
   cout<<"[2] square"<<endl;
   cout<<"[3] rectangle"<<endl;
   cout<<"[4] trapezoid"<<endl;
   cout<<"[5] parallelogram"<<endl<<endl;
   cout<<"Your choice: ";
   cin>>cho;
   switch(cho)
   {
    case 1:
     system("cls");
     cout<<"AREA OF A TRIANGLE"<<endl;
     cout<<"enter the base and height of the triangle"<<endl;
     cout<<"Base: ";
     cin>>x;
     cout<<"Height: ";
     cin>>y;
     cout<<"The Area of tha triangle is "<<areaT(x,y,a)<<endl;
     system("pause");
     goto menu;
     break;
    case 2:
     system("cls");
     cout<<"AREA OF A SQUARE"<<endl;
     cout<<"enter the length of side of the square"<<endl;
     cout<<"Side: ";
     cin>>x;
     cout<<"The Area of tha square is "<<areaS(x)<<endl;
     system("pause");
     goto menu;
     break;
    case 3:
     system("cls");
     cout<<"AREA OF A RECTANGLE"<<endl;
     cout<<"enter the width and height of the triangle"<<endl;
     cout<<"Width: ";
     cin>>x;
     cout<<"Height: ";
     cin>>y;
     cout<<"The Area of tha rectangle is "<<areaR(x,y)<<endl;
     system("pause");
     goto menu;
     break;
    case 4:
     system("cls");
     cout<<"AREA OF A TRAPEZOID"<<endl;
     cout<<"enter the two sides and the vertical height of the trapezoid"<<endl;
     cout<<"side a: ";
     cin>>x;
     cout<<"side b: ";
     cin>>y;
     cout<<"vertical height: ";
     cin>>z;
     cout<<"The Area of tha trapezoid is "<<areaTr(x,y,z,a)<<endl;
     system("pause");
     goto menu;
     break;
    case 5:
     system("cls");
     cout<<"AREA OF A PARALLELOGRAM"<<endl;
     cout<<"enter the base and height of the parallelogram"<<endl;
     cout<<"Base: ";
     cin>>x;
     cout<<"Height: ";
     cin>>y;
     cout<<"The Area of tha parallelogram is "<<areaP(x,y)<<endl;
     system("pause");
     goto menu;
     break;
    default:
     cout<<"Wrong choice go back to menu!!"<<endl;
     system("pause");
     goto menu;
     break;
     
   }
   break;
   
  case 3:
   system("cls");
   cout<<"PYTHAGOREAN THEOREM"<<endl;
   cout<<"What is missing?"<<endl;
   cout<<"[1] Side A"<<endl;
   cout<<"[2] Side B"<<endl;
   cout<<"[3] Side C (Hypothenus) "<<endl<<endl;
   cout<<"Your choice: ";
   cin>>cho;
   switch (cho)
   {
    case 1:
     system("cls");
     cout<<"Side A is missing"<<endl;
     cout<<"Enter Side C (hypothenus): ";
     cin>>x;
     cout<<"Enter Side B:  ";
     cin>>y;
     cout<<"Side A = "<<PTa(x,y)<<endl;
     system("pause");
     goto menu;
     break;
    case 2:
     system("cls");
     cout<<"Side B is missing"<<endl;
     cout<<"Enter Side C (hypothenus): ";
     cin>>x;
     cout<<"Enter Side A: ";
     cin>>y;
     cout<<"Side B = "<<PTb(x,y)<<endl;
     system("pause");
     goto menu;
     break;
    case 3:
     system("cls");
     cout<<"Side C is missing"<<endl;
     cout<<"Enter Side A: ";
     cin>>x;
     cout<<"Enter Side B: ";
     cin>>y;
     cout<<"Side C (hypothenus) = "<<PTc(x,y)<<endl;
     system("pause");
     goto menu;
     break;
  
   
   }
   break;
  case 4:
   return 0;
  break;
  
  default:
   system("cls");
   cout<<"wrong choice go back to menu!!"<<endl;
   system("pause");
   goto menu;
   break;
  }
  
  
 }

  system("pause");
  return 0;

  
} 
I heard using OS specific things like system("cls") etc are bad? Is it true?
system("cls") can be bad for two reasons. First, it's much slower than clearing the screen directly. That's because it starts the command line processor, which usually has some initialization that it does, then the command processor has to parse the command and finally do whatever the command is.

The second reason is that it's usually insecure. Unless you know what you're doing, it's very easy to write code that can be coerced into running whatever a hacker wants.
Topic archived. No new replies allowed.