Jump to content

Php Newbie Trying To Make Calculator


Nerock

Recommended Posts

Hi Everyone i am completely new to PHP and im not sure how to create a calculator here is the question.

 

" First, you need the most recent three scores for the Freegumpher. You are just the statistician here, you’ll need to ask the Freegumpher to tell you the scores. (Of course, you will trust the Freegumpher to tell the truth, because Freegumphers always tell the truth.)

It is possible that a Freegumpher for whom you are calculating a handicap has not played Freegumph three times in his/her life. In that case, you will just tell him/her that you cannot determine a handicap.

On the other hand, some Freegumphers will be able to provide their three most recent scores. Then your job is to add them up and determine the average (mean) of the three scores.

Once you have determined the average of the three scores, you have to determine the handicap. To do this, you need to compare the average to the official par for Freegumph, as provided by the National Inter-Collegiate Freegumph Society (NICFS), pronounced just like it looks. It turns out that par for Freegumph is exactly twenty-seven.

If the Freegumpher’s average is higher than or equal to par, then you don’t have to do any more arithmetic – the Freegumpher’s handicap is automatically zero.

On the other hand, if the Freegumpher you are currently working on has an average below the par value, you must determine the difference between the Freegumpher’s average and par (by subtracting the Freegumpher’s average from par – this will always result in a positive number.) Then you have to multiply that difference by an adjustment factor of seven/tenths. The result of that multiplication is the Freegumpher’s handicap.

 

and here is my code any help would be greatly appreciated. when I press the get handicap button nothing happens

 

<HTML>
<HEAD>
<TITLE>My Freegumph Handicap Calculator </TITLE>
</HEAD>
<BODY bgcolor="Green">
<?php
if ($submit)
{

if (($one == "") || ($two =="") || ($three =="") || (!$calc))
   {
   echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Oops, you forgot
   to supply some information.  Please correct the problem below.</b></font><br>");
   }

else if ((!ereg("[0-9]",$one)) || (!ereg("[0-9]",$two) || (!ereg("[0-9]",$three))))
   {
   echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Please restrict
   your input to only numbers.</b></font><br>");
   }
else
{
 $par = 0;[/size][/font][/color]
[color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3]   if ($calc  == "Freegumph Handicap")
  {
  $total = $one + $two + $three ;
  $average = $total / 3 ;[/size][/font][/color]
[color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3]	  if ( $average >= 27 )
  {
	  echo "There is no handicap you're too good!!!<br />";
  }
  else
  {
	  $par = 27 - $average;
  }

  $handicap = $par * .7;
  }
}
printf("The handicap is: <b> %.2f", $handicap . "<br>");
}
?>
<FORM METHOD="post" ACTION="Freegumph.php">
<P><font face="Tahoma" size="2"><b>Enter your First Score: <br><INPUT TYPE="text" NAME="one" SIZE=20></b></font></p>
<font face="Tahoma" size="2"><b>Enter your Second score: <br><INPUT TYPE="text" NAME="two" SIZE=20></b></font></p>
<font face="Tahoma" size="2"><b>Enter your Third Score: <br><INPUT TYPE="text" NAME="three" SIZE=20></b></font></p>
<font face="Tahoma" size="2">
<input TYPE="radio" NAME="calc" VALUE="Freegumph Handicap">Freegumph Handicap
</b></font><br><br>
<P><INPUT TYPE="submit" NAME="submit" VALUE="Get Handicap"></p>
</FORM>
</BODY>
</HTML>
Link to comment
Share on other sites

Sorry to sound like a complete new guy here but i have no idea what apache is my professor basically threw me a bone and said do this and if you can do it then good job, you'll be a good programmer... also heres the website i need to upload it to if that helps any... http://athena.ecs.csus.edu/~cs010130/Freegumph.php

Edited by Nerock
Link to comment
Share on other sites

The top of your script instead of having if ($submit) you need something like this:

 

if (isset($_POST['submit']) {

 

This is the reason that your code currently seems to do nothing -- it never reaches the computation portion because $submit is not set to anything. When you do a form post php makes the values of the form available in the $_POST superglobal. Please read up on that to understand more.

Link to comment
Share on other sites

well you gotta start somewhere right? I dont think we're even learning how to program at this point we're basically just doing flow charts and learning what pseudocode is... see i dont even know what ereg is.... like i said i was basically doing this blind folded... anyone care to explain? Doesnt have to be a great explanation im just trying to learn here.

Link to comment
Share on other sites

well you gotta start somewhere right?
When teaching someone English, don't start with how it was in the 15th century. Start with today. You're learning PHP so out of date it's actually dangerous and won't work on a modern web host.

 

As for ereg: It's a family of functions which has been entirely replaced by the preg family of functions. Ereg is simply old, vestigial, deprecated functionality, left in the language for just a few more years to give out-of-date developers a final chance at updating their code.

Link to comment
Share on other sites

Everything Dan stated is completely right. However, at the end of the day, the main point of using the ereg or preg functions is to perform pattern matching against strings using regular expressions. Regex is just so damn nifty and useful for text pattern matching, that there are regex libraries for just about every language. There are a number of really good books out there on regex, but there are also some great online resources like www.regular-expressions.info.

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.