gpt4 book ai didi

CS50 Pset1 现金 : "undefined reference to ' get_change'"

转载 作者:行者123 更新时间:2023-11-30 19:28:20 25 4
gpt4 key购买 nike

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

int main(void)
{
// prompt user for "0.00" value
float dollars;
double get_change(float dollars);
// prompt user for "0.00" value
do
{
dollars = get_float("Change owed: ");
}
while(dollars <= 0);
printf("%f\n", get_change(dollars));

//calculate which coins will be used
int cents = round(dollars * 100);
int coins = 0;
int denom[] = {25, 10, 5, 1};

for (int i = 0; i < 4; i++)
{
coins += cents / denom[i];
cents = cents % denom[i];
}
return coins;
}

在 CS50 中进行 Pset1 现金。收到错误消息“在函数 main”中:
/home/ubuntu/workspace/pset1/cash/cash2.c:15: undefined reference
get_change'clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)make: [cash2] 错误 1"非常困惑

最佳答案

以下建议代码:

  1. 执行所需硬币总数的操作(假设没有纸币)
  2. 正确分离功能
  3. 正确使用子函数的原型(prototype)
  4. 正确地从float转换为int
  5. 通过包含小数点和尾随“f”,正确地将常量定义为 float

现在,建议的代码:

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

int get_change( float dollars );


int main(void)
{
// prompt user for "0.00" value
float dollars;

do
{
dollars = get_float("Change owed: ");
}
while(dollars <= 0.0f);

printf("%d\n", get_change(dollars));
}

// returns number coins needed, not their denominations
int get_change( float dollars )
{
//calculate which coins will be used
int cents = (int)floorf(dollars * 100.0f);
int coins = 0;
int denom[] = {25, 10, 5, 1};

for (int i = 0; i < 4; i++)
{
coins += cents / denom[i];
cents = cents % denom[i];
}
return coins;
}

关于CS50 Pset1 现金 : "undefined reference to ' get_change'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54166685/

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