Joel
Joel

Reputation: 9

C# change making program

Making a change making program for homework and it has to return the amount of change when an amount is entered (it is based off Australian currency) and I've got it working up to the fifty cent mark. When the change is calculated and the program has to return a value of a twenty cent, ten cent or a five cent change, the program freezes

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

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        double change = Convert.ToDouble(txtOffered.Text) - Convert.ToDouble(txtDue.Text);
        // MessageBox.Show(change.ToString());            
        double hund = 100;
        double fifty = 50;
        double twent = 20;
        double ten = 10;
        double five = 5;
        double two = 2;
        double one = 1;
        double fifcent = 0.50;
        double twentcent = 0.20;
        double tencent = 0.10;
        double fivecent = 0.05;


        while (change > 0) 
        {
            if (change >= hund) 
            {
                txtChange.Text += "1x $100 \r\n"; 
                change = change - hund;
            }


            else if (change >= fifty)
            {
                txtChange.Text += "1x $50 \r\n";
                change = change - fifty;
            }
            if (change >= twent) 
            {
                txtChange.Text += "1x $20 \r\n";
                change = change - twent;
            }
            else if (change >= ten)
            {
                txtChange.Text += "1x $10 \r\n";
                change = change - ten;
            }
            if (change >= five)
            {
                txtChange.Text += "1x $5 \r\n";
                change = change - five;
            }
            else if (change >= two)
            {
                txtChange.Text += "1x $2 \r\n";
                change = change - two;
            }
            if (change >= one)
            {
                txtChange.Text += "1x $1 \r\n";
                change = change - one;
            }
            else if (change >= fifcent)
            {
                txtChange.Text += "1x 50c \r\n";
                change = change - fifcent;
            }
            if (change >= twentcent)
            {
                txtChange.Text += "1x 20c \r\n";
                change = change - twentcent;
            }
            else if (change >= tencent)
            {
                txtChange.Text += "1x 10c \r\n";
                change = change - tencent;
            }
            if (change >= fivecent)
            {
                txtChange.Text += "1x 5c \r\n";
                change = change - fivecent;
            }

        }

    }
}

Upvotes: 0

Views: 389

Answers (2)

Pikoh
Pikoh

Reputation: 7713

For a newbie this maybe hard to spot. The problem with your code is you are using the wrong DataType. Instead of double,you should be using decimal:

decimal change = Convert.ToDouble(txtOffered.Text) - Convert.ToDouble(txtDue.Text);
// MessageBox.Show(change.ToString());            
decimal hund = 100;
decimal fifty = 50;
decimal twent = 20;
decimal ten = 10;
decimal five = 5;
decimal two = 2;
decimal one = 1;
decimal fifcent = 0.50m;
decimal twentcent = 0.20m;
decimal tencent = 0.10m;
decimal fivecent = 0.05m;

With this, your code wouldn't freeze.

For an explanation of this, the problem was that,using double the result of 0.25 - 0.20 is...0.049999999999999989. This is because double uses floating-point values, that can lead to rounding problems. Have a look here if you want to know more about floating point calculations.

Upvotes: 1

dlxeon
dlxeon

Reputation: 2010

The app will stuck if you entered amount < 0.05 or you receive amount < 0.05 after change result.

Reason is here: if your change variable value is > 0, but < 0.05 you'll stuck forever.

while (change > 0)
{
    ...
    if (change >= fivecent)
    {
        txtChange.Text += "1x 5c \r\n";
        change = change - fivecent;
    }
}

Upvotes: 1

Related Questions