User Input To Choose An Operation

i'm writing a program that allows the user to choose to find the area of three different geometric shapes by inputting a letter, but i can't figure out what's wrong in the code thats not making the program actually choose one when its run:
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
// Geometry 
// Calculates area of a circle, triangle or rectangle


#include <cmath>  
#include <iostream>
#include <iomanip>
#include <string>


const  double PI = 3.14159;

using namespace std;

int main()
{ 

  char shape;              // input by the user to 
                           // choose T(riangle) , C(ircle), or R(ectangle)

  double area ;           
  
  double length, width;   // for the rectangle
  double radius;          // for the circle
  double base, height;    // for the triangle

                                   
  cout << "This program will calculate the area of a "
       << "\ncircle, a rectangle or a triangle.\n"
       << " \nto calculate a circle area, input C (or c) " 
       << " \nto calculate a rectangle area, input R (or r )"
       << " \nto calculate a triangle area, input T ( or t)"
       << " \n\nInput C, T, or R ";
       cin >> shape;
       cout << endl;
  
  
     if (shape == 'C'||'c')
     {
     cout << "Input The Radius Of The Circle: " << endl;
     cin >> radius;
     cout << endl;
     
     area = PI * pow(radius, 2);
     
     cout << area << endl << endl;
     }
     
     if (shape == 'R'||'r')
     {
     cout << "Input The Length Of The Rectangle: " << endl << endl;
     cin >> length;
     cout << "Input The Width Of The Rectangle: " << endl << endl;
     cin >> width;
     
     area = length * width;
     
     cout << area << endl << endl;
     }
     
     if (shape == 'T'||'t')
     {
     cout << "Input The Base Of The Triangle: " << endl << endl;
     cin >> base;
     cout << "Input The Height Of The Triangle: " << endl << endl;
     cin >> height;
     
     area = .5 * base * height;
     
     cout << area << endl << endl;
     }
     
     if (shape == !'T'|!'t'|!'C'|!'c'|!'R'|!'r')
     cout << "Invalid Response Is Invalid." << endl << endl;
  
  
  system("pause");
  return 0;
}
You need to fix all of your ifs. They should be like:

shape == 'C' || shape == 'c'

etc, etc.
hint:
if (shape == 'C' || shape == 'c')

'C'||'c' will generate a value that will be compared with shape and is not going to be what you were looking for.

DOH, jsmith has quicker typing skills than me
Last edited on
ohhh, okay, thanks, i thought it might be a problem with the if's, and for the last one, it's supposed to basically output that the user typed an invalid character, i.e. not C,R, or T, so should i make that if similar to the other ones, or can I just use an 'else' statement?
You can use
1
2
3
4
5
6
7
8
9
10
11
12
if()
{
}
else if()
{
}
else if()
{
}
else
{
}

or lookup the switch() and case and default statments as an optional way of doing this.
Last edited on
aha! thanks so much! i was wondering why the code would run, but it would still cout invalid response even if it chose the right option and everything, thanks for the help!
Topic archived. No new replies allowed.