String Issue (May be in the wrong section).

Answered String Issue (May be in the wrong section).

  • Saturday, May 12, 2012 5:30 PM
     
     

    Hi, I'm very new to coding (trying to teach myself through tutorials and such). I started about a week ago. And, I'm having a problem with my Windows Form application. So I am making a calculator, and I'm having an issue trying to create a divide by zero error. I can't figure out how to stop one instance from converting to string d. If I comment out my Convert::ToString(D) My divide by zero message displays with no issue, however my calculator doesn't add, subtract, multiply, and divide. But, If I leave it instead of displaying a divide by zero error when I divide by zero it displays the most recent solution even if I hit clear in between. (All my clear button does is erase the text from every text box). And, if I try to divide by zero before doing anything else so that there is no most decent solution it just displays zero because d (my solution) is set equal to zero by default. I was thinking of using an if statement to exclude the Convert::ToString(d) somehow, but like I said I am new to coding and don't know how.

    Note: This is not a homework assignment or anything this is a personal project.

    Here is part of my code: 

    private: System::Void Equal_Click(System::Object^  sender, System::EventArgs^  e) {
     x = System::Convert::ToSingle(this->num1->Text);
     y = System::Convert::ToSingle(this->num2->Text);
     switch(c)
     {
     case '+': d = x + y; break;
     case '-': d = x - y; break;
     case '*': d = x * y; break;
     case '/':  
     if ( y == 0 ) {
     this->Solution->Text = "Divide by zero error";
     }
     else {
     d = x / y;
     }
     break;
     }
     this->Solution->Text = System::Convert::ToString(d);
             }

All Replies

  • Saturday, May 12, 2012 6:08 PM
     
     Answered Has Code

    Here you go

    private: System::Void Equal_Click(System::Object^  sender, System::EventArgs^  e) {
     x = System::Convert::ToSingle(this->num1->Text);
     y = System::Convert::ToSingle(this->num2->Text);
     switch(c)
     {
     case '+': d = x + y; break;
     case '-': d = x - y; break;
     case '*': d = x * y; break;
     case '/':  
     if ( y == 0 ) {
     this->Solution->Text = "Divide by zero error";
     }
     else {
     d = x / y;
     }
     break;
     }
    if (( y != 0 ) || (c != '/') )
     this->Solution->Text = System::Convert::ToString(d);
             }


    Regards,
    Ahmed Ibrahim
    SQL Server Setup Team
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
    This can be beneficial to other community members reading the thread.


  • Saturday, May 12, 2012 6:37 PM
     
     
    Thank you so much!
  • Wednesday, May 16, 2012 11:17 PM
     
     
    I'm sorry for taking so long to say this, but this isn't exactly working. Everything is working except dividing by any number except zero doesn't actually make the program divide. I think that it's not correctly converting to the d string. But, I don't see what's wrong...
  • Wednesday, May 16, 2012 11:23 PM
     
     
    Corrected, please try the modified code.

    Regards,
    Ahmed Ibrahim
    SQL Server Setup Team
    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you.
    This can be beneficial to other community members reading the thread.

  • Thursday, May 17, 2012 12:49 AM
     
     
    Thanks! I tested it right away this time and everything seems to be working properly. Hopefully I can move on to a new project now.