Wrong output for correct code

So I had to create this project for a course I am taking, and for some reason keeps outputting the circle calculations. How do I fix this to output other shape calculations when I enter other characters?

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int main()
{
string shapeChoice = "";
float rectL;
float rectW;
float radius;
float triH;
float triB;
int shapeNumber;
cout << "****** Geometric Figure Calculator ******" << endl;
cout << "\n";
cout << " C - Calculate the area of a Circle" << endl;
cout << " R - Calculate the area of a Rectangle" << endl;
cout << " T - Calculate the area of a Triangle" << endl;
cout << " Q - Quit" << endl;
cout << "\n";
cout << " Enter your choice: ";
cin >> shapeChoice;
cout << "-----------------------------------------" << endl;

if (shapeChoice == "C" or "c")
{
shapeNumber = 1;
}
else if (shapeChoice == "R" or "r")
{
shapeNumber = 2;
}
else if (shapeChoice == "T" or "t")
{
shapeNumber = 3;
}
else if (shapeChoice == "Q" or "q")
{
shapeNumber = 4;
}

switch(shapeNumber)
{
case 1:
{
cout << "Please enter the radius of the circle: ";
cin >> radius;
cout << "\n";
double circleRadius = M_PI * pow(radius, 2);
cout << std::setprecision(6) << "The area of a circle with a radius of " << radius << " is: " << circleRadius;
break;
}

case 2:
{
cout << "Please enter the rectangle's length and width (separated by a space): ";
cin >> rectL, rectW;
cout << "\n";
cout << std::setprecision(6) << "The area of a triangle with a length of" << rectL << "and a width of " << rectW << " is " << rectL*rectW;
break;
}

case 3:
{
cout << "Please enter the triangle's base: ";
cin >> triB;
cout << "\n";
cout << cout << "And height : ";
cin >> triH;
cout << "\n";
cout << std::setprecision(6) << "The area of a triangle with a base of " << triB << " and a height of " << triH << " is " << triH * triB / 2;
break;
}

case 4:
{
cout << "Program will now quit.";
}
}



return 0;
}
Last edited on
if (shapeChoice == "C" or "c")

c++ does not understand this, and it becomes if shape =="C" or TRUE
which is then resolved as "TRUE"
you must say it explicitly eg:
if (shapeChoice == "C" or shapeChoice == "c")
{

As a side note, your compiler will warn you about this if you turn on all the warnings. Conditions are one of the most easy things to mess up -- they are simple, and people can rush past them with mistakes that are not obvious to fix, such as = instead of == (compiler also warns here if you let it) or inverted logic (and /or mixups) which are not warned (just gotta figure them out if messed up).
Last edited on
It worked! Thank you!
Topic archived. No new replies allowed.