cs50 拼字游戏 - 我自己的实验室方法

问题描述 投票:0回答:1

所以我正在 CS50 中做我的拼字游戏实验室,

我已经查看了发布的其他问题,但似乎我正在尝试一种与其他人迄今为止不同的方法,或者至少我还没有找到。

我创建了一个函数来转换为大写,我称之为大写,并且它已编译,但现在我要返回并尝试构建“compute_score”函数。我无法让两者一起工作。我不知道我做错了什么。我尝试了几行我认为可行的不同代码。我需要在“compute_score”函数上方编写“uppercase”函数吗?或者我试图错误地调用“大写”?我确定那里有一个转换错误,由于我目前遇到的错误,我还无法解决?它说,当我在代码中声明上面时,我有未声明的标识符,并且代码的数学行也不正确,我认为我正在尝试在数组内调用数组,这似乎是错误的。但由于我无法克服“未声明”的方面,我还无法解决数学问题。 “debug50”不会帮助我,因为我无法编译代码。

这是我到目前为止所拥有的完整代码...

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

此外,我确信我可能还遗漏了其他错误,因此请随时启发我。我就是无法通过我所坚持的部分。任何帮助将不胜感激。

arrays math cs50 scrabble
1个回答
0
投票

string s = uppercase(word[i]);
的根本问题是将字符串(
s
)设置为字符(
word[i]
)。
注意 IDE 编译器 (
make
) 给出
error: incompatible integer to pointer conversion passing 'char' to parameter of type 'string'....note: passing argument to parameter 'word' here string uppercase(string word);
,然后退出。
即使它确实编译了,这也是一个等待发生的运行时错误。
i
被声明为 int,但它未初始化为值,因此
word[i]
会给出不可预测的结果。

既然程序需要迭代每个字符来计算分数,当

uppercase
可以完成这项工作时,为什么还要重新发明轮子(即
toupper
)呢?

© www.soinside.com 2019 - 2024. All rights reserved.