Square or Triangle?

Pages: 12
Hey guys :)

I'm suppose to write a C++ code that computes and displays the area of a square or a triangle after prompting its user to type in the first letter of the desired figure name. The program MUST use a switch statement.

Here is what I have!
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
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
	int h, b, a, figure;
	double t = 0.5*h*b;
	double s = a*a;

	cout << "Please enter the type of figure (t) for triangle and (s) for square \n" << endl;

	switch (figure)
	{
case t:
		cout << "Please enter a positive integer value for the base in inches \n"<<b << endl;
		cout << "Please enter a positive integer value for the height in inches \n" << h<< endl;
		cout << "The area of the triangle is \n" << 0.5*h*b << endl;

case s:
		cout << "Please enter the  length of a side in inches \n" << a << endl;
		cout << "The area of the square is \n" << a*a  << endl;

default:
		cout << "The type of figure you have selected is invalid \n" <<endl;

	}

	return 0;

}


There might be quite a few errors as I really have no clue how to do Switch.
a) You never ask for input. You need cin >> figure; before your switch.
b) 'figure' should be a char, since you want them to intput 't' or 's'.
c) Your cases are wrong. There is no variable t or s. You want the characters 't' and 's'.
d) Switch doesn't stop at the end of a case. It stops when it reaches "break;". Unless you want case 't' to do case 't', case 's' AND default, you need to add "break;" statements at the end of each case [except the default].

[edit]

I just noticed there ARE variables 't' and 's', but they make no sense. Get rid of them.
You can't assign a function to a variable. You can only assign a value (or result of a function) to a variable. In your example, a, h and b have no (useful) value. What you're doing is t = 0.5*nonsense*nonsense, which equals nonsense.

You're already doing the calculations in your cout statements, so get rid of variables t and s.
Last edited on
Your using variables, h,b and a, before they have a value.Your don't ask for the input of the figure type or for any of the variables you ask for someone to enter. You're using the t variable as a double, and then as a char with the switch. The variable 'figure', should also be a char. Then use cin >> figure. The t and s in the case should have a single quote around them.
1
2
case 't':
case 's':
. Hopefully this will get you close to a working program.
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
int main ()
{
	int h, b, a, t, s;
	char figure;
	
	cout << "Please enter the type of figure (t) for triangle and (s) for square \n" << endl;
	cin >> figure;
	switch (figure)
	{
case 1:
		cout << "Please enter a positive integer value for the base in inches \n"<<b << endl;
		cout << "Please enter a positive integer value for the height in inches \n" << h<< endl;
		cout << "The area of the triangle is \n" << 0.5*h*b << endl;
		break;

case 2:
		cout << "Please enter the  length of a side in inches \n" << a << endl;
		cout << "The area of the square is \n" << a*a  << endl;
		break;
default:
		cout << "The type of figure you have selected is invalid \n" <<endl;
		break;

	}

	return 0;

}


It still has an error in there, with the cases being wrong. I
Last edited on
char figure;

cout << "Please enter the type of figure (t) for triangle and (s) for square \n" << endl;

switch (figure)

case 1:

can you see the conflict here ?

You want the user to enter 't' or 's' but in the switch the two cases are 1 and 2 (???)

It must be case 't' and case 's' because the switch take action based on the value of figure.figure is char and must take 't' or 's' so you should change the two case lines.

and also :

char t, s;

P/S remember 't' is of type char but t is not char...

If that still bugged you,I edited your code...see and compare with what I said :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main ()
{
	int h, b, a;
        char t, s;
	char figure;
	
	cout << "Please enter the type of figure (t) for triangle and (s) for square \n" << endl;
	cin >> figure;
	switch (figure)
	{
case 't':

//WHATEVER HERE
case 's':
//SOME MORE
default:
		cout << "The type of figure you have selected is invalid \n" <<endl;
		break;

	}

	return 0;

}


