首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >我可以在switch语句中包含cin吗

我可以在switch语句中包含cin吗
EN

Stack Overflow用户
提问于 2016-10-09 06:19:11
回答 4查看 608关注 0票数 1

我正在写一个程序,将给出一个形状的面积,我必须创建一个菜单,让用户选择一个形状使用开关。所以我的问题是,我可以使用cin和开关情况,或者我必须以不同的方式格式化我的代码。

代码语言:javascript
运行
复制
#include <cmath>
    #include <iostream> 
    #include <cassert>
    using namespace std;

    int main()
    {
    int shape, invalid;
    double area, radius, width, height;

    const double pi=3.14159;

    cout << "Shape Menu"<<endl<< "1. Circle"<<endl<<"2. Rectangle"<<endl<<"3. Triangle"<<endl
    <<"shape (1, 2, or 3)? ";
    cin >> shape;


        switch (shape){
            case 1: cout << "Radius? "<<;
            cin >> radius >> endl;break;    // this is were my error is when I compile 
            case 2: cout << "width? ";
            cin >> width >> endl;
            cout << "Height? ";
            cin >> height >> endl;break;
            case 3: cout<< "Base? ";
            cin >> base >> endl;
            cout << "Height? ";
            cin >> height >> endl;break;
            default: invalid = shape 
            cout<< shape << "is an invalid menu option, program terminated."<<endl;
            assert (invalid == T)
        }


        return 0;

    }
EN

回答 4

Stack Overflow用户

发布于 2016-10-09 06:20:53

case 1: cout << "Radius? "<<;你在"Radius?"之后有一个流浪的<<

票数 0
EN

Stack Overflow用户

发布于 2016-10-09 06:28:50

是的,您可以在switch语句的cases之后包含cin。像case 1:这样的案例仅仅是标签。所以后面的指令可以是任何指令。

您的代码有许多编译错误:下面是switch语句的一个可能的替代品。

代码语言:javascript
运行
复制
    switch (shape){
        case 1: cout << "Radius? ";
        cin >> radius;break;    // no more error here 
        case 2: cout << "\nwidth? ";
        cin >> width;
        cout << "\nHeight? ";
        cin >> height;break;
        case 3: cout<< "Base? ";
        cin >> base;
        cout << "\nHeight? ";
        cin >> height;break;
        default:
        invalid = shape;
        cout<< shape << "is an invalid menu option, program terminated."<<endl;
    }
票数 0
EN

Stack Overflow用户

发布于 2016-10-09 07:00:16

您可以使用枚举和开关进行fullfil:

代码语言:javascript
运行
复制
#include <iostream>

enum SHAPES{CIRCLE = 1, SQUARE, RECTANGLE, TRIANGLE, CUBE};


int main()
{
    int choice;
    std::cout << "1: Circle  2: Square  3: Rectangle  4: Triangle  5: Cube" << std::endl;
    std::cout << " >> ";
    std::cin >> choice;
    std::cout << std::endl;

    switch(choice)
    {
        case CIRCLE:
        {
            //do something
        }
        break;
        case SQUARE:
        {
            //do something
        }
        break;
        case RECTANGLE:
        {
            //do something
        }
        break;
        case TRIANGLE:
        {
            //do something
        }
        break;
        case CUBE:
        {
            //do something
        }
        break;
        default:
            std::cout << "Bad Entry!" << std::endl;
    }

    std::cout << std::endl;
    return 0;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39937944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档