Jump to content


Photo

"Vending Machine"


  • Please log in to reply
2 replies to this topic

#1 Dr Ben Warne

Dr Ben Warne
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 02 October 2006 - 12:19 PM

Im new to PHP, and i find it intriguing, so i am teaching myself basic php language.

However, I've hit a stumbling block with this particular bit of code, i want it to display the change in its highest format, no frills just straight code

Thanks!

<head>
<title>Vending Machine</title>
</head>
<body>
<h2>Vending Machine</h2>
<?php
  $price = 27;
  $payment = 100;
  $change = $payment - $price;
while ($change >0) {
  if ($change >= 50)
    $result = "50";
      echo "$result";
        $change = $change-$result;
 
    if ($change >= 20)
      $result = "20";
        echo "$result";
        $change = $change-$result;

    if ($change >= 10)
      $result = "10";
        echo "$result";
        $change = $change - 10;
 
    if ($change >= 5)
      $result = "5";
        echo "$result";
          $change = $change - 5;
 
    if ($change >= 2)
      $result = "2";
        echo "$result";
          $change = $change - 2;
 
    if ($change >= 1)
      $result = "1";
        echo "$result";
          $change = $change - 1;
 

}


echo "$change";
?>
</body>
</html>

#2 thedarkwinter

thedarkwinter
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts
  • LocationLondon

Posted 02 October 2006 - 01:44 PM

Hi

You are missing the brackets for the if statements...

while ($change >0) {
  if ($change >= 50) {
    $result = "50";
      echo "$result";
        $change = $change-$result; }

    if ($change >= 20) {
      $result = "20";
        echo "$result";
        $change = $change-$result; }


etc etc...


Remember - if you don't figure it out yourself, you'll probably forget it tomorrow :)

#3 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 02 October 2006 - 01:52 PM

There are all kinds of ways to do this, I guess the best way would depend on what you would be using the result for! Here is a simple (off the top of my head) example using a loop where (coins) are the key in figuring the correct change to give! I can think of (2) other quicker ways to do this, but I used this way so you can see the formatted result returned. Your if() blocks need a loop, so they can add the coins up as you go through the coins, form largest amount to smallest amount

<?

// based on 1 dollars

$coin = array ( 50 => '50 cent piece', 25 => 'quarter', 10 => 'dime', 5 => 'nickel', 1 => array ( 'penny', 'pennies' ) );

// base

$base = 100;

// cost

$cost = 27;

// total

$total = $base - $cost;

$out = array ();

$num = 0;

if ( $total > 0 )
{
	foreach ( $coin AS $cn => $cv )
	{
		if ( $total >= $cn )
		{
			while ( $total >= $cn )
			{
				$out[$num] += 1;

				$total -= $cn;
			}

			if ( $out[$num] > 1 )
			{
				$add = ( is_array ( $cv ) ? $cv[1] : $cv . 's' );
			}
			else
			{
				$add = ( is_array ( $cv ) ? $cv[0] : $cv );
			}

			$out[$num] = '(' . $out[$num] . ') ' . $add;

			$num++;
		}
	}

	echo "change\r\n<br />\r\n" . implode ( "\r\n<br />\r\nand\r\n<br />\r\n", $out );
		
}
else
{
	echo 'no change to give';
}
	
?>

me!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users