无法弄清楚这个分段错误
Can't figure out this segmentation fault
我对编程还很陌生,我正在做 CS50,在这个拼字游戏程序中我遇到了麻烦,因为我无法弄清楚我犯了哪个分段错误。可能是我的索引在我的数组之外。如果您能找到问题并向我解释一下,我将不胜感激。
#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);
if (score1 > score2)
{
printf("Player 1 wins!\n");
}
else if (score1 == score2)
{
printf("Tie!\n");
}
else
{
printf("Player 2 wins!\n");
}
// TODO: Print the winner
}
int compute_score(string word)
{
int number;
int sum1=0;
for (int i = 0; i<strlen(word); i++)
{
if (isupper(word))
{
number = POINTS[word[i]-'A'];
}
else if (word[i] < 97 || word[i]>122 || word[i]<65 || word[i]>90)
{
number = 0;
}
else
{
number = POINTS[word[i] - 'a'];
}
sum1 = sum1 + number;
}
return sum1;
// Assign points to letters
// read the letters in the word
// covert the letters into scores and add
// TODO: Compute and return score for string
}
if语句中至少有这个条件
if (isupper(word))
不正确。你必须写
if (isupper( ( unsigned char )word[i]))
这个if语句中的条件
else if (word[i] < 97 || word[i]>122 || word[i]<65 || word[i]>90)
没有意义。对于初学者,不要使用像 97 这样的幻数。条件可以看起来像
else if ( !( ( word[i] >= 'a' && word[i] <= 'z' ) || ( word[i] >= 'A' && word[i] <= 'Z' ) ) )
我对编程还很陌生,我正在做 CS50,在这个拼字游戏程序中我遇到了麻烦,因为我无法弄清楚我犯了哪个分段错误。可能是我的索引在我的数组之外。如果您能找到问题并向我解释一下,我将不胜感激。
#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);
if (score1 > score2)
{
printf("Player 1 wins!\n");
}
else if (score1 == score2)
{
printf("Tie!\n");
}
else
{
printf("Player 2 wins!\n");
}
// TODO: Print the winner
}
int compute_score(string word)
{
int number;
int sum1=0;
for (int i = 0; i<strlen(word); i++)
{
if (isupper(word))
{
number = POINTS[word[i]-'A'];
}
else if (word[i] < 97 || word[i]>122 || word[i]<65 || word[i]>90)
{
number = 0;
}
else
{
number = POINTS[word[i] - 'a'];
}
sum1 = sum1 + number;
}
return sum1;
// Assign points to letters
// read the letters in the word
// covert the letters into scores and add
// TODO: Compute and return score for string
}
if语句中至少有这个条件
if (isupper(word))
不正确。你必须写
if (isupper( ( unsigned char )word[i]))
这个if语句中的条件
else if (word[i] < 97 || word[i]>122 || word[i]<65 || word[i]>90)
没有意义。对于初学者,不要使用像 97 这样的幻数。条件可以看起来像
else if ( !( ( word[i] >= 'a' && word[i] <= 'z' ) || ( word[i] >= 'A' && word[i] <= 'Z' ) ) )