-1

I am revisiting one of my old exercises and I seem to be having problems with my logic. It is a simple console application that is supposed to calculate and display change for a given total and cash tendered. For some reason, I get an infinite loop when I input values which decimal part other than 25.

Here is a screenshot of my console application enter image description here

Here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace James_Archbold_A2
{
    class Program
    {
        static void Main(string[] args)
        {
            double change,total,cash;
            int  fifties = 0, twenties = 0, tens = 0, fives = 0, ones = 0,
                quarters = 0, dimes = 0, nickels = 0, pennies = 0;

            Console.Write("Please enter the total: ");
            total = Convert.ToDouble(Console.ReadLine());

            Console.Write("Please enter the cash tendered: ");
            cash = Convert.ToDouble((Console.ReadLine()));

            change = CalculateChange(total, cash);
            Console.WriteLine(change);

            change *= 100;


            while (change > 0)
            {


                if (change >= 5000)
                {
                    change -= 5000;
                    fifties++;
                    Console.WriteLine(change);


                }

                else if (change >= 2000)
                {
                    change -=2000;
                    twenties++;
                    Console.WriteLine(change);

                }

                else if (change >= 1000)
                {
                    change -=1000;
                    tens++;
                    Console.WriteLine(change);

                }

                else if (change >= 500)
                {
                    change -= 500;
                    fives++;
                    Console.WriteLine(change);
                }

                else if (change >= 100)
                {
                    change -=100;
                    ones++;
                    Console.WriteLine(change);

                }

                else if (change >= 25)
                {
                    change -=25;
                    quarters++;
                    Console.WriteLine(change);

                }

                else if (change >= 10)
                { 
                    change -=10;
                    dimes++;
                    Console.WriteLine(change);
                }

                else if (change >= 5)
                {
                    change -=5;
                    nickels++;
                    Console.WriteLine(change);

                }

                else if (change >= 1)
                {
                    change -=1;
                    pennies++;
                    Console.WriteLine(change);
                }
            }

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Total: {0:c}\nCash Tendered: {1:c}" +
                            "\nChange Due: \nFifties: {2}\nTwenties: {3}\nTens: {4}" +
                              "\nFives: {5}\nOnes: {6}\nQuarters: {7}\nDimes: {8}" +
                              "\nNickels: {9}\nPennies: {10}",
                              total, cash, fifties, twenties, tens, fives, ones, quarters, dimes,nickels, pennies);
            Console.WriteLine(sb);
            Console.ReadLine();
        }

        static double CalculateChange(double total, double cash)
        {
            return cash - total;
        }
    }
}
Blorgbeard
  • 93,378
  • 43
  • 217
  • 263
Jameslat
  • 37
  • 1
  • 1
  • 7

5 Answers5

4

If you're writing anything to do with financial calculation, use the decimal data-type. Not double.

I think Jon Skeet's article "Binary floating point and .NET" will give you the greatest insight.

A few other pointers:

Is a double really unsuitable for money?

What Every Computer Scientist Should Know About Floating Point

Community
  • 1
  • 1
spender
  • 106,080
  • 28
  • 202
  • 324
1

Make change an int to truncate the .0000000000001 that prevents change from ever falling below 0 with your current algorithm:

int change = CalculateChange(total, cash) * 100;
Moho
  • 13,165
  • 1
  • 24
  • 28
0

Use change>=0 in while condition

Laxmikant Dange
  • 6,983
  • 4
  • 36
  • 60
0

You're using doubles for your arithmetic, you can't rely on exact precision for decimal input using doubles.

If you want to use floating points with decimal, you should use C# decimal type.

anopse
  • 153
  • 1
  • 2
  • 9
0

In the code snippet provided just round the value of the variable change to its nearest integer and you would get the required output.

From

`while (change > 0)`

to

`while(Math.Round(change)>0)`

Hope this helps