【发布时间】:2017-11-11 01:07:22
【问题描述】:
我在程序中的目标是提示用户输入一定范围内的货币价值(浮动)以用于另一个功能。但是,使用字符数组作为输入来完成另一个功能似乎要容易得多。我已经研究过使用 sprintf 和 snprintf,但不确定如何/是否可以使用变量输入而不是常量来实现这些。
这个数字将被传递给的函数需要将数字转换为书面文字。示例:1150.50 = 一千一百五十美元五十美分。
这是我要实现的代码段;
do {
puts("Please enter the amount of the paycheck, this must be from 0$ to 10000$: \n");
scanf("%.2f", entered_amount);
if (entered_amount < 0.00 && entered_amount > 10000.00) {
printf("This is not a valid amount, please try again! \n\n");
}
} while (entered_amount < 0.00 && entered_amount > 10000.00);
sprintf(amount, "%f", entered_amount);
//Trying to convert a float entered by the user to an array of characters to use in the number_to_word function!
printf("%s", amount);
entered_amount 是用户输入的浮点数,amount 是 字符数组。前任: 5555.55 = {"5,5,5,5,.,5,5"}
感谢所有帮助和反馈,谢谢!
【问题讨论】:
-
1)
entered_amount < 0.00 && entered_amount > 10000.00)-->entered_amount < 0.00 || entered_amount > 10000.00) -
啊,很好,谢谢!
-
scanf("%.2f", entered_amount);...emmm..在某处缺少&? -
当第一次输入负数然后输入不是数字时,这个(带有建议的修复)永远循环。