views:

43

answers:

4

For the most part this does work the problem is the message box pops up for Andrea and Brittany but it works correctly for Eric. If I try to put the else statement after each if statement it still pops up on Brittany, and Andrea, but then also pops up on Eric then. Can someone tell me what i am doing wrong.

    private void button1_Click(object sender, EventArgs e)
    {
        String Andrea;
        String Brittany;
        String Eric;
        if (textBox1.Text == "Andrea")
        {     
            Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
        }

        if (textBox1.Text == "Brittany")
        {
            Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
        }


        if (textBox1.Text == "Eric")
        {
            Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
        }
        else
        {
            MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
        }   

        {




        } 

    }
+2  A: 

try using a else if like this.

if (textBox1.Text == "Andrea")
{     
    Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else if (textBox1.Text == "Brittany")
{
    Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else if (textBox1.Text == "Eric")
{
    Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else
{
    MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
} 
RRUZ
+2  A: 

try this... by keeping a list of the names, you can easily expand the names covered and not have to write any more code. Simply add the new name to the names list

List<string> names = new List<string>() // list of names to check for
{                                       // if a name is not in this list
   "Andrea","Brittany","Eric"           // the error message will show
};                                      // otherwise, the calculation will be performed

if ( names.Contains(TextBox1.Text) )
{
    Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
}
else
{
    MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
}
Muad'Dib
this works the best. I have one other question what would a do while loop look like to acomplish the same task.
randywhite30
do...while what? it would depend on what you are trying to accomplish.
Muad'Dib
A: 

Well, I don't know about your earlier attempts, but as it stands currently each if statement is being handled separately. so if textBox1.Text != "Eric", the else attached to Eric is going to fire, and in this case show the MessageBox, regardless of whether the other two if's had a match.

Perhaps you had an error in your else if attempt? Try how some of the above people posted and see if that works.

Lunin
+1  A: 
switch(textBox1.Text)
{
    case "Andrea" : Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
    case "Brittany" : Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
    case "Eric" : Commission.Text = (Convert.ToDouble(textBox2.Text) / 10).ToString();
    default: MessageBox.Show("The spelling of the name is incorrect", "Bad Spelling");
}
sgrassie
@sgrassie +1, but really we're repeating ourselves. Hopefully we can skinny it down even further.
p.campbell