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.
#include <iostream>
#include <cmath>
usingnamespace 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.
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.
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
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 :
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;
}
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.
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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.
#include <iostream>
#include <cmath>
usingnamespace 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;
}
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.