gpt4 book ai didi

C 告诉我初始化变量,即使它已经初始化

转载 作者:行者123 更新时间:2023-11-30 18:49:56 25 4
gpt4 key购买 nike

我正在研究 CS50 PSET1。到目前为止我有以下代码:

#include <stdio.h>
#include <cs50.h>

int main(void) {

float change;

do {
printf("Change: ");
change = get_float();
} while(change < 0);

int coins;

for(int q = change; q < 25; q++) {
q = 25 / q;
coins += 1;
}
printf("%i", coins);

}

我遇到了问题。当我尝试使用 make 编译我的代码时命令我收到一条错误消息

greedy.c:17:9: error: variable 'coins' is uninitialized when used here [-> Werror,-Wuninitialized] coins += 1;

最佳答案

编译器是正确的。你从一开始就不会为硬币分配任何东西。您所做的就是增加其(未初始化)值。

要分配初始值,请写入

int coins = 0;  /* or whatever the correct initial value is */

顺便说一句,我不太确定意图是什么,但以下内容极不可能是您想要的:

for(int q = change; q < 25; q++) {
q = 25 / q;

注意赋值如何修改循环变量。虽然这是允许的,但在这种情况下,它看起来不太可能是故意的。

关于C 告诉我初始化变量,即使它已经初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41434121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com