Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CS50 Pset1现金:“;未定义对'的引用;找零钱'&引用; #包括 #包括 #包括 内部主(空) { //提示用户输入“0.00”值 浮动美元; 双倍兑换(浮动美元); //提示用户输入“0.00”值 做 { 美元=获得浮动(“欠的零钱:”); } 而(美元_C_Cs50 - Fatal编程技术网

CS50 Pset1现金:“;未定义对'的引用;找零钱'&引用; #包括 #包括 #包括 内部主(空) { //提示用户输入“0.00”值 浮动美元; 双倍兑换(浮动美元); //提示用户输入“0.00”值 做 { 美元=获得浮动(“欠的零钱:”); } 而(美元

CS50 Pset1现金:“;未定义对'的引用;找零钱'&引用; #包括 #包括 #包括 内部主(空) { //提示用户输入“0.00”值 浮动美元; 双倍兑换(浮动美元); //提示用户输入“0.00”值 做 { 美元=获得浮动(“欠的零钱:”); } 而(美元,c,cs50,C,Cs50,以下建议代码: #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 {

以下建议代码:

#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;
}
  • 执行所需硬币总数的操作(假设没有纸币)
  • 适当分离功能
  • 正确使用子功能的原型
  • 正确地从
    float
    转换为
    int
  • 通过包含小数点和尾随的“f”,将常量正确定义为
    float
  • 现在,拟议的守则:

    #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;
    }
    
    #包括
    #包括
    #包括
    零钱(浮动美元);
    内部主(空)
    {
    //提示用户输入“0.00”值
    浮动美元;
    做
    {
    美元=获得浮动(“欠的零钱:”);
    }
    而(美元<代码>#包括
    #包括
    #包括
    零钱(浮动美元);
    内部主(空)
    {
    浮动美元;
    //提示用户输入0.00金额
    做
    {
    美元=获得浮动(“欠的零钱:”);
    }
    美元<0;
    //
    printf(“%i\n”,获取零钱(美元));
    }
    整数兑换(浮动美元)
    {
    //计算将使用多少枚硬币
    整数分=整数(美元*100);
    整数=0;
    int denom[]={25,10,5,1};
    对于(int i=0;i<4;i++)
    {
    硬币+=美分/分币[i];
    美分=美分%denom[i];
    }
    归还硬币;
    }
    

    我对我的问题的正式回答。

    您缺少函数声明double get_change(浮动美元);函数呢?函数声明是什么?我很困惑您似乎试图在
    main
    中定义
    get\u change
    。我建议您再次查看有关函数的相关资料。阅读此文您声明并调用了函数
    get\u change()
    ,但您从未定义过它。(在
    浮动美元之后声明;
    -调用
    printf()
    语句。)注释前面的代码
    计算将使用哪些硬币
    可能是该功能的主体。您似乎只是在计算硬币总数,而没有识别哪些硬币。我办公室的自动售货机也分发1美元硬币。(但您不希望处理1美元的硬币,尽管您的代码使处理变得相当容易——使用循环和数组做得很好。)
    #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;
    }