Jump to content

homework debug


dhmyers82
Go to solution Solved by mac_gyver,

Recommended Posts

I need help debugging some homework.I have narrowed the problem down to the switch. when it gets to the switch it is spitting out the default everytime, also in the default the $score in both echo statment is not showing. Its probably something small killing me I just can't see it and dont have enough experience with php yet.

<body>
    <?php
    $faceNamesSingular = array("one", "two", "three", "four", "five", "six");
    $faceNamesPlural = array("ones", "twos", "threes", "fours", "fives", "sixes");
    
    function checkForDoubles($die1, $die2) {
        global $faceNamesSingular;
        global $faceNamesPlural;
        $returnValue = false;
        if ($die1 == $die2) { //Doubles
            echo "The roll was double ", $faceNamesPlural[$die1-1], ".<br />";
            $returnValue = true;
        }
        else { // Not Doubles
            echo "The roll was a ", $faceNamesSingular[$die1-1], " and a ", $faceNamesSingular[$die2-1], ".<br />";
            $returnValue = false;
        }
        return $returnValue;
    }    
    function displayScoreText($scores, $doubles) {
        switch ($score) {
            case 2:
                echo "You rolled snake eyes!<br />";
                break;
            case 3:
                echo "You rolled a loose deuce!<br />";
                break;
            case 5:
                echo "You rolled a fever five!<br />";
                break;
            case 7:
                echo "You rolled a natural!<br />";
                break;
            case 9:
                echo "You rolled a nina!<br />";
                break;
            case 11:
                echo "You rolled a yo!<br />";
                break;
            case 12:
                echo "You rolled boxcars!<br />";
                break;
            default:
                if ($score % 2 == 0) {//any even number
                    if ($doubles) {
                        echo "You rolled a hard $score!<br />";
                    }
                    else { //Not Doubles
                        echo "You rolled an easy $score!<br />";
                    }
                }
                break;
        }
    }
    
    $dice = array();
    $dice[0] = rand(1,6);
    $dice[1] = rand(1,6);
    $score = $dice[0] + $dice[1];
    echo "<p>";
    echo "The total score for the roll was $score.<br />";
    
    $doubles = checkForDoubles($dice[0], $dice[1]);
    displayScoreText($score, $doubles);
    echo "</p>";
    ?>
</body>
</html>

DiceRoll.php

Link to comment
Share on other sites

  • Solution

your function definition is using $scores as the parameter variable name. your switch() statement is using $score

 

if you set php's error_reporting to E_ALL and display_errors to ON in your php.ini, php would have been throwing an undefined variable error at the $score usage that would have pointed you toward the problem.

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.