案例不会在Switch语句中执行......?

时间:2011-03-08 04:37:57

标签: switch-statement visual-c++-2010

我正在尝试一些看起来非常简单的东西,但是现在我想把我的显示器扔到外面。我无法理解为什么我的switch语句在被调用时没有执行。

这是:

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) // Arithmetic Button Click
         {
            String^ riS = dc1->Text;
            String^ ciS = dc2->Text;        
            colint = int::Parse(dc2->Text);
            std::ostringstream ss;
            String^ answer;
            op1 = Double::Parse(foValue->Text);
            op2 = Double::Parse(soValue->Text);

            // Enter switch 
            switch(op_sym)
    {
        case '+':

            sum = op1 + op2;
            DGV->CurrentCell = DGV->Rows[RI]->Cells[CI];
            ss << sum;
            answer = Convert::ToString(answer);
            MessageBox::Show(answer);
            DGV->CurrentCell->Value = answer;
            sumLabel->Text = "TEST";
            break;

        case '-':
            sum = op1 - op2;
            break;
        case '*':
            sum = op1 * op2;
            break;
        case '/':
            if (op2 == 0)
            {
                MessageBox::Show("Sorry, you cannot divide by zero \n Please, reselect yoru second cell operand");
                secondOpText->Text = "";
            }
            else
            {
            sum = op1/op2;
            }
            break;
        default:
            MessageBox::Show("I'm sorry.  Please select one of these four arithmetic symbols from the drop down list: \n +, -, *, /");
            break;
    }


         }

我从上面得到了op_sym:

private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) 
         {
             Object^ selectedItem = comboBox1->SelectedItem;
             String^ cb = selectedItem->ToString();
             if( cb = "+")
             {
                op_sym = '+';
             }
             if(cb = "-")
                 op_sym = '-';
             if(cb = "/")
                 op_sym = '/';
             if(cb = "*")
                 op_sym = '*';

         }

op_sym已经在顶部声明为char。如果有人告诉我最有可能的,初学者的错误,我会非常感激。感谢。

修改

...
    case '+':
                {
                sum = op1 + op2;
                DGV->CurrentCell = DGV->Rows[RI]->Cells[CI];
                ss << sum;
                answer = Convert::ToString(sum);
                MessageBox::Show( answer);
                DGV->CurrentCell->Value = answer;
                sumLabel->Text = answer;
                break;
                }
...


private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) 
         {
             Object^ selectedItem = comboBox1->SelectedItem;
             String^ cb = selectedItem->ToString();
             if( cb == "+")
             {
                op_sym = '+';
             }
             if(cb == "-")
                 op_sym = '-';
             if(cb == "/")
                 op_sym = '/';
             if(cb == "*")
                 op_sym = '*';
}

2 个答案:

答案 0 :(得分:2)

注意你的第二个函数(比较op_sym的实际值):

 if( cb = "+")
         {
            op_sym = '+';
         }
         if(cb = "-")
             op_sym = '-';
         if(cb = "/")
             op_sym = '/';
         if(cb = "*")
             op_sym = '*';

您正在执行cb的分配而不是实际比较。尝试使用==运算符来比较两个值:

if ( cb == "+" )
...

如果要更改op_sym的值,请使用赋值运算符(=)。如果要比较String&#39; s的值,请使用比较运算符(==)。

另外 - 检查用于使用String的API是VC ++:http://msdn.microsoft.com/en-us/library/ms177218.aspx

答案 1 :(得分:0)

single =是作业 ==等于

if( cb == "+")
 {
    op_sym = '+';
 }
 if(cb == "-")
     op_sym = '-';
 if(cb == "/")
     op_sym = '/';
 if(cb == "*")
     op_sym = '*';