c++ 中 switch 块变量的问题

trouble with switch block variable in c++

我有一个 class 的任务,我要使用开关块来允许用户 select 一个形状,然后确定该形状的区域。下面是这个 switch 语句的代码(对于这个赋值来说可能不必要地冗长和复杂)。 C++ 对我来说是新手,所以现在我的问题是我无法将 work/apply 的“形状”字符传递给开关块。我 运行 代码,它总是进入默认值,不接受 1、2、3 或 4 的输入。

#include <cmath>
#include <iostream>
using namespace std;
int main() {
    // variables:
    float area, radius, length, width, base, height, a, b;
    char shape;
    // input
    cout << "Please enter the number corresponding with one of the following "
            "shapes:"
         << endl
         << endl;
    cout << "1. circle" << endl;
    cout << "2. rectangle" << endl;
    cout << "3. triangle" << endl;
    cout << "4. trapezoid" << endl << endl;
    cin >> shape;
    cout << "\n";
    // SWITCH statement:
    switch(shape) {
        {
        case(1):;
            cout << "You have selected: circle" << endl;
            cout << "Please enter the radius of the circle. " << endl;
            cout << "radius: ";
            cin >> radius;
            area = M_PI * pow(radius, 2);
            cout << "The radius entered: [" << radius
                 << "] results in a circle with an area of " << area << ".";
            break;
        }
        {
        case(2):;
            cout << "You have selected: rectangle" << endl;
            cout << "Please enter the length and width of the rectangle. "
                 << endl;
            cout << "length: ";
            cin >> length;
            cout << "width: ";
            cin >> width;
            area = length * width;
            cout << "The length: [" << length << "] and width [" << width
                 << "] entered result in a rectangle with an area of " << area
                 << ".";
            break;
        }
        {
        case(3):;
            cout << "You have selected: triangle" << endl;
            cout << "Please enter the base and height of the triangle: "
                 << endl;
            cout << "base: ";
            cin >> base;
            cout << "height: ";
            cin >> height;
            area = 0.5 * base * height;
            cout << "The base [" << base << "] and height [" << height
                 << "] entered result in a triangle with an area of " << area
                 << ".";
            break;
        }
        {
        case(4):;
            cout << "You have selected: trapezoid" << endl;
            cout << "Please enter the height and lengths of sides a & b: "
                 << endl;
            cout << "height: ";
            cin >> height;
            cout << "side a: ";
            cin >> a;
            cout << "side b: ";
            cin >> b;
            area = ((a + b) / 2) * height;
            cout << "The height [" << height << "], side a [" << a
                 << "], and side b [" << b
                 << "] entered result in a trapezoid with an area of " << area
                 << ".";
            break;
        }
    default: {
        cout
            << "The value entered is not a number that corresponds with one of "
               "the available shapes. Please try again with a number that does."
            << endl;
    }
    }
}

显然我做错了什么,我只是希望它是什么 simple/straightforward。任何 insight/advice 不胜感激!!

将形状声明为 'int' 将解决问题

已修复!谢谢您的帮助!我没有将值“shape”标记为整数,而是尝试创建一个字符,菜鸟错误,我知道......还向向我展示代码格式化程序的人大喊大叫,这很有帮助,我会使用它在未来。