dappercomment Posted July 22, 2021 Share Posted July 22, 2021 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! Quote Link to comment https://forums.phpfreaks.com/topic/313413-php-help/ Share on other sites More sharing options...
Psycho Posted July 22, 2021 Share Posted July 22, 2021 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 } Quote Link to comment https://forums.phpfreaks.com/topic/313413-php-help/#findComment-1588489 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.