lampentisch
lampentisch

Reputation: 9

Upper case letter input in scrabble game always returns 0

i wrote a code for cs50. My task was to create a scrabble game. everything worked fine with lower case letters but when i tried upper case letter the value the computer returned to me was always the same 0. I tried to fix it on my own but i only made it worse. i would appreciate it if someone could tell me how.

// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};

int compute_score(string word);

int main(void)
{
    // Get input words from both players
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Score both words
    int score1 = compute_score(word1);
    int score2 = compute_score(word2);

    printf("Score for Player 1: %i\n", score1);
    printf("Score for Player 2: %i\n", score2);
    // Print the winner
    if (score1 == score2)
    {
        printf("Tie! \n");
    }
    if (score1 < score2)
    {
        printf("Player 2 wins! \n");
    }
    if (score1 > score2)
    {
        printf("Player 1 wins! \n");
    }
}

int compute_score(string word)
{
    // Compute and return score for string
    int compute_score = 0;
    int numb;
    for (int i = 0, n = strlen(word); i < n; i++)
    {
        if(isupper(word[i]))
        {
            numb = word[i] - 65;
            numb = POINTS[numb];
        }
        if(islower(word[i]))
        {
            numb = word[i] - 97;
            numb = POINTS[numb];
        }
        else
        {
           numb = 0;
        }
        compute_score += numb;
    }
    return compute_score;
}

Upvotes: 0

Views: 174

Answers (1)

Eric Postpischil
Eric Postpischil

Reputation: 223693

Consider this code:

        if(isupper(word[i]))
        {
            numb = word[i] - 65;
            numb = POINTS[numb];
        }
        if(islower(word[i]))
        {
            numb = word[i] - 97;
            numb = POINTS[numb];
        }
        else
        {
           numb = 0;
        }

Let’s replace the bodies with single letters so we can focus on the conditions and code flow:

        if(isupper(word[i]))
            {A}
        if(islower(word[i]))
            {B}
        else
            {C}

Look at the first if. Its body is {A}, and it is followed by another if. There is no else with it. When this if is executed, {A} will be executed if the condition is true. Then the code goes on to the next if.

That second if is always executed, regardless of whether {A} was executed or not. There is no else preventing the second if from being executed.

Depending on that second condition, either {B} or {C} will be executed, because that is an if statement with an else.

So, if there is an uppercase letter, the first condition is true, {A} is executed, the second condition is false, so {B} is skipped, and {C} is executed. That sets numb to zero, so this routine always produces zero for a capital letter, because it executes both {A} and {C}.

What you want is for just one of {A}, {B}, or {C} to be executed. To do that, nest the second if inside an else:

        if(isupper(word[i]))
            {A}
        else
            if(islower(word[i]))
                {B}
            else
                {C}

That indentation shows the formal grammar for these statements—the second if is technically inside the else. However, since these statements chain nicely, we often write it this way:

        if(isupper(word[i]))
            {A}
        else if(islower(word[i]))
            {B}
        else
            {C}

Now exactly one of those bodies will be executed—{A} if the letter is uppercase, {B} if the letter is lowercase, and {C} otherwise.

Upvotes: 1

Related Questions