Jump to content

Recommended Posts

Hi,

this is my first php assignment so please be patient (this is also one of my first time programming in general).  My assignment is to creat a vending machine that has candybars for .65 and handles only .25, .10, and .05 values of currency. We are to use rand(0,10) to create a random number for each amount of currency and I believe this is where my issue lies. The code is not displaying, so I know I have an error, but also I am unsure if I am calling on rand properly. Please if anyone can guide me it would be much appreciated.

<html>
<head>
	<title>Vending Machine</title>
</head>
<body>
	<?php

	//define the amount of nickels
		$NR = (rand(0,10));
		// define variable for nickels
		$N = .05;
		// define variable for total value of nickels
		$NT = $N*$NR;
		echo "Total Nickels is $NR <br />";	

	//define the amount of dimes
		$DR = (rand(0,10));
		// define variable for dimes
		$D = .10;
		// define variable for total value of dimes
		$DT = $D*$DR;		
		echo "Total Dimes is $DR <br />";

	//define the amount of quarters
		$QR = (rand(0,10));
		// define variable for quarters
		$Q = .25;
		// define variable for total value of quarters
		$QT = $Q*$QR;		
		echo "Total Quarters is $QR <br />";

	//define the total amount of money
		$MT = $NT+$DT+$QT;
		//use \$ to insert a $
		echo "Total money is \$ $MT <br />";

	//define the cost of a candy bar
		$CB = .65;
	//define total candy bars purchased
		$CT = $MT/$CB;
		echo "Amount of candy bars dispensed is $CT <br />";

	//define the variable that illustrates change due using modulo
		$CHANGE = $MT%$CB;
		echo "Amount of change due is $CHANGE";

	//find change by giving fewest coins possible, start by dividing change by quarters
		$CH1 = $CHANGE/$Q;
		echo "The amount of quarters is $CH1 <br />";

	//finds change left after dispensing quarters and issues appropriate amount of dimes
		$CH2 = (($CHANGE%$Q)/$D);
		echo "The amount of dimes is $CH2 <br />";
	?>

</body>

</html>


 

thanks for any help in advance

I you do that, you won't get the values of the variables, but rather the names of the variables. In other words:

 

echo 'Total Nickels is $NR <br />';

would literally print this:

Total Nickels is $NR <br />

To use single quotes, you'd have to do this:

echo 'Total Nickels is '.$NR.' <br />';

Hi,

this is my first php assignment so please be patient (this is also one of my first time programming in general).  My assignment is to creat a vending machine that has candybars for .65 and handles only .25, .10, and .05 values of currency. We are to use rand(0,10) to create a random number for each amount of currency and I believe this is where my issue lies. The code is not displaying, so I know I have an error, but also I am unsure if I am calling on rand properly. Please if anyone can guide me it would be much appreciated.

Can you tell us how you're running the code? Make sure you're not running it directly in your web browser (via Open With.. or File Open etc), as web browsers do not understand PHP. You need to run your code in a PHP enabled server environment, your university should provide information about this to you. Also most importantly make sure all PHP code is saved in a file ending in a .php file extension.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.