skip to Main Content

My decimal variables weren’t adding up using else if.
I have by now resolved all issues except for one.

code with explanation:

namespace Huiswerk5._2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

    private void Form1_Load(object sender, EventArgs e)
    {

        radioButton2.Checked = true;//only this doesnt work


    }


    private void button1_Click(object sender, EventArgs e)
    {
        
    
        decimal kleinekoffie = 0.50m;//here i make variables
        decimal middelgrotekoffie = 0.75m;// of the decimal type
        decimal grotekoffie = 1.00m;// i figured out to add the m at 
        decimal melk = 0.05m;//the end to be able to type numbers
        decimal suiker = 0.05m;//with two decimals
        decimal extrasterk = 0.25m;
        decimal prijs = 0;






        if (radioButton3.Checked == true)/// here are the if's

        {
            prijs = +kleinekoffie;
            label3.Text = prijs.ToString("0.00");





            if (checkBox3.Checked == true)
            {
                prijs = +kleinekoffie + melk;
                label3.Text = prijs.ToString("0.00") + " Euro ";


            }



            if (checkBox2.Checked == true)

            {
                prijs = +kleinekoffie + suiker;
                label3.Text = prijs.ToString("0.00") + " Euro";


            }




            if (checkBox1.Checked == true)

            {
                prijs = kleinekoffie + extrasterk;
                label3.Text = prijs.ToString("0.00") + " Euro";


            }


        }


        if (radioButton3.Checked == true)




            if (checkBox3.Checked == true)
                if (checkBox2.Checked == true)
                    if (checkBox1.Checked == false)
                    {
                        prijs = +kleinekoffie + melk + suiker;
                        label3.Text = prijs.ToString("0.00") + " Euro ";


                    }



        if (radioButton3.Checked == true)
            if (checkBox3.Checked == true)
                if (checkBox1.Checked == true)
                    if (checkBox2.Checked == false)
                    {
                        prijs = +kleinekoffie + melk + extrasterk;
                        label3.Text = prijs.ToString("0.00") + " Euro ";


                    }

        if (radioButton3.Checked == true)
            if (checkBox2.Checked == true)
                if (checkBox1.Checked == true)
                    if (checkBox3.Checked == false)

                    {
                        prijs = +kleinekoffie + suiker + extrasterk;
                        label3.Text = prijs.ToString("0.00") + " Euro ";



                    }


        if (radioButton3.Checked == true)
            if (checkBox2.Checked == true)
                if (checkBox1.Checked == true)
                    if (checkBox3.Checked == true)

                    {
                        prijs = +kleinekoffie + melk + suiker + extrasterk;
                        label3.Text = prijs.ToString("0.00") + " Euro ";



                    }






        if (radioButton2.Checked == true)

        {

            prijs = middelgrotekoffie;
            label3.Text = prijs.ToString("0.00") + " Euro";








            if (checkBox3.Checked == true)
            {

                prijs = middelgrotekoffie + melk;
                label3.Text = prijs.ToString("0.00") + " Euro";

            }




            if (checkBox2.Checked == true)

            {

                prijs = middelgrotekoffie + suiker;
                label3.Text = prijs.ToString("0.00") + " Euro";

            }




            if (checkBox1.Checked == true)

            {

                prijs = middelgrotekoffie + extrasterk;
                label3.Text = prijs.ToString("0.00") + " Euro";

            }

            if (radioButton2.Checked == true)
                if (checkBox3.Checked == true)
                    if (checkBox2.Checked == true)
                        if (checkBox1.Checked == false)
                        {

                            prijs = middelgrotekoffie + melk + suiker;
                            label3.Text = prijs.ToString("0.00") + "Euro";

                        }

            if (radioButton2.Checked == true)
                if (checkBox3.Checked == true)
                    if (checkBox2.Checked == true)
                        if (checkBox1.Checked == true)
                        {

                            prijs = middelgrotekoffie + melk + suiker + extrasterk;
                            label3.Text = prijs.ToString("0.00") + "Euro";

                        }

        }

        if (radioButton1.Checked == true)

        {
            prijs = grotekoffie;
            label3.Text = prijs.ToString("0.00") + "Euro";







            if (checkBox3.Checked == true)

            {

                prijs = grotekoffie + melk;
                label3.Text = prijs.ToString("0.00") + "Euro";
            }




            if (checkBox2.Checked == true)

            {

                prijs = grotekoffie + suiker;
                label3.Text = prijs.ToString("0.00") + "Euro";

            }








            if (checkBox1.Checked == true)

            {

                prijs = grotekoffie + extrasterk;
                label3.Text = prijs.ToString("0.00") + "Euro";


            }
        }

        if (radioButton1.Checked == true)
        {
            if (checkBox3.Checked == true)
                if (checkBox2.Checked == true)
                    if (checkBox1.Checked == false)

                    {
                        prijs = grotekoffie + melk + suiker;
                        label3.Text = prijs.ToString("0.00") + "Euro";


                    }
        }


        if (radioButton1.Checked == true)
            if (checkBox3.Checked == false)
                if (checkBox2.Checked == true)
                    if (checkBox1.Checked == true)

                    {
                        prijs = grotekoffie + suiker + extrasterk;
                        label3.Text = prijs.ToString("0.00") + "Euro";


                    }


        if (radioButton1.Checked == true)
            if (checkBox3.Checked == true)
                if (checkBox2.Checked == false)
                    if (checkBox1.Checked == true)

                    {
                        prijs = grotekoffie + melk + extrasterk;
                        label3.Text = prijs.ToString("0.00") + "Euro";


                    }



        if (radioButton1.Checked == true)
        {
            if (checkBox3.Checked == true)

                if (checkBox2.Checked == true)
                    if (checkBox1.Checked == true)

                    {
                        prijs = +grotekoffie + melk + suiker + extrasterk;
                        label3.Text = prijs.ToString("0.00") + "Euro";


                    }

        }








    }

}

}

i didnt understand that this sequence of "if’s" doesnt create "double reactions" so else if or return arent neccesary.
just make sure all possible combinations of radiobutton and checkbox are handles, including et to false.

the only thing that doesnt work, is making radiobutton2 autocheck itself at start up.

the code is at the very beginning, i got the code and where to write off the internet.. visual studio recognizes it as such, the only thing i can think of is that it needs to be at a different line.

any suggestions on this last mystery of this assignment?

greetings,

stefan.

2

Answers


  1. The issue you seem to be encountering is that your if and else if statements are checking for the same condition. In this case, only the first statement will run as the second one is never checked since your first if statement has been successful. They would only both run when they are both if statements instead of an else if.

    You will need to use a different condition in your else if statement for it to run as shown here with an example.

    if (checkBox3.Checked == true)
    
    {
        label3.Text = kleinekoffie.ToString();
    }
    //The previous statement was the same as the above so it would never be reached, now that 
    //the condition is different, it will execute when checkBox3.Checked is false
    else if (checkBox3.Checked != true)
    {
        if   (radioButton3.Checked == true)
        {
            label3.Text = kleinekoffie.ToString() + melk.ToString() + " cent ";
        }
    }
    

    If you want the following line to run when checkBox3.Checked == True then I would recommend moving it inside the first if statement.

    label3.Text = kleinekoffie.ToString() + melk.ToString() + " cent ";
    

    Hope this helps and good luck with your code.

    Login or Signup to reply.
  2. First, make sure your indentation is correct. It will help you maintain your code more easily.

    You code but indented right:

    if (checkBox3.Checked == true)
    {
        label3.Text = kleinekoffie.ToString();///this one works
        return;
    } else
    if (checkBox3.Checked == true)
    {
        if (radioButton3.Checked == true)
        {
    
            label3.Text = kleinekoffie + melk.ToString() + " cent ";/// this one doesnt!
        }
    }
    

    Now you probably see for yourself what is going on here. Please study it before reading further.


    • You check is checkBox3.Checked is true

    • If not true, you check if it is true

    • and if it is, which will never ever be, you check

    • if radioButton3.Checked is true

    Depending on what you need, the next could be a solution:

    if (checkBox3.Checked == true)
    {
    
        label3.Text = kleinekoffie.ToString();///this one works
    
        if (radioButton3.Checked == true)
        {
    
            label3.Text = kleinekoffie + melk.ToString() + " cent ";
    
        }
    
        return; // this can be removed, isn't the problem.
    
    }
    

    Although this code probably would work, it is wrong practice. The Text property is set twice, this is not neccessary and even unwanted.

    You have some values you should add together and then convert it to a string (if neccessary) and assign it to this label3.Text.


    (made a little edit here, you use decimals not floats, my mistake)

    // Check if your `Control` is checked
    if( checkBox3.Checked == true )
    {
    
        // if so, ...
    
        // declare a variable for a total value and initialise it to zero.
        decimal price = 0;
    
        // As the coffee already is checked to be true, add its price
        price += kleinekoffie;
    
        // Did milk got ordered too...
        if( radioButton3.Checked == true )
        {
            // so add it too
            price += melk;
    
        }
    
        // Convert the price to string and assign that to the label's text property.
        label3.Text = price.ToString();
    
    }
    

    This is a good starting point to learn coding. There are way better methods but that’s for a later concern.


    You can fancy up label3.Text = price.ToString(); with:

    label3.Text = price.ToString("0.00");

    This will output the price with only two decimals.


    (edit: removed the parts of compilation error, it’s probably not there. decimal + string should work, my mistake)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search