2011-03-08 66 views
0

我試圖嘗試一些看起來超級簡單的東西,但現在我想把我的顯示器外面扔進雪中。我不明白爲什麼我的switch語句在調用時沒有執行。在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已被宣佈爲在頂部有一個字符。如果有人會告訴我最有可能的,初學者的錯誤,我會非常感激。謝謝。

編輯

... 
    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

在你的第二個功能的通知(比較op_sym的實際值):

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

您正在執行的任務,以CB和不實際的比較。嘗試使用==操作符用於比較兩個值:

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

當你想改變op_sym的值,可以使用賦值運算符(=)。當你想比較String的值時,使用比較運算符(==)。

而且 - 退房的API與字符串的工作是VC++:http://msdn.microsoft.com/en-us/library/ms177218.aspx

+0

媽呀,這是令人尷尬的。謝謝。固定在上面。 – Jordan 2011-03-08 05:04:56

+0

+1 @ Fran71是幾乎所有人都至少犯下一次的初學者的錯誤;) – 2011-03-08 05:15:24

0

單=是賦值 ==是等於

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

謝謝,忘記了。固定頂部。 – Jordan 2011-03-08 05:09:14