开发者

Cases won't execute in Switch statement...?

开发者 https://www.devze.com 2023-02-15 05:43 出处:网络
I\'m trying to attempt something that seems super simple, but right now I want to throw my monitor outside out into the snow.I can\'t see why my switch statement is not executing when called.

I'm trying to attempt something that seems super simple, but right now I want to throw my monitor outside out into the snow. I can't see why my switch statement is not executing when called.

Here it is:

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;
    }


         }

I'm getting the op_sym from right above:

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 has already been declared as a char at the top. If someone would inform me of my most likely, beginner's mistake, I would be much appreciate. Thanks.

EDIT

...
    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 = '/';
            开发者_Go百科 if(cb == "*")
                 op_sym = '*';
}


Notice in your second function (comparing the actual value of the op_sym):

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

You're performing assignments to cb and not actually comparisons. Try using the == operator for comparing two values:

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

When you want to change the value of the op_sym, you use the assignment operator (=). When you want to compare values of String's, use the comparison operator (==).

Also - Check out the API for working with String is VC++: http://msdn.microsoft.com/en-us/library/ms177218.aspx


single = is assignment == is equals

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

精彩评论

暂无评论...
验证码 换一张
取 消