if and switch statement need help

im not sure what im doing wrong im trying to use if and switch

im trying to find the area of a rectangle or a triangle so i want the user to type in triangle or rectangle
and i must use a if statement
is their any other shorter way
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 <string>
#include <cmath>
using namespace std;

int main() //start of main function
{ 
int object;
float triangle;
float rectangle;
cout << "type the shape triangle or rectangle" << endl;
cin >> object;

if (object == triangle)
{ cout << "the area to the triangle is " << endl;
cin >> case 1;
}
if (object == rectangle)
{cout << " the area to the rectangle is " << endl;
cin >> case 2;
}
else
{ cout << " chosse triangle or rectangle" << endl;
}
{
case 1:
int side; // input of the side
cout << "Input length of side: "; // putting in the variable
cin >> side; // stores input into variable side
cout << endl;
int area = side * side; // calculating the area
cout << "area is: " << area << endl; // the out put
}
{
case 2:
float base, height;
cout << "Find the Area of Triangle" << endl; // input
cout << " Enter Base : "; //input
cin >> base; // output
cout << " Enter Height : ";
cin >> height;
}


system ("pause");
return 0;
}
Last edited on
you can read if and switch statement here http://www.cplusplus.com/doc/tutorial/control/, you are absolutely doing it wrong

Holy C++....

This looks like code I produced in the first week or two of programming... For that reason Daniel, I'm gonna give you this one. This is your freebie...

Variable names like count, Object, array, and other keywords and terms we use daily are a very bad idea. You could open a Pandora's box. Little kitty's wouldn't climb trees, and puppys would cringe in fear of butterflies... You don't want to live with that on your conscious. Lets use some other names that are meaningful for our variables.



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
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cmath>
using namespace std;

int main() //start of main function
{

int geodesign;
float triangle;
float rectangle;
cout << "Please select a shape\n1. Triangle\n2. Rectangle" <<endl;
cin >> geodesign;

switch (geodesign){
case 1:
    {
            cout << "Please enter the area to the triangle ";
            cin >> triangle;
  // Do things here

break;
}

case 2:
    {

                cout << "Please enter the area to the rectangle " ;
                cin >> rectangle;

  // Do things here

break;
}

default :
    {
        cout << "Whoops, we made a boo boo.\nNext time press 1 or 2 please." << endl;
    }
}

system ("pause");
return 0;
}
Last edited on
First thing you must know is that if/else is a statement or series of statements as well switch is it's own series of statements. If you try to combine two different types of statements you will get compiler errors.

second is that it is bad practice to write code without checking it with the compiler every now and then, especially if you are starting out.

here are examples on how to use if/else statements and a switch statement


if/else statement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

using namespace std;

int main() {

    int x;
    cout << "This program will use an if/else statements for > or < than 50." << endl << endl;
    cout << "Please enter a number and I will repeat it back to you: ";
    cin >> x;
    if (x > 50) {
        cout << "Your number: " << x << '\n' << "is above 50." << endl;
    }else{
        cout << "Your number: " << x << '\n' << "is below 50." << endl;
    }

    return 0;
}


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

using namespace std;

int main() {
    char letter;
    cout << "This program tests a switch statement." << endl;
    cout << "Enter A, B, C, or D: ";
    cin >> letter;

    switch(letter) {
    case 'A':
        cout << "You entered " << letter << endl;
        break;
    case 'B':
        cout << "You entered " << letter << endl;
        break;
    case 'C':
        cout << "You entered " << letter << endl;
        break;
    case 'D':
        cout << "You entered " << letter << endl;
        break;
    }
    return 0;
}


This does not mean that you can't use both. You just can't combine switch and if.
Topic archived. No new replies allowed.