CS50 - Lab 2: Scrabble - Error: called object type 'int [26]' is not a function or function pointer scrabble
CS50 - Lab 2: Scrabble - Error: called object type 'int [26]' is not a function or function pointer scrabble
我一直在做这段代码;应该从两个不同的玩家那里获取单词,计算分数,同时使用 for 循环一次一个地遍历每个角色。我已经重读了 1000 遍,但似乎没有帮助。
我在第 45 行和第 49 行收到此错误消息:
Error: called object type 'int [26]' is not a function or function pointer scrabble
不要给我答案,只要给我一些指导。谢谢你。这是我的代码:
#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);
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 is the winner!\n");
}
else if (score1 < score2)
{
printf("Player 2 is the winner!\n");
}
else if (score1 == score2)
{
printf("Tie!\n");
}
}
int compute_score(string word)
{
//Compute and return store for string
int score = 0;
int l = strlen(word);
for (int n = 0; n < l; n++)
{
if (isupper(word[n]))
{
score += POINTS(word[n] - 'A');
}
else if (islower(word[n]))
{
score += POINTS(word[n] - 'a');
}
}
return score;
}
正如编译器所说,POINTS
是一个 int [26]
类型的数组。有问题的行是这些:
POINTS(word[n] - 'A')
那是胡说八道,你不能那样使用数组。编译器认为它看起来像一个函数调用 ()
,因此出现了奇怪的编译器错误。您可能打算访问数组,这将是:
POINTS[word[n] - 'A']
但是你已经知道了,因为你 word[n]
正确...
一个提示是,不要过分关注编译器错误的含义,而应关注它所指向的行。编译器消息可能非常含糊,有些需要 C 语言老手才能理解。现在,把它们当作编译器在说“坏:第 26 行”。
我一直在做这段代码;应该从两个不同的玩家那里获取单词,计算分数,同时使用 for 循环一次一个地遍历每个角色。我已经重读了 1000 遍,但似乎没有帮助。
我在第 45 行和第 49 行收到此错误消息:
Error: called object type 'int [26]' is not a function or function pointer scrabble
不要给我答案,只要给我一些指导。谢谢你。这是我的代码:
#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);
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 is the winner!\n");
}
else if (score1 < score2)
{
printf("Player 2 is the winner!\n");
}
else if (score1 == score2)
{
printf("Tie!\n");
}
}
int compute_score(string word)
{
//Compute and return store for string
int score = 0;
int l = strlen(word);
for (int n = 0; n < l; n++)
{
if (isupper(word[n]))
{
score += POINTS(word[n] - 'A');
}
else if (islower(word[n]))
{
score += POINTS(word[n] - 'a');
}
}
return score;
}
正如编译器所说,POINTS
是一个 int [26]
类型的数组。有问题的行是这些:
POINTS(word[n] - 'A')
那是胡说八道,你不能那样使用数组。编译器认为它看起来像一个函数调用 ()
,因此出现了奇怪的编译器错误。您可能打算访问数组,这将是:
POINTS[word[n] - 'A']
但是你已经知道了,因为你 word[n]
正确...
一个提示是,不要过分关注编译器错误的含义,而应关注它所指向的行。编译器消息可能非常含糊,有些需要 C 语言老手才能理解。现在,把它们当作编译器在说“坏:第 26 行”。