如何解决这个问题:C ++ switch语句

时间:2016-05-23 16:35:08

标签: c++

我的// number of crowns// frame color

中的切换语句出现问题

这是我的输出enter image description here

如何展示我的cout输出frame_colornumber_crowns// number of crowns// frame color)?

即时通讯使用CodeBlocks

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

int main()
{
    int length, width, number_crowns;
    double cost;
    int frameType;
    int yes_no;
    int colorSelection;
    string frame_color;
    string type;

    cout << "Input length and width of the picture: ";
    cin >> length >> width;

    cout << "1(spacebar)regular" << endl
         << "2(spacebar)fancy" << endl
         << "(regular/fancy)Enter type of frame: ";
    cin >> frameType >> type;

    cout << "1 = with" << endl
         << "2 = without" << endl
         << "(with/without)With color/Without?";
    cin >> colorSelection;

    cout << "1 = yes" << endl
         << "2 = no" << endl
         << "(yes/no)Do you want to put crowns? ";
    cin >> yes_no;

    cost = (2 * 0.1) * (length + width);               // cost in length and width of the frame
    cost += (0.02 * (length * width));                 // cost per square inch

    switch(frameType)
    {                                 // type of frame
    case '1':
        cost += 0.15;
        break;
    case '2':
        cost += 0.25;
        break;
    default:
        cout << "wrong input!";
    }

    switch(colorSelection)
    {                           // frame color
    case '1':
        cost += 0.10;
        cout << "Enter desired frame color: ";
        cin >> frame_color;
        break;
    case '2':
        cost += 0;
        break;
    default:
        cout << "wrong input!";
    }

    switch(yes_no)
    {                                 // number of crowns
    case '1':
        cout << "Input number of crowns: ";
        cin >> number_crowns;
        cost += (number_crowns * 0.35);
        break;
    case '2':
        cost =+ 0;
        break;
    default:
        cout << "wrong input!";
    }

    cout << endl;
    cout << "\n\nFrame type: " << type << "\n" << endl;
    cout << "Frame colour: " << frame_color << "\n" << endl;
    cout << "Total frame cost: " << cost << "\n" << endl;
}

非常感谢!

最新更新W *** wahahaha

如何展示我的cout输出frame_colornumber_crowns// number of crowns// frame color)?

#include <iostream>

using namespace std;

int main()
{
    int length, width, number_crowns;
    double cost;
    char frameType;
    char yes_no;
    char colorSelection;
    string frame_color;
    string type;

    cout << "Input length and width of the picture: ";
    cin >> length >> width;

    cout << "(regular/fancy) Enter type of frame: ";
    cin >> frameType >> type;

    cout << "(with/without) With color/Without?";
    cin >> colorSelection;

    cout << "(yes/no) Do you want to put crowns? ";
    cin >> yes_no;

    cost = (2 * 0.1) * (length + width);               // cost in length and width of the frame
    cost += (0.02 * (length * width));                 // cost per square inch

    cout << "\nFrame type: " << frameType << type << "\n" << endl;
    switch(frameType)
    {                                 // type of frame
    case 'regular':
        cost += 0.15;
        break;
    case 'fancy':
        cost += 0.25;
        break;
    default:
        cout << "wrong input!";
    }

    cout << "\nFrame colour: " << frame_color << "\n" << endl;
    switch(colorSelection)
    {                           // frame color
    case 'with':
        cost += 0.10;
        cout << "Enter desired frame color: ";
        cin >> frame_color;
        break;
    case 'without':
        cost += 0;
        break;
    default:
        cout << "wrong input!";
    }

    switch(yes_no)
    {                                 // number of crowns
    case 'yes':
        cout << "Input number of crowns: ";
        cin >> number_crowns;
        cost += (number_crowns * 0.35);
        break;
    case 'no':
        cost =+ 0;
        break;
    default:
        cout << "wrong input!";
    }

    cout << endl;
    cout << "\nTotal frame cost: " << cost << "\n" << endl;
}

最新输出 enter image description here

2 个答案:

答案 0 :(得分:2)

您的变量frameTypeyes_nocolorSelection被声明为int,您正在对char中的switch进行比较。

您应该删除引号。

e.g:

cout<<"My frame type: "<<frameType<<endl;
switch(frameType)
{                                 // type of frame
case 1:
    cost += 0.15;
    break;
case 2:
    cost += 0.25;
    break;
default:
    cout << "wrong input!";
}

请注意,您可以在switch语句

之前显示变量

答案 1 :(得分:0)

Case语句检查输入值是否与某个常量匹配。

在您的情况下,您正在检查int的{​​{1}}值是1还是0,是否与'1'的常量值相同。

这个'1'(这是frameType类型)实际上会在与整数比较时自动转换为整数,但它会转换为基于ascii table的整数,所以它会变成整数char

所以当它全部编译时,好像你写了这样的东西:

49

这些隐藏的转换对每个人来说都是一个巨大的痛苦,因此大多数人会打开编译器警告,如果发生这些“隐式转换”,将停止编译。

如果您在打开所有警告的情况下进行编译并将其视为错误,那么大多数编译器都会为您捕捉到这种情况!在我工作的地方,这是默认的编译模式,因为我们都犯了这些错误......偶尔说。