CS50 Pset1 现金:"undefined reference to 'get_change'"
CS50 Pset1 Cash: "undefined reference to 'get_change'"
#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 Cash。收到错误消息“在函数 main':
/home/ubuntu/workspace/pset1/cash/cash2.c:15: undefined reference to
get_change 中”
clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
make: [cash2] 错误 1" 很困惑
以下建议代码:
- 执行所需硬币总数的运算(假设没有纸币)
- 正确分隔功能
- 正确使用子函数的原型
- 正确地从
float
转换为 int
- 通过包含小数点和尾随 '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;
}
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int get_change(float dollars);
int main(void)
{
float dollars;
//prompts user for 0.00 amount
do
{
dollars = get_float("change owed: ");
}
while (dollars < 0);
//
printf("%i\n", get_change(dollars));
}
int get_change(float dollars)
{
//calculate how mnay 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;
}
我对我的问题的正式回答。
#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 Cash。收到错误消息“在函数 main':
/home/ubuntu/workspace/pset1/cash/cash2.c:15: undefined reference to
get_change 中”
clang:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
make: [cash2] 错误 1" 很困惑
以下建议代码:
- 执行所需硬币总数的运算(假设没有纸币)
- 正确分隔功能
- 正确使用子函数的原型
- 正确地从
float
转换为int
- 通过包含小数点和尾随 '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;
}
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int get_change(float dollars);
int main(void)
{
float dollars;
//prompts user for 0.00 amount
do
{
dollars = get_float("change owed: ");
}
while (dollars < 0);
//
printf("%i\n", get_change(dollars));
}
int get_change(float dollars)
{
//calculate how mnay 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;
}
我对我的问题的正式回答。