【问题标题】:My switch case is not working, case is ending early我的 switch 案例不工作,案例提前结束
【发布时间】:2021-12-08 01:07:19
【问题描述】:

我选择1并输入汽车颜色后,程序不询问闲置部分,我该怎么办。输出是这样的: 1.添加一辆车 2.移除汽车 3.显示汽车信息 4.退出

1

输入汽车颜色:黑色

输入车型年份: 输入车型价格: 输入汽车的乘客人数:

case 1:

    cout << "\nEnter car color: ";
    cin >> c.color;

    cout << "\nEnter  car model year: ";
    cin >> c.modelyear;

    cout << "\nEnter  car model price: ";
    cin >> c.carprice;

    cout << "\nEnter number of passengers for car:   ";
    cin >> c.passengers;

    cout << "\nEnter car model (Sedan, SUV, Coupe, Station Wagon, Hatchback):    ";
    cin >> c.model;

【问题讨论】:

  • 在末尾添加break;

标签: visual-studio c++11 visual-c++ error-handling switch-statement


【解决方案1】:

你没有为你的c 结构提供声明,所以我会推测:)

该行为与将c.color 声明为int 一致。当您键入black&lt;Enter&gt; 时,cin 无法将其转换为int,因此它设置了fail 位(我相信)。从那时起,所有后续的cin &gt;&gt; 操作都将失败,直到该位被清除。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多