1

Could you please help me find out about variable scope? Below is a program for the CS50 Scrabble assignment.

I initialized score1 = 0, score2 = 0 atop. I thought since they are global variables, when they are manipulated in one funciton (compute_score), its effect carries through to every other function without my overwriting them. But, it seems that the values of both varialbes are unchanged throughout the program, and as a result I get "Tie!" in the end everytime I run the program. Could you explain why this happens for me? Thank you so much in advance!

#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>

// 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 score1 = 0, score2 = 0;

int compute_score(string word, int score);

int main(void)
{
    // Prompt the user for two words
    string word1 = get_string("Player 1: ");
    string word2 = get_string("Player 2: ");

    // Compute the score of each word
    compute_score(word1, score1);
    compute_score(word2, score2);

    // Print the winner
    if (score1 > score2)
    {
        printf("Player 1 wins!\n");
    }
    else if (score1 < score2)
    {
        printf("Player 2 wins!\n");
    }
    else
    {
        printf("Tie!\n");
    }
}

int compute_score(string word, int score)
{
    // Compute score for each character
    for (int i = 0, len = strlen(word); i < len; i++)
    {
        if (isupper(word[i]))
        {
            score += POINTS[word[i] - 'A'];
        }
        else if (islower(word[i]))
        {
            score += POINTS[word[i] - 'a'];
        }
    }

    return score;
}

2 Answers 2

2

First: you shouldn't do this global variables, and you really can't for reasons you will see.

Second: The problem is that you are not actually 'using' your global variables in your function call compute_score. You are passing by value the value of the global value into the function. In the function compute_score the variable score is a local variable that is initialized with the value from the global, and when you change it you aren't changing the global's value but the local variable score's value.. When the function terminates score is destroyed and the changes to it are of course lost. If you wanted to use a global you would have to reference the global directly in the function but that is a bad idea.

The proper way to do this is to make the variables score1 and score2 non-global, declare them inside main. The second key change is that you can't pass them by value if you want the change to occur in the function and be persisted. You can either pass by value and return the result and assign that to the variable ( what Captain Nemo was suggesting basically ) or you can pass the value by address and then actually modify the value and it will be persisted. In the end the choice is up to you.

1

In your code return value of compute_score isn't assigned to any variable. it should be corrected as,

score1 = compute_score(word1, score1); 
score2 = compute_score(word2, score2);

In your code it's always a tie because values of score1 and score2 do not change after the initial assignment.

3
  • while the code correction is correct you dont provide any explanation as to why which is more important for the OP.
    – UpAndAdam
    Commented Jan 8 at 15:33
  • @UpAndAdam yea actually sorry for that I'm not that good at english. btw your explanation is great! Commented Jan 9 at 15:49
  • you should explain why score1 and score2 arent changed in the function call as OP expects them to based on them being 'global'. thank you im glad you like my explanation if yuo find it useful i would encourage you to upvote it. If you add the missing bit i would upvote yours as well.
    – UpAndAdam
    Commented Jan 9 at 21:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .