0

so im getting

scrabble.c:20:32: error: incompatible pointer types passing
'string' (aka 'char *') to parameter of type 'int *'
[-Werror,-Wincompatible-pointer-types]

because of this line specifically

int score1 = compute_score(word1);

word one that player one types in has numbers assigned to it. "compute_score" is a custom function and the input to that is the global variable POINTS[]. heres the code:

#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.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(int POINTS[]);

int main(int argc, string argv[])
{
    //get input 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!!!");
    }
    else if ( score2 >  score1)
    {
        printf("player 2 wins!!!");
    }
    else if (score1 == score2)
    {
        printf("TIE!");
    }
}
// declare function to compute score
int compute_score(int POINTS[])
{
    // convert non capitol letters to capitol
    int score1 = ((int POINTS[]), toupper);
    return score1;
    
    int score2 = ((int POINTS[]), toupper);
    return score2;
}

ive flirted with the idea of using a function in the library that i think is supposed to force this conversion but i cant remember what its called, or did i just flat out do this wrong?

heres my for loop (pretty sure is erroneous)

string word1;
for (int i=0, n= strlen(word1); i <n; i++)
{
    int score1 =  POINTS, toupper;
    return score1;
}
3
  • so i just sammiched it in a "for" loop. making it iterate over each of the letters in the word and it was acceptable
    – Aamon Van
    Mar 21 at 19:05
  • but now ive got this scrabble.c:51:9: error: incompatible pointer to integer conversion initializing 'int' with an expression of type 'int [26]' [-Werror,-Wint-conversion] int score2 = POINTS, toupper; ...ugh
    – Aamon Van
    Mar 21 at 19:14
  • got rid of a bracket and got this scrabble.c:38:31: error: expected ';' after top level declarator int compute_score(string word) ^ ; scrabble.c:41:5: error: expected identifier or '(' for (int i=0, n= strlen(word1); i <n; i++) ^ scrabble.c:48:5: error: expected identifier or '(' for (int i = 0, n= strlen(word2); i<n; i++)....pretty sure we dont put a semicolon after the first line in a function definition and what is going on with wanting weird parenthesis in my for loops?
    – Aamon Van
    Mar 21 at 19:26
1

You cannot force a string to an int. A string is an array of char values. An int is a single value. As you noted in a comment, you will need to iterate though the char array and convert each of the values individually. For your errors with your for loop, you will need to show how you have written it for me to point out why it is throwing errors. If you edit your post to provide your new code for the compute_score method, I can take a look at it.


EDIT

You have two options for how you can use your POINTS array with your strings to easily find the value of each letter in a word. The first would be to have an array of each letter and use the letters placement in the alphabet to find it's value in POINTS (this is assuming that POINTS has the values in alphabetical order, which it seems to). This would look something like this:

for(int i = 0, n = strlen(word); i < n; i++)
{
    int index;

    for(int j = 0; j < 26; j++)
    {
        if(toupper(word[i]) = LETTERS[j]) //comparing only uppercase letters
        {
            index = j;
        }
    }

    score += POINTS[index];
}

The above example code will compare each letter of a word to the letters of the alphabet. Once it finds the letter's place in the alphabet, it will use that place to find the associated value. You will need to declare the LETTERS constant and make sure that, if you use a similar algorithm, its logic matches the way your function is called. Note that this also shows you the proper way to call the toupper function.

You can also find the index of the letter using it's ASCII value with atoi. To do this, you would need to call this external function for every letter in a loop, similar to the above code:

for(int i = 0; n = strlen(word); i < n; i++)
{
    //for each character in word, ensure it is uppercase
    //and subtract 40 to get location in alphabet because
    //"A" is #41 in ASCII
    int index = atoi(word[i]) - 40;
    score += POINTS[index];
}

There are positives and negatives to both approaches, in my mind at least. While the second looks much simpler and shorter, most of the "work" is done by an external function inside of a loop that will run many times; this is a small program and a small function call, but I try not to rely heavily on an external function call unless it both a) is more efficient than what I can write in the time given and b) is efficient enough that it will not have a significant negative effect on the amount of time that my program will take to run. Neither of my requirements would be violated here, but you will need to make sure that you include the stdlib to have access to atoi.

Writing out the algorithm in a way that does not require most of the work to be done with an external call, though, gives you more control over editing how it works, if a problem should arise or change be needed--I tend to like that. With the first approach, you will need to make sure that you include ctype.h to get the functionality of toupper (though you should already have it since you were using this in your original code). Either way, you will find your own preferences and come across a lot of different approaches to doing the same thing and I tend to try and balance "control" with "don't reinvent the wheel."

Hopefully all of that makes sense and you understand how these two approaches would work. Now, you just have to decide how you would prefer to solve this in your own code.


11
  • i can definitely do that. hang on
    – Aamon Van
    Mar 21 at 21:03
  • its done but definitely not in the format i typed it in...my appologies
    – Aamon Van
    Mar 21 at 21:07
  • What are you trying to accomplish with int score1 = POINTS, toupper;? This line is currently trying assigning an array of ints, POINTS, to a single int, score1, and then improperly calling the toupper function on nothing in particular. Mar 21 at 21:32
  • 1
    Correct. But, you will first need to find out which letter each character in the string is and where it falls in the alphabet so you can get an index to find the correct value in the POINTS array. I recommend making aLETTERS array that you can compare each character to, and then use that index to find the value in the POINTS array. Mar 21 at 22:28
  • 1
    You need to know what letter you are calculating the point for in your string. I made an edit to my answer to better help with this, as it would take too many characters than I can type in a comment. Mar 22 at 11:49
1

Going back to the initial problem, you've created a function called compute_score. It should convert the user input to a score. The score is a number, an int. The user input isn't a number yet, it's a string. The way compute_score is called looks good.

int score1 = compute_score(word1);
int score2 = compute_score(word2);

The 'word1' and 'word2' parameters are strings, which is not what the declaration calls for. To fix that first compile error, the declaration should look like:

int compute_score(string someword);

The input type to a function does not need to be the same type as the output. The POINTS array does not have to be passed to the function, since it's global.

When implementing the function, you'll need to iterate over each char in the string. If you simplify the problem by passing in numbers, you can use the atoi (ascii-to-int) library function. The next step would be to use the POINTS array to convert each char in the string to the point value.

3
  • k i fixed my declaration. my output type to my function is an int, and my input is the string word that i asked for. how do i assign the POINTS[] to the letters in string word1. if im using atoi it'll change my letter in string word to a # and then i can assign it to POINTS[]?
    – Aamon Van
    Mar 21 at 22:17
  • k i sorta get it im changing my letters in string word1 to its ascii value by using atoi but ive never seen atoi used whats its format?
    – Aamon Van
    Mar 21 at 22:20
  • You pass atoi a char and it returns an int. You can read up on it int CS50's documentation: manual.cs50.io/3/atoi Mar 22 at 1:04

You must log in to answer this question.

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