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.
The fundamental problem with
string s = uppercase(word[i]);is setting a string (s) to a char (word[i]).NB the IDE compiler (
make) giveserror: 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.
iis declared an int, but it is not initialized to a value soword[i]gives unpredictable result.Since program needs to iterate over each character to compute the score, why reinvent the wheel (ie
uppercase) whentoupperwill do the job?