Jump to content

PHP Help


dappercomment

Recommended Posts

Hi all,

I am stuck on an assignment for school and would really appreciate any help or suggestions.

Declare the following variables, use proper PHP syntax for declaring variables:

cash_on_hand and assign some value to it.

meal, and assign some value to it.

tip_percent, and assign some value to it.

cost, to be computed.

Calculate cost with a tip and store it in a variable named: cost. Cost formula is: meal + meal * (tip_percent / 100).

Make sure that cash is a little more than meal price. For example assign $20 for cash_on_hand and $15 for meal and 15% to tip_percent.

Create a while-loop that runs as long as cost is less than cash_on_hand and displays the following in each run :

I can afford a tip of 15% , for Total Cost: (34.5) such that 15% is coming from variable tip_percent and 34.5 from cost. When cost is greater than cash-on-hand, then loop stops. In each iteration, loop displays this message and adds 1% to tip_percent until meal cost exceeds your cash_on_hand.

This is what I have so far:

$cash_on_hand = 35;
$meal = 25;
$tip_percent = 10
$cost = $meal + $meal * ($tip_percent / 100);

$cash_on_hand = 35; while($cash_on_hand <= 35) { echo "I can afford a tip of $tip_percent%, for Total Cost: $cost <br/>"; $tip_percent++; }

I am not sure what I am doing wrong. Thanks for the help!

Link to comment
Share on other sites

Quote

Create a while-loop that runs as long as cost is less than cash_on_hand

while($cash_on_hand <= 35)

Well, that loop is not set up per your instructions. You declared $cash_on_hand twice then in the while condition you hard coded the value (35) instead of using the variable. That loop will run infinitely because $cash_on_hand will always equal 35. You need to calculate $cost on each iteration of the loop and compare that to $cash_on_hand. To be honest, I don't like the instructions given because they require you to define the calculation for $cost at least twice (you should never write code twice - write it once and call it many times).

$cash_on_hand = 35;
$meal = 25;
$tip_percent = 10
$cost = $meal + $meal * ($tip_percent / 100);

$cash_on_hand = 35; //Remove this
while($cash_on_hand <= 35) { //Fix this to test the condition in the instructions
    echo "I can afford a tip of $tip_percent%, for Total Cost: $cost <br/>";
    $tip_percent++;
    //Add a recalculation of $cost here
}

 

Link to comment
Share on other sites

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.