Do-While 循环正在运行但未获得最终输出
Do-While loop is working but not getting final output
此代码的目的是要求找零,然后输出可用于找零的最小数量的硬币。
不确定为什么我的代码没有输出任何东西?
#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);
}
您的 return
语句在程序中的输出 printf
语句之上。这意味着 get_defined_float
的后半部分是死代码——它永远不会是 运行.
看来您可能想把那个死代码移到您的 main
例程中,用 f
代替 int input = round(n * 100);
语句中的 n
。
您可能还需要向该输出语句添加换行符 (\n
) 以刷新输出缓冲区(通常为 TTY 行缓冲)。
打开一些编译器警告!
此代码的目的是要求找零,然后输出可用于找零的最小数量的硬币。
不确定为什么我的代码没有输出任何东西?
#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);
}
您的 return
语句在程序中的输出 printf
语句之上。这意味着 get_defined_float
的后半部分是死代码——它永远不会是 运行.
看来您可能想把那个死代码移到您的 main
例程中,用 f
代替 int input = round(n * 100);
语句中的 n
。
您可能还需要向该输出语句添加换行符 (\n
) 以刷新输出缓冲区(通常为 TTY 行缓冲)。
打开一些编译器警告!