如何获得此代码以拒绝负数(以浮点数的形式)?这是CS50问题集一的一部分吗?尝试编译时,在“ n = get_int(%n,提示”中生成错误。
#include <cs50.h>
#include <stdio.h>
//prompts user to submit amount of change
int get_positive_float(string prompt);
int main(void)
{
float change = get_positive_float("Change owed: ");
printf("%.2f\n", change);
}
//ensures user submits a positive number
int get_positive_float(string prompt)
{
int n;
do
{
n = get_int(%n, prompt);
}
while (n < 0);
return n;
}
这行:
Is wrong.
%
is the modulo operator and needs two operands. Maybe you intended this to be some kind of format string? You don't have a definition ofget_int
present, so it's hard to say.As an editorial aside, it's a bit weird to have a function called
get_positive_float
that returns anint
.