EDIT fixed the variable declaration
Last edited on
I can do this program with if/else. I'm just have a bit of trouble converting it. For instance, when I plug in t or s into the compiler, it doesn't state whether it is a triangle or a square, rather, it states that the figure is invalid.
P/S remember 't' is of type char but t is not char...
Your case should be case 't':, case 's': not case 1: and case 2: Also, you're not not inputting any information. Should be more line
1
2
cout << "Please enter a positive integer value for the base in inches ";
cin >> b;
Do that with each place you ask for information, but of course, use cin >> corresponding variable b, h or a
Last edited on
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
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
	int h, b, a;
	char figure;
	char t;
	char s;
	
	cout << "Please enter the type of figure (t) for triangle and (s) for square \n" << endl;
	cin >> figure;

	switch (figure)
	{
case 't':

		cout << "Please enter a positive integer value for the base in inches \n"<<b << endl;
		cin >> b;
		
		cout << "Please enter a positive integer value for the height in inches \n" << h<< endl;
		cin >> h;
		
		cout << "The area of the triangle is \n" << 0.5*h*b << endl;
		break;

case 's':
		cout << "Please enter the  length of a side in inches \n" << a << endl;
		cin >> a;

		cout << "The area of the square is \n" << a*a  << endl;
		break;
default:
		cout << "The type of figure you have selected is invalid \n" <<endl;
		break;

	}

	return 0;

}
Does it work now?

In either case: why do you still have "char t" and "char s" in there? Those are unused variables.
cout << "Please enter the length of a side in inches \n" << a << endl;

cin >> a;

before the cin >> a; a is not assigned (say,have) any value.

so don't bother output it.

So the solution should be :

cout << "Please enter the length of a side in inches \n" << endl;
It compiles yes, howver the program itself has errors. When the first prompt comes up, of pick s or t, upon t being inputted, an error comes up saying that the variable b is not initialized and to either abort/ignore/retry the program.

When s is inputted, it says that the variable a is not initialized and to either abort/ignore/retry the program.

See my reply above.
I did, I made the changes that yuo suggested.
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
#include <iostream>
#include <cmath>

using namespace std;

int main ()
{
	int h, b, a;
	char figure;

	
	cout << "Please enter the type of figure (t) for triangle and (s) for square \n" << endl;
	cin >> figure;

	switch (figure)

	{

case 't':

		cout << "Please enter a positive integer value for the base in inches \n"<<b << endl;
		cin >> b;
		
		cout << "Please enter a positive integer value for the height in inches \n" << h<< endl;
		cin >> h;
		
		cout << "The area of the triangle is \n" << 0.5*h*b << endl;
		break;

case 's':
		cout << "Please enter the  length of a side in inches \n" << a << endl;
		

		cout << "The area of the square is \n" << a*a  << endl;
		break;

default:
		cout << "The type of figure you have selected is invalid \n" <<endl;
		break;

	}

	return 0;

}
So what's the code now ?

Not just

cout << "Please enter a positive integer value for the base in inches \n"<<b << endl;

Change also :

cout << "Please enter a positive integer value for the height in inches \n" << h<< endl;

cout << "Please enter the length of a side in inches \n" << a << endl;

in the same way yourself.

EDIT :

In general, you CANNOT output what is not inputted.
Last edited on
So its working for t (yay!!) but for s, it keeps bringing up the error of a not being initialized!
Does the line
1
2
cout << "Please enter a positive integer value for the base in inches \n"<<b << endl;
		cin >> b;
, still look this way? If yes, change them. None of them should have the variable at the end of the cout. It should like cout << "Please enter a positive integer value for the base in inches " << endl; If you did make the changes, and it's still not running properly, try posting the code you have now, and we'll take a look at what the problem may be.
Change

cout << "Please enter the length of a side in inches \n" << a << endl;

to

cout << "Please enter the length of a side in inches \n" << endl;

and

cin >> a;
Last edited on
It;s working guys :) Just forgot a cin >> a;

Thanks for the help guys :) Much MUCH appreciated!
Pages: 12