C 中的 Int 变量在未调用时发生变化
提前道歉,我对此很陌生。
我正在尝试根据具有关联分值的 POINTS
数组对“拼字游戏”进行评分。
- 我首先获取
word1
字符串的长度。 - 然后,我创建一个
for
循环,该循环将小写字母的值减去 97(以达到索引 0)并将其添加到数组中。
您可以忽略其余代码,因为这就是问题所在。虽然 stringlength 变量仅定义一次,但我发现它在 for 循环的第二个循环期间发生了某种变化。四字母单词最初的 stringlength
为 4
,但是 for
循环将其更改为 1
第二次运行。
#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 main(void) {
// Get input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
int stringlength = strlen(word1);
int array[] = {};
int array2[] = {};
//Create an array with each letter
for (int index = 0; index < stringlength; index++) {
//THE BELOW LINE IS WHERE "STRINGLENGTH" CHANGES TO "1" ON THE SECOND LOOP
array[index] = word1[index] - 97;
array2[index] = POINTS[array[index]];
}
int score = 0;
for (int index2 = 0; index2 < stringlength; index2++)
score += array2[index2];
printf("%i", stringlength);
printf("\n");
}
我知道我可以创建一个辅助 stringlength
变量,但我知道这是糟糕的编程,我很想知道我做错了什么。
任何帮助将不胜感激。谢谢!
Apologies in advance, I'm very new to this.
I am attempting to score "scrabble words" according to a POINTS
array with associated score values.
- I first get the length of the string of
word1
. - I then create a
for
loop that takes the value of the lower case letter minus 97 (to get to the 0 index) and adds it to the array.
You can ignore the rest of the code, as this is where the issue lies. While the stringlength
variable is only defined once, I've found that it somehow changes during the second cycle of the for
loop. A four letter word will originally have a stringlength
of 4
, however the for
loop changes it to 1
on the second run.
#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 main(void) {
// Get input words from both players
string word1 = get_string("Player 1: ");
string word2 = get_string("Player 2: ");
int stringlength = strlen(word1);
int array[] = {};
int array2[] = {};
//Create an array with each letter
for (int index = 0; index < stringlength; index++) {
//THE BELOW LINE IS WHERE "STRINGLENGTH" CHANGES TO "1" ON THE SECOND LOOP
array[index] = word1[index] - 97;
array2[index] = POINTS[array[index]];
}
int score = 0;
for (int index2 = 0; index2 < stringlength; index2++)
score += array2[index2];
printf("%i", stringlength);
printf("\n");
}
I know I could just create a secondary stringlength
variable but I know that's bad programming and I'd love to know what I'm doing wrong.
Any help would be greatly appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
代码中存在多个问题:
int array[] = {};
是语法错误。数组定义必须至少有一个初始值设定项。在您的情况下,您必须定义长度为stringlength
的array
:您的编译器似乎接受定义并创建一个空数组,该数组的长度不足以存储字母分数:您在两个
for
循环中都有未定义的行为,访问超出其边界的数组元素。未定义的行为意味着任何事情都可能发生,包括您观察到的情况。在
word1[index] - 97
中,您对'a'
的 ASCII 值进行硬编码。这是不好的做法,你应该这样写:但请注意,您在此表达式中还做出了 2 个更无声的假设:
word1
仅包含小写字母,应在用户可以键入任何字符串时对其进行测试。printf("%i", stringlength);
输出字符串长度,而不是分数。这是一个没有附加数组的修改版本:
如果您了解函数,您可以编写一个函数
intcompute_score(const char *word)
以避免重复代码并提高可读性。There are multiple problems in the code:
int array[] = {};
is a syntax error. There must be at least one initializer for an array definition. In your case, you must definearray
with a length ofstringlength
with:your compiler seems to accept the definition and create an empty array, which is not long enough to store the letter scores: you have undefined behavior in both
for
loops, accessing array elements beyond their boundaries. Undefined behavior means anything can happen, including what you observe.in
word1[index] - 97
, you hard code the ASCII value of'a'
. This is bad practice, you should write this instead:note however that you also make 2 more silent assumptions in this expression:
word1
only contains lowercase letters, which should be tested as the user can type any string.printf("%i", stringlength);
outputs the string length, not the score.Here is a modified version without additional arrays:
If you know about functions, you could write a function
int compute_score(const char *word)
to avoid duplicating the code and improve readability.