-1

When I run the code, it doesn't calculate and it doesn't show the calculations. Wondering if my variables or something else is wrong or maybe in the wrong place.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Question_3_Retail_Price_Calculator
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            decimal costPrice = 0;
            decimal markUpRateA = 0.19m;
            decimal markUpRateB = 0.14m;
            decimal markUpRateC = 0.12m;
            decimal markUpRate = 0m;
            decimal markUpTotal = 0;
            decimal totalCost = 0;

            // Add the items to ListBox 1
            listBox1.Items.Add("Markup Rate =");
            listBox1.Items.Add("Markup Total =");
            listBox1.Items.Add("Total Cost =");

            try
            {
                // Read the cost price from the user
                costPrice = decimal.Parse(textBox1.Text);

            }
           catch (System.Exception excep )
             {
                 MessageBox.Show(excep.Message);
             }

                if (costPrice <= 50)
                {
                    // Calculate the value of  the product 
                    costPrice = (costPrice / 100) * markUpRateA;  
                }
                else if (costPrice >= 50 && costPrice < 100)
                {
                    //  Calculate the value of the product
                    costPrice = (costPrice / 100) * markUpRateB;
                }
                else if (costPrice < 100)
                {
                    // Calculate the value of the product
                    costPrice = (costPrice / 100) * markUpRateC;
                }
                else if (markUpRate == markUpTotal)
                {
                    // Calculate the total monetary amount
                    markUpTotal = costPrice * markUpRate;
                }
                else
                {
                    // Calculate the Total Cost  of the product
                    totalCost = costPrice + markUpTotal;
                }
                // Output the total Cost
                MessageBox.Show("Total Cost is: €"  + totalCost);
            }
    }
}

Need some help figuring this out! Thanks in advance!

Sobe
  • 1
  • 1
  • Have you tried setting breakpoints in your code to see where it is going wrong? – dub stylee Apr 24 '15 at 23:37
  • 1
    Tip for a mellow future: The first thing you need to do when you start to program with a new language/environment is: How does debugging work? Even if some claim that debugging is not necessary in that highly cool technology xyz. Especially then! – BitTickler Apr 24 '15 at 23:38
  • You never add anything to your list boxes. You only add the text's, not the actual values. You should start there. – Der Kommissar Apr 24 '15 at 23:42
  • 1
    Hint: You only get one trip through the if statements, they are not processed sequentially. Also, if something is >= 50 and <100, what does having a test for <100 add to your set of conditions? The best approach is to review your text and make smaller programs and build up, otherwise this is going to be a troublesome experience for you. – jonsca Apr 24 '15 at 23:44
  • 2
    I don't think you understand how an if statement works, as your logic is messed up. If the first condition is true, it executes the code and then drops out the bottom of the if, so none of the other (else) conditions are checked and none of the other code is executed. In your case, the second else if (costPrice < 100) is never hit, because the condition is covered by the previous two conditions. The final else if OR the final else will only be executed if costPrice >= 100. Can you see why? – cf_en Apr 24 '15 at 23:45

2 Answers2

2

You're setting your total cost in the last else condition which will be executed only if all the other if-else conditions are not executed. That is why this code isn't working as you expect.

Even if you enter a value >100, your result is never assigned to totalCost. Because execution enters this part of your code

         else if (markUpRate == markUpTotal)
        {
            // Calculate the total monetary amount
            markUpTotal = costPrice * markUpRate;
        }

and then jumps directly to the Message.Show(..) line.

0

There is a lot of crossover in you if else statements. If costPrice is 50, do you want to run rate A, rate B, or rate C (I think you have put your > sign the wrong way with markup rate C).

Also, why is it in a try loop? You are basically saying: "If there are no problems, assign costPrice to whatever the user entered, then skip the rest. And if there is a problem, do the rest but the costPrice will be the default value I assigned it to at the start (0)."

Basically, all the rest of the stuff should be in the try loop, not the catch loop.

Oh and read Xaero's answer as well, I have to wite this here though because i'm not allowed not comment yet :(.

Winky2222
  • 46
  • 7