为什么删除printf函数会弄乱我的代码?CS50 pset1现金贪婪挑战赛

为什么删除printf函数会弄乱我的代码?CS50 pset1现金贪婪挑战赛,cs50,greedy,printf-debugging,Cs50,Greedy,Printf Debugging,我完成了挑战,我的第一个C代码显然运行得很好,每次都返回正确的最小硬币数量以进行更改。然后,当我试图“清理”一点并删除一个多余的printf时,一切似乎都出了问题。我无法用脑子思考这个问题,我很困惑。。。为什么会发生这种情况 #include <cs50.h> #include <stdio.h> #include <math.h> int q = 25; //variable for quarters int d = 10; //dimes int n =

我完成了挑战,我的第一个C代码显然运行得很好,每次都返回正确的最小硬币数量以进行更改。然后,当我试图“清理”一点并删除一个多余的printf时,一切似乎都出了问题。我无法用脑子思考这个问题,我很困惑。。。为什么会发生这种情况

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

int q = 25; //variable for quarters
int d = 10; //dimes
int n = 5;  //nickels
int p = 1;  //pennies

int x;  //variable for final result
int r;  // variable for the reminder

float amount(string prompt);

int main(void)
{
    float a = amount("Enter dollar amount with format 0.00: $");
    int cents = round(a * 100);
    printf("Your input: $ %.2f", a);
    // printf(", which amounts to %i total.\n", cents); //WHY DELETING THIS LINE MESSES UP WITH THE FLOAT AND THE RESULT?

    x = cents / q;
    r = cents % q;

    x = x + (r / d);
    r = r % d;

    x = x + (r / n);
    r = r % n;

    x = x + (r / p);
    printf("%i\n", x);
    r = r % p;
    printf("%i\n", r);
}

float amount(string prompt)
{
    float a;
    do {
        a = get_float("%s", prompt);
    }
    while (a <= 0);
    return a;
}
#包括
#包括
#包括
int q=25//季度变量
int d=10//一角硬币
int n=5//镍币
int p=1//便士
int x//最终结果的变量
int r;//用于提醒的变量
浮动金额(字符串提示);
内部主(空)
{
浮动a=金额(“输入美元金额,格式为0.00:$”;
整数分=圆形(a*100);
printf(“您的输入:$%.2f”,a);
//printf(“,相当于%i总计。\n”,美分);//为什么删除此行会弄乱浮点和结果?
x=美分/季度;
r=美分%q;
x=x+(r/d);
r=r%d;
x=x+(r/n);
r=r%n;
x=x+(r/p);
printf(“%i\n”,x);
r=r%p;
printf(“%i\n”,r);
}
浮动金额(字符串提示)
{
浮动a;
做{
a=获取浮点值(“%s”,提示);
}

虽然(a这是一件小事,但我就是看不见。。。 删除该陈述而不在上面添加换行符将在输入后立即得到答案,因此在我看来,这是一个错误的额外数字和缺少的解决方案(我知道…哈)。基本上:

 $ ./cash3
Enter dollar amount with format 0.00: $1.12
7
0
…变成了

Enter dollar amount with format 0.00: $1.12
Your input: $ 1.127
0
有时候,靠近树看会让你错过整个森林,哈哈
感谢Blauelf在这方面提供的帮助和解决方案!

当您删除打印语句时会发生什么?具体出了什么问题?您好,感谢您的评论。其他人指出了问题所在,这是一个非常明显的问题,但我看不到它!基本上,由于我错过了换行符\n练习的结果将是next,在我看来是输入上的一个额外数字,结果丢失了…相当尴尬,学习编码是我能记得的最谦卑的经历之一。哈哈。再次感谢