2016-05-23 135 views
-4

我有我的switch语句的问题// number of crowns// frame color如何解决这个问题:C++ switch语句

这是我的输出enter image description here

我怎样才能让我的cout输出frame_colornumber_crowns(上// number of crowns// frame color)?

BTW即时通讯使用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

你的'case'标签是错误的 - 你正在比较'int'值和'char'文字 - 去除单引号,即case'1':' - >'case 1:'。另外,为了将来的参考,您可能想了解如何使用调试器来遍历代码。使用一个好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏离的位置。如果你打算做任何编程,这是一个重要的工具。进一步阅读:[如何调试小程序](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –

+3

请停止发布文字图片,将文字改为文字。 –

+0

oooh非常感谢! – Vadamadafaka

回答

2

你的变量frameTypeyes_nocolorSelection被声明为int和你正在做针对char比较在switch

您应该删除引号。

例如为:

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声明

+0

@ElecTreeFrying有关您的变量显示的更新。您应该在呼叫交换机前简单地执行此操作(请参阅上面的代码)。 –

0

Case语句检查,看看输入值是否某个常数相匹配之前显示您的变量。

在您的情况下,您正在检查frameTypeint值是1还是0,它与'1'的常数值相同。

此“1”(至极为char型)当与整数相比实际上将自动转换为一个整数,但它会转换为基于所述ascii table所以它将成为整数49的整数。

所以,当这一切编译它,如果你写了这样的事情:

switch (frameType) { 
case 49: 
    // Clearly frametype won't be 49 so this won't happen 
case 50: 
    // Same problem 

这些隐藏的转换可以给大家一个很大的痛苦,所以大多数的人会打开编译器警告,将停止编译如果这些“隐式转换”发生。

如果您编译时所有警告都打开并将它们视为错误,那么大多数编译器会为您抓住这类事情!在我工作的地方,这是编译的默认模式,因为我们都会犯这些错误......偶尔可以说。