ccs50scrabble

Scrabble cs50 computescore


Some of the preset responses are not being checked correctly.

Please don't give the answer, just hoping to get some guidance on how to improve.

preset responses: "hai!" should win over "Oh," , "COMPUTER" should win over "science" .

Below is my code for the computescore function;

int compute_score(string word)
{
    int j = strlen(word);
    int total = 0;
    int index;
    
    
    for (int i = 0; i < j; i++)
    {
        char c = word[i];
        if (isupper(c))
        {
            c = c - 65;
            index = c;
            total = POINTS[index];
        }
    
        if (islower(c))
        {
            c = c - 97;
            index = c;
            total = POINTS[index];
        }
        
    }

Solution

  • Presumably you are trying to make a summation of the character score values.

    This line reassigns the total variable to the latest singular point value every iteration:

    total = POINTS[index];
    

    If you had a look at the value of total after the for loop, you'd find it to be the value of the last valid character in the string.

    Instead, use the plus-equals operator to add on as you go:

    total += POINTS[index];
    

    And don't forget to return something from that function.