即时循环正在工作,但没有最终输出

此代码的目的是要求进行找零,然后输出可用于提供找零的最小数量的硬币。

不确定我的代码为什么不输出任何内容吗?

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

float get_defined_float(string prompt);

int main(void)

{
    float f = get_defined_float("Required Change: ");
}

float get_defined_float(string prompt)
{
    float n;
        do
        {
            n = get_float("Required Change: ");
        }
    while (n<=0);
    return n;




    int input = round(n * 100);
    int quarters = input / 25;
    int dimes = (input % 25) / 10;
    int nickles = (input % 25 % 10) / 5;
    int pennies = (input % 5) / 1;


    int total = quarters + dimes + nickles + pennies;
    printf("%d",total);
}
xujing_521 回答:即时循环正在工作,但没有最终输出

您的return语句在程序中的输出printf语句上方。这意味着get_defined_float的后半部分是无效代码-永远不会运行。

似乎您可能想获取该无效代码并将其移至main例程中,在f语句中用n替换为int input = round(n * 100);

您可能还需要在该输出语句中添加换行符(\n,以便刷新输出缓冲区(通常为TTY行缓冲)。

打开一些编译器警告!

本文链接:https://www.f2er.com/3069040.html

大家都在问