0

When I run my code, I get proper results including the printf's for Player 1 wins!, Player 2 wins! or Tie! But check50 errors with can not find any of the printf outputs. I've checked, double checked my spelling, does not seem to be the issue. An additional note, VS running this code is very sluggish.

#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};

// declaring/**prototyping** with includes inputs types function compute_score
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);


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

}

    // define Function compute_score, Compute and return score for string
    // POINTS[] arrary stores points for each alph char
   // upper and lower case alph char have equal points for that character
   // non alph char receive no points

int compute_score(string word)                          // compute scrabble score for 
                                                 prompted word passed to function
{

int a = 0;                                              // variable a to cylce through 
                                                     ASCII upper case scenerios
int sum_score = 0;                                      // variable sum_score to return 
                                                         points sum of word

    for (int i = 0; i < strlen(word); i++)              // variable i to cylcle through 
                                                  word to eval each letter for points
    {
            if (isalpha(word[i]))                       // checking for alpha, only 
                                                    alpha characters receive points
            {
                while (toupper(word[i]) != 65 + a)      // cycle through ASCII starting 
                                               with A to determine uppercase letter
                {
                    a++;
                }
            sum_score += POINTS[a];                     // continue sum for each letter 
                                                            in word
            }
    }
    return sum_score;
}

Error Codes

:) scrabble.c exists

:) scrabble.c compiles

:( handles letter cases correctly

Did not find "Tie!" in ""

:( correctly identifies 'hai!' as winner over 'Oh,'

Did not find "Player 2 wins!..." in ""

:( correctly identifies 'COMPUTER' as winner over 'science'

Did not find "Player 1 wins!..." in ""
Fitz
  • 1
  • 2
  • 1
    I think you want to reset `a = 0` in the loop. – 001 Jul 18 '22 at 19:27
  • Reality check: Assuming you're working with an [ascii](https://www.asciitable.com/) platform (or none of this will work), the entire guts of that `if (isalpha(word[i]))` interior block can be reduced to simply the single line: `sum_score += POINTS[toupper(word[i]) - 65];` . The `a` is ultimately worthless (and clearly a point of potential mistakes), since that is all you're really computing here. – WhozCraig Jul 18 '22 at 19:38
  • As per the previous comment, that (the interior block) is also the cause of "_VS running this code is very sluggish_". Suspect check50 is timing out (though it would be politer/more helpful if that was made clear :) – DinoCoderSaurus Jul 18 '22 at 22:08
  • Thank You All so much. Wanted to start with the thanks. Hoping sooner rather than later I will be able to provide such great help to other's questions. I spent a lot of time on this problem, first trying to make it work with nested for loops, and then with the while loop. I will experiment, for my own benefit, resetting the a variable, and then incorporating the ...-65]; code, now that I've seen it. – Fitz Jul 19 '22 at 16:35
  • tltr - I am curious and will test code as is, because I was getting a proper output when I ran the code directly. I can rationalize that words provided could work if the letters were in alphabetical order, and I was testing with "boy" which should not require the var a to be reset. And again, many THANKS, this was extremely helpful as I ran out of ideas to experiment with. – Fitz Jul 19 '22 at 16:36
  • Just a follow-up for anyone who finds this thread in search of help on the cs50 scrabble lab. After the spot on comments regarding var a not being reset, I checked the code with debug50 and sure enough, the while loop would go on and on for letter combinations in reverse alphabetical order, as expected, because the next letter index had already been passed by the previous letter. And as expected, providing two words, both with alph char in alphabetical order the code ran quickly and correctly, the var a index would always be stopped prior to the next letter index. – Fitz Jul 19 '22 at 22:14
  • And since I could not cycle through var a in debug50, I set var a as my return to see what it was, and var a was set at 0 value after the while loop cycles. Perhaps the most disconnect, is that with the var a not reset, the coding still works. Even when I tested words in reverse alphabetical order, I got the correct results. The results took more time to show, but they printf out. – Fitz Jul 19 '22 at 22:18

0 Answers0