cs50 scrabble- my own approach to the lab

54 Views Asked by At

So I'm doing my scrabble lab in CS50,

I've looked at other questions posted, but it seems I'm trying a different approach than others have so far, or at least I haven't found one yet.

I created a func to convert to uppercase which I called uppercase, and it compiled, but now that I'm going back and trying to build the "compute_score" func. I can't get the two to work together. I don't know what I'm doing wrong. I've tried a few different lines of code I thought would work. Do I need to write the "uppercase" func above the "compute_score" func? Or am I trying to call "uppercase" incorrectly? I'm sure there's a conversion error somewhere in there that I can't address yet due to the errors I have at the moment? It says that I have undeclared identifiers when it's declared above in the code, also the math line of code isn't correct either, I think I'm trying to call an array inside an array which seems wrong. But as I can't get past the "undeclared" aspect I can't trouble shoot the math line yet. "debug50" wont help me because I can't compile the code.

Here's the full code of what I have so far...

#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 compute_score(string word);
string uppercase(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);

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


int compute_score(string word)
{
// TODO: Compute and return score for string
int i;
string s = uppercase(word[i]);// <-- it givees me undeclared identifier
int score = 0;

if (word[i] >= 65 && word[i] <= 90 ) // <-- again it gives me undeclared but it's declared above
{
   score = score += POINTS[word[i]]; //<-- here I'm getting an error about the array being a "char" type
                                     //how to do I properly convert it to the int that I need it to be
}
return 0;
}


string uppercase(string word)
{
string s = word;
for (int i = 0; i < strlen(s); i++)
{
    printf("%c", toupper(s[i]));
}
return 0;
}

Also I'm sure there are probably other errors I'm missing so feel free to enlighten me on them. I just can't get passed the part I'm stuck on. Any help would be appreciated.

2

There are 2 best solutions below

1
DinoCoderSaurus On

The fundamental problem with string s = uppercase(word[i]); is setting a string (s) to a char (word[i]).
NB the IDE compiler (make) gives error: incompatible integer to pointer conversion passing 'char' to parameter of type 'string'....note: passing argument to parameter 'word' here string uppercase(string word);, then quits.
Even if it did compile, it is a run-time error waiting to happen. i is declared an int, but it is not initialized to a value so word[i] gives unpredictable result.

Since program needs to iterate over each character to compute the score, why reinvent the wheel (ie uppercase) when toupper will do the job?

0
chux - Reinstate Monica On

Problems include:

  • Passing char, where string required. @DinoCoderSaurus s is not used, so call uppercase() with the pointer word and let it update the string.
  // string s = uppercase(word[i]); // <-- it givees me undeclared identifier
  uppercase(word);
  • Missing sequence point. Simplify code
// score = score += 
score += POINTS[(unsigned char) word[i]];
  • No need to recalculate the string length multiply times. Simplify.
  // for (int i = 0; i < strlen(s); i++) {
  for (int i = 0; word[i]; i++) {
  • Do not print the case conversion. Assign the result
    // printf("%c", toupper(s[i]));
    word[i] = toupper((unsigned char) word[i]);
  • Save space as the values are only 1 to 10.
//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};
const signed char 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};
  • Use character constants for more clarity.
  // if (word[i] >= 65 && word[i] <= 90)
  if (word[i] >= 'A' && word[i] <= 'Z')
  • Form a 0-base index. The subtraction result is type int.
    score += POINTS[word[i] - 'A'];
  • Code needs a loop and return the score
  // if (word[i] >= 'A' && word[i] <= 'Z') {
  //   score += POINTS[ word[i] - 'A'];
  // }
  // return 0;

  for (int i = 0; word[i]; i++) {
    if (word[i] >= 'A' && word[i] <= 'Z') {
      score += POINTS[ word[i] - 'A'];
    }
  }
  return score;

Example re-write of compute_score() that also does the uppercase conversion.

int compute_score(string word) {
  int score = 0;
  while (*word) {
    char letter = *word;
    if (letter >= 'a' && letter <= 'z') {
      letter += 'A' - 'a';
    }
    if (letter >= 'A' && letter <= 'Z') {
      score += POINTS[letter - 'A'];
    }
    word++;
  }
  return score;
}