8 Replies - 426 Views - Last Post: 04 November 2019 - 08:17 PM Rate Topic: -----

#1 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Pop machine round up when you insert dimes

Posted 02 November 2019 - 06:21 PM

I made some progress in the pop machine and I can't figure out why it won't round up when you insert dimes into the machine. This is what happens when I run the program.

1 Coke
2 Pepsi
3 Sprite
4 Dr Pepper
5 Water
6 Gadorade
Choose one of the drinks
2
You picked a Pepsi
enter money to make $1.55

.1
You deposited $0.1. You're still short $1.45
enter money to make $1.55

.1
You deposited $0.1. You're still short $1.3499999999999999
enter money to make $1.55

.1
You deposited $0.1. You're still short $1.25
enter money to make $1.55

.1
You deposited $0.1. You're still short $1.15
enter money to make $1.55

.1
You deposited $0.1. You're still short $1.0499999999999998
enter money to make $1.55

This is the program.
using System;

namespace Pop_machine
{
    class Program
    {
        static void Main(string[] args)
        {
            int pennies, nickels, dimes, ones, fives, tens, twenties,
                fifties, hundreds = 0;
            int quarters = 0;
            int coins = 0;
            double money = 0;
            double bottle = 1.55;
            double sum = 0;
            double total = 0;
            double popchange = bottle - money;
            int bottlex = 155;
            double change = bottlex - coins;
            string[] cola = {" Coke"," Pepsi"," Sprite"," Dr Pepper"," Water"," Gadorade"};
            int choice = 0;
            Pick(cola, choice);
            while(true)
            {
                Console.WriteLine("enter money to make $1.55\n");
                money = Convert.ToDouble(Console.ReadLine());
                if(money == bottle || money == popchange || money == bottlex || change == coins)
                {
                    Console.WriteLine("Enjoy your cool refreshing drink");
                    break;
                }
                else if(money > bottle)
                {
                    Console.WriteLine("Enjoy your pop. You put in ${}", money + ". Your change will be $"+ popchange);
                    Console.WriteLine("which will be:\n");
                    popchange = money - bottle;
                    coins = Convert.ToInt32(popchange * 100);

                    fifties = coins / 5000;
                    coins = coins % 5000;
                    twenties = coins / 2000;
                    coins = coins % 2000;
                    tens = coins / 1000;
                    coins = coins % 1000;
                    fives = coins / 500;
                    coins = coins % 500;
                    ones = coins / 100;
                    coins = coins % 100;
                    quarters = coins / 25;
                    coins = coins % 25;
                    dimes = coins / 10;
                    coins = coins % 10;
                    nickels = coins / 5;
                    coins = coins % 5;
                    pennies = coins % 1;

                    Console.WriteLine("$"+popchange);
                    Console.WriteLine("Fifties {0}", fifties);
                    Console.WriteLine("Twenties {0}", twenties);
                    Console.WriteLine("Tens {0}", tens);
                    Console.WriteLine("Fives {0}", fives);
                    Console.WriteLine("Ones {0}", ones);
                    Console.WriteLine("Quarters {0}", quarters);
                    Console.WriteLine("Dimes {0}", dimes);
                    Console.WriteLine("Nickels {0}", nickels);
                    Console.WriteLine("Pennies {0}", pennies);
                    break;

                }
                else
                {
                    
                    popchange = bottle - money;
                    popchange = bottle - money;
                    popchange = popchange - sum;
                    sum = sum + money;
                    coins = Convert.ToInt32(money * 100);
                    change = change - total;
                    Console.WriteLine("You deposited ${0}", money+". You're still short $"+ popchange);
                    total = total + coins;
                }
            }
        }
        static void Pick(string[] cola, int choice)
        {
            for(int x = 0; x < 6; x++)
            {
                Console.Write(x +1);
                Console.WriteLine(cola[x]);
            }
            Console.WriteLine("Choose one of the drinks");
            choice = Convert.ToInt32(Console.ReadLine());
            switch(choice)
            {
                case 1:
                    Console.WriteLine("You picked a" + cola[0]);
                    break;
                case 2:
                    Console.WriteLine("You picked a" + cola[1]);
                    break;
                case 3:
                    Console.WriteLine("You picked a" + cola[2]);
                    break;
                case 4:
                    Console.WriteLine("You picked a" + cola[3]);
                    break;
                case 5:
                    Console.WriteLine("You picked a" + cola[4]);
                    break;
                case 6:
                    Console.WriteLine("You picked a" + cola[5]);
                    break;
                case 7:
                    Console.WriteLine("You picked a" + cola[6]);
                    break;
                default:
                    Console.WriteLine("");
                    break;

                }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Pop machine round up when you insert dimes

#2 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Pop machine round up when you insert dimes

Posted 02 November 2019 - 07:57 PM

Disregard, I figured it out. This is what I used

Console.WriteLine("${0:0.00}", Math.Round(popchange, 2, MidpointRounding.AwayFromZero));

This is the working program

using System;

namespace Pop_machine
{
    class Program
    {
        static void Main(string[] args)
        {
            int pennies, nickels, dimes, ones, fives, tens, twenties,
                fifties, hundreds = 0;
            int quarters = 0;
            int coins = 0;
            double money = 0;
            double bottle = 1.55;
            double sum = 0;
            double total = 0;
            double popchange = bottle - money;
            int bottlex = 155;
            double change = bottlex - coins;
            string[] cola = {" Coke"," Pepsi"," Sprite"," Dr Pepper"," Water"," Gadorade"};
            int choice = 0;
            Pick(cola, choice);
            while(true)
            {
                Console.WriteLine("enter money to make $1.55\n");
                money = Convert.ToDouble(Console.ReadLine());
                if(money == bottle || money == popchange || money == bottlex || change == coins)
                {
                    Console.WriteLine("Enjoy your cool refreshing drink");
                    break;
                }
                else if(money > bottle)
                {
                    Console.WriteLine("Enjoy your pop. You put in ${}", money + ". Your change will be $"+ popchange);
                    Console.WriteLine("which will be:\n");
                    popchange = money - bottle;
                    coins = Convert.ToInt32(popchange * 100);

                    fifties = coins / 5000;
                    coins = coins % 5000;
                    twenties = coins / 2000;
                    coins = coins % 2000;
                    tens = coins / 1000;
                    coins = coins % 1000;
                    fives = coins / 500;
                    coins = coins % 500;
                    ones = coins / 100;
                    coins = coins % 100;
                    quarters = coins / 25;
                    coins = coins % 25;
                    dimes = coins / 10;
                    coins = coins % 10;
                    nickels = coins / 5;
                    coins = coins % 5;
                    pennies = coins % 1;

                    Console.WriteLine("$"+popchange);
                    Console.WriteLine("Fifties {0}", fifties);
                    Console.WriteLine("Twenties {0}", twenties);
                    Console.WriteLine("Tens {0}", tens);
                    Console.WriteLine("Fives {0}", fives);
                    Console.WriteLine("Ones {0}", ones);
                    Console.WriteLine("Quarters {0}", quarters);
                    Console.WriteLine("Dimes {0}", dimes);
                    Console.WriteLine("Nickels {0}", nickels);
                    Console.WriteLine("Pennies {0}", pennies);
                    break;

                }
                else
                {
                    
                    popchange = bottle - money;
                    popchange = bottle - money;
                    popchange = popchange - sum;
                    sum = sum + money;
                    coins = Convert.ToInt32(money * 100);
                    change = change - total;
                    Console.Write("You deposited ${0}", money);
                    Console.Write(" you are still short ");
                    Console.WriteLine("${0:0.00}", Math.Round(popchange, 2, MidpointRounding.AwayFromZero));
                    //Console.WriteLine("You deposited ${0}", money+". You're still short $"+ popchange);
                    total = total + coins;
                }
            }
        }
        static void Pick(string[] cola, int choice)
        {
            for(int x = 0; x < 6; x++)
            {
                Console.Write(x +1);
                Console.WriteLine(cola[x]);
            }
            Console.WriteLine("Choose one of the drinks");
            choice = Convert.ToInt32(Console.ReadLine());
            switch(choice)
            {
                case 1:
                    Console.WriteLine("You picked a" + cola[0]);
                    break;
                case 2:
                    Console.WriteLine("You picked a" + cola[1]);
                    break;
                case 3:
                    Console.WriteLine("You picked a" + cola[2]);
                    break;
                case 4:
                    Console.WriteLine("You picked a" + cola[3]);
                    break;
                case 5:
                    Console.WriteLine("You picked a" + cola[4]);
                    break;
                case 6:
                    Console.WriteLine("You picked a" + cola[5]);
                    break;
                case 7:
                    Console.WriteLine("You picked a" + cola[6]);
                    break;
                default:
                    Console.WriteLine("");
                    break;
                }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7141
  • View blog
  • Posts: 24,251
  • Joined: 05-May 12

Re: Pop machine round up when you insert dimes

Posted 03 November 2019 - 09:13 AM

Part of the problem here is that you are using double for money. In C#, they specifically may the decimal type to deal with money which won't have the rounding issues.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7141
  • View blog
  • Posts: 24,251
  • Joined: 05-May 12

Re: Pop machine round up when you insert dimes

Posted 03 November 2019 - 09:28 AM

Also beware of comparing equality with floating point numbers like double and float.
if (money == bottle || money == popchange || money == bottlex || change == coins)



See: Difference between decimal, float and double in .NET?
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7141
  • View blog
  • Posts: 24,251
  • Joined: 05-May 12

Re: Pop machine round up when you insert dimes

Posted 03 November 2019 - 09:50 AM

And on further review, you don't even need to deal with any floating point numbers. Just always work internally with pennies. When someone enters monetary values, convert to pennies, and when outputting values divide by 100 to get dollars and cents.
Was This Post Helpful? 0
  • +
  • -

#6 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Pop machine round up when you insert dimes

Posted 03 November 2019 - 03:29 PM

I want to make sure I understand your suggestions. You suggest I change the double to decimal because its more exact and line 81 change from this:
Console.WriteLine("${0:0.00}", Math.Round(popchange, 2, MidpointRounding.AwayFromZero));

to to this:
int coins = Decimal.ToInt32(popchange * 100);
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7141
  • View blog
  • Posts: 24,251
  • Joined: 05-May 12

Re: Pop machine round up when you insert dimes

Posted 03 November 2019 - 06:32 PM

View Postalbert003, on 03 November 2019 - 06:29 PM, said:

You suggest I change the double to decimal because its more exact

Yes.

View Postalbert003, on 03 November 2019 - 06:29 PM, said:

You suggest I change ... line 81 change from this:
Console.WriteLine("${0:0.00}", Math.Round(popchange, 2, MidpointRounding.AwayFromZero));

to to this:
int coins = Decimal.ToInt32(popchange * 100);

No.

You won't even need that call to Math.Round() if you use decimal because it'll be an exact value. So the line
Console.WriteLine("${0:0.00}", Math.Round(popchange, 2, MidpointRounding.AwayFromZero));


can simply be:
Console.WriteLine("${0:0.00}", popchange);



I would suggest changing line 26 from:
money = Convert.ToDouble(Console.ReadLine());


to
money = Convert.ToDecimal(Console.ReadLine());



Furthermore, regardless if you use double or decimal, use the following so that the system will do the culture appropriate formatting for money:
Console.WriteLine("{0:c}", popchange);



Off topic: Why do you have this big switch statement:
            switch(choice)
            {
                case 1:
                    Console.WriteLine("You picked a" + cola[0]);
                    break;
                case 2:
                    Console.WriteLine("You picked a" + cola[1]);
                    break;
                case 3:
                    Console.WriteLine("You picked a" + cola[2]);
                    break;
                case 4:
                    Console.WriteLine("You picked a" + cola[3]);
                    break;
                case 5:
                    Console.WriteLine("You picked a" + cola[4]);
                    break;
                case 6:
                    Console.WriteLine("You picked a" + cola[5]);
                    break;
                case 7:
                    Console.WriteLine("You picked a" + cola[6]);
                    break;
                default:
                    Console.WriteLine("");
                    break;
            }



Notice that your index into cola is always one less than your value in your case constant. (e.g when case 1, you are displaying cola[0], when case 2, you are displaying cola[1], etc.) Why not just check for a valid entered value, and then compute the index?
Was This Post Helpful? 0
  • +
  • -

#8 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Pop machine round up when you insert dimes

Posted 03 November 2019 - 09:46 PM

I did what you suggested and it works perfectly.

I am using a switch statement for now. Because originally, I used an enum with a bunch of if statements (Which I thought was not a good idea to have that many if statements)and my plan was to have the user pick their drink, say for example a Pepsi (which is 1). Later when the user is paying for the drink it would say something like "You put in .25 but you are still short $1.30. This was my latest idea to try to do that in my program. Your suggestion is actually a good idea. I have an idea how to do it.

This post has been edited by albert003: 03 November 2019 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#9 albert003   User is offline

  • D.I.C Addict

Reputation: 37
  • View blog
  • Posts: 791
  • Joined: 15-December 14

Re: Pop machine round up when you insert dimes

Posted 04 November 2019 - 08:17 PM

I figured a way to verify the input using TryParse so it would only take integers
(I've taken that part out of the program for the moment). But I haven't figured out a way to make it work with the second part of your suggestion to compute the index. I have a new question for you, when I enter the pop I want to buy it chooses the right one, but later when I put change to pay for my pop it gives me the error message index is out of bounds of the array. But when I remove the -1 from it, the program works but shows the wrong drink. Could you give me a hint or suggestion what I could do to fix the problem? (Its on line 82)

This is the program so far.

using System;

namespace Pop_machine
{
    class Program
    {
        static void Main(string[] args)
        {
            int pennies, nickels, dimes, ones, fives, tens, twenties,
                fifties, hundreds = 0;
            int quarters = 0;
            int coins = 0;
            decimal money = 0m;
            decimal bottle = 1.55m;
            decimal sum = 0m;
            decimal total = 0m;
            decimal popchange = bottle - money;
            int bottlex = 155;
            decimal change = bottlex - coins;
            string[] cola = {" Coke"," Pepsi"," Sprite"," Dr Pepper"," Water"," Gadorade"};
            int choice = 0;
            Pick(cola, choice);

            while (true)
            {
                Console.WriteLine("enter money to make $1.55\n");
                money = Convert.ToDecimal(Console.ReadLine());
                if (money == bottle || money == popchange || money == bottlex || change == coins)
                {
                    Console.WriteLine("Enjoy your cool refreshing drink");
                    break;
                }
                else if (money > bottle)
                {
                    Console.WriteLine("Enjoy your pop. You put in $ ", money + ". Your change will be $" + popchange);
                    Console.WriteLine("which will be:\n");
                    popchange = money - bottle;
                    coins = Convert.ToInt32(popchange * 100);

                    fifties = coins / 5000;
                    coins = coins % 5000;
                    twenties = coins / 2000;
                    coins = coins % 2000;
                    tens = coins / 1000;
                    coins = coins % 1000;
                    fives = coins / 500;
                    coins = coins % 500;
                    ones = coins / 100;
                    coins = coins % 100;
                    quarters = coins / 25;
                    coins = coins % 25;
                    dimes = coins / 10;
                    coins = coins % 10;
                    nickels = coins / 5;
                    coins = coins % 5;
                    pennies = coins % 1;

                    Console.WriteLine("$" + popchange);
                    Console.WriteLine("Fifties {0}", fifties);
                    Console.WriteLine("Twenties {0}", twenties);
                    Console.WriteLine("Tens {0}", tens);
                    Console.WriteLine("Fives {0}", fives);
                    Console.WriteLine("Ones {0}", ones);
                    Console.WriteLine("Quarters {0}", quarters);
                    Console.WriteLine("Dimes {0}", dimes);
                    Console.WriteLine("Nickels {0}", nickels);
                    Console.WriteLine("Pennies {0}", pennies);
                    break;

                }
                else
                {
                    popchange = bottle - money;
                    popchange = bottle - money;
                    popchange = popchange - sum;
                    sum = sum + money;
                    coins = Convert.ToInt32(money * 100);
                    change = change - total;
                    Console.Write("You deposited ${0}", money);
                    Console.Write(" you are still short ");
                    Console.Write("${0:0.00}", popchange);
                    Console.WriteLine(" for your"+ cola[choice-1]+"\n");
                    total = total + coins;
                }
            }
        }
        static void Pick(string[] cola, int choice)
        {
            for(int x = 0; x < 6; x++)
            {
                Console.Write(x +1);
                Console.WriteLine(cola[x]);
            }
            Console.WriteLine("Choose one of the drinks");
            choice = Convert.ToInt32(Console.ReadLine());
            Console.Write("You picked ");
            Console.WriteLine(cola[choice-1]);
        }
    }
}


This post has been edited by albert003: 04 November 2019 - 09:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1