错误:忽略使用 pure 属性声明的函数的返回值
#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);
// TODO: Print the winner
if (score1 > score2) {
printf("Player 1 wins!\n");
} else if (score2 > score1) {
printf("Player 2 wins!\n");
} else {
printf("Tie!\n");
}
}
int compute_score(string word)
{
// TODO: Compute and return score for string
int score = 0;
for (int i = 0, n = strlen(word); i < n; i++) {
if (isalpha(word[i])) {
tolower(word[i]);
score += POINTS[word[i] - 64];
}
}
return score;
}
似乎是说compute_score函数的返回值有问题。我尝试了一切,但不知道发生了什么。它说它位于第 38 行第 13 行。我尝试注释掉代码的某些部分,但仍然出现相同的错误。
#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);
// TODO: Print the winner
if (score1 > score2) {
printf("Player 1 wins!\n");
} else if (score2 > score1) {
printf("Player 2 wins!\n");
} else {
printf("Tie!\n");
}
}
int compute_score(string word)
{
// TODO: Compute and return score for string
int score = 0;
for (int i = 0, n = strlen(word); i < n; i++) {
if (isalpha(word[i])) {
tolower(word[i]);
score += POINTS[word[i] - 64];
}
}
return score;
}
It seems to be saying there is something wrong with the return value of the compute_score
function. I tried everything but don't know what is up. It says it is on line 38 character 13. I tried to comment out certain parts of the code but it still comes up with the same error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据推测,该消息来自
tolower(word [i]);
line - 因为您是忽略调用的返回值tolower
。而且您不应该忽略它,因为它是要分配给word [i]
的值。tolower 函数 )
int
参数,这意味着它是按值传递的(即发送到函数)和变量的值通话功能不会(无法更改)。相反,(可能)修改的(小写)字符将在您忽略的返回值中。
因此,使用此信息,而不是您进行的错误调用:(
许多人会懒惰地忽略这两个演员;但是它们确实应该使用应该使用:在调用toupper(),tolower()等)之前,我还需要施放未签名的char
(尽管与您的错误消息无关),以下行是高度可疑:
使用ASCII字符集,这将为 uppercase 字母的阵列提供基于 1的索引;小写字母实际上将保证会导致访问
积分
数组的界限错误。不要在代码中使用这样的“魔术”数字。对于使用小写字母基于的索引字母,请使用以下操作:
实际上,您可以将我提供的两行校正组合为一个:
Presumably, that message is coming from the
tolower(word[i]);
line – because you are ignoring the return value of the call totolower
. And you shouldn't be ignoring it, because it's the value you want to assign toword[i]
.The
tolower
function takes a (plain)int
argument, which means it is passed by value (i.e. a copy is sent to the function) and the value of the variable in the calling function won't (can't) be changed.Instead, the (possibly) modified (lowercase) character will be in the returned value that you are ignoring.
So, instead of the erroneous call you make, use this:
(Many people will lazily ignore the two casts; but they really should be used: Do I need to cast to unsigned char before calling toupper(), tolower(), et al.?)
Also (though not related to your error message), the following line is highly suspicious:
Using the ASCII character set, this will give the 1-based index into your array for uppercase letters; lowercase letters will be virtually guaranteed to cause an out-of-bounds error accessing the
POINTS
array.Don't use such "magic" numbers in your code. For a zero-based index using lowercase letters, use this:
In fact, you can combine the two lines I have offered corrections for into one: