Jump to content

homework debug


dhmyers82

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
https://forums.phpfreaks.com/topic/294286-homework-debug/
Share on other sites

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
https://forums.phpfreaks.com/topic/294286-homework-debug/#findComment-1504435
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.