0

I am very stuck with scrabble.

I seem to have the everything working correctly EXCEPT when the words contain the letters 'a','b' or 'c'.

I will post a simplified snippet of the code below and just the compute score section, I have removed some parts of the full solution I had and added some parts that may seem unnecessary because I'm trying to pinpoint what is going wrong.

int compute_score(string word) {

// TODO: Compute and return score for string
   int n = strlen(word);
for (int l = 0; l < n; l++)
    if(isupper(word[l]))
    {
        word[l] = tolower(word[l]);
    }

printf("%s\n", "word: "); printf("%s\n", word);

printf("\n\n");
for (int l = 0; l < n; l++)
    for (int a = 0; a < 26; a++)
    if(word[l] == (a + 97))
    {
        word[l] = POINTS[a];
        printf("%i\n", word[l])
       
    }

    else if(word[l] == a)
    {
        word[l] = 0;
        printf("%s\n", "Here");
        printf("%i\n", word[l]);
        printf("%i\n", a);
        printf("\n");
    }

printf("\n");
int sum = 0;
for (int l = 0; l < n; l++)
{
    sum = sum + word[l];
}
return sum;

}

I am having the code print out the value of each word[l] to identify the problem, and every word[l] prints out the corresponding value of POINTS[a] correctly

Except the letters a,b & c.

So for example, if I enter "xyz" for word1 the program will print

8 4 10

Which is great, that's exactly what I want.

But if for word1 I enter "abc"

The program will output

1 0 3 0 3 0

So there are two outputs for each letter, the first is correct and the second is 0 for some reason.

And when calculating "sum" it only takes into account the '0' outputs.

I have tried so many different things to try to figure out what is causing this and I'm out of ideas.

As you can see in the else if(word[l] == a) section

I am now having it print out the value of a that 'a','b' & 'c' that are triggering this are and when I enter word1: abc

The output is

Here 0 1

Here 0 3

Here 0 3

So 'a' == 1, 'b' == 3 and 'c' == 3, I don't understand how this is possible or why this is happening, and why it doesn't happen to any other letters other than a,b and c.

Any help would be really appreciated.

1 Answer 1

0

For troubleshooting suggest you change the first printf("%i\n", word[l]) to printf("the letter is %c\n", word[l]). Remembering that word[l] needs to be the entered letter for the entirety of the a loop. The spoiler reveals the cause and a suggestion for solving.

The cause of the problem is changing the value of word[l] while iterating over word[l] (in the a loop). When the first letter is 'a', word[l] is changed to 1. On the next iteration of the a loop (i.e. when a = 1), this else if(word[l] == a) is true.

Consider using a different variable for the accumulator thus never changing word[l]. This could also eliminate the need for the "sum" loop. Hint, hint.

1
  • Oh man, thank you so much. I understand, i've been racking my brain for days trying to understand haha.
    – acodeaday
    Feb 7, 2022 at 13:38

You must log in to answer this question.

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