工作花车和整数 [英] Working With Floats and Integers
问题描述
我创建了像节目的ATM持有的钱,一个人的帐户。当人拿出一个撤军,它减去从帐户.50收费沿撤出。我遇到的问题是,在这个方案既整数和浮点数的工作。我转换整数账户浮点数,但是当我尝试打印出来的发言中,我得到一个错误信息。谁能告诉我什么,我做错了吗?
的#include<&stdio.h中GT;诠释主要(无效){
INT账户= 2000;
INT停药;
浮充= 0.50; 的printf(多少钱你想取出来?);
scanf函数(%i的,&安培;撤); 而(撤%5!= 0){
的printf(提款必须是5整除);
scanf函数(%i的,&安培;撤);
} 账户=((浮点)账户 - 费用) - 停药; 的printf(剩余账号:%.2f \\ n,账户); 返回0;
}
INT账户= 2000;
的printf(剩余账号:%.2f \\ n,账户);
这是错误的;它应该是%D
为整数,或者更好的,改变你的帐户
变量类型的东西,可以重新present你surcharging 0.50。我不建议你使用(IM precise)浮动
来要钱无论是。你不想撤 10.499999997
时,你的意思是 10.5
。你需要想想precision,你会使用四舍五入规则。据我所知,这些都是由法律或规定的东西。
I've created an ATM like program which holds the money in a person's account. When the person takes out a withdrawal, it subtracts the withdrawal from the account along with a .50 surcharge. The problem I'm having is working with both integers and floats in this program. I converted the integer account to a floating point number but I get an error message when I try to print out the statement. Can someone tell me what I'm doing wrong?
#include <stdio.h>
int main (void) {
int account = 2000;
int withdrawal;
float charge = 0.50;
printf ("How much money would you like to take out? ");
scanf ("%i", &withdrawal);
while (withdrawal % 5 != 0) {
printf ("Withdrawal must be divisible by 5. ");
scanf("%i", &withdrawal);
}
account = ((float) account - charge) - withdrawal;
printf("Remaining account: %.2f\n", account);
return 0;
}
int account = 2000;
printf("Remaining account: %.2f\n", account);
This is wrong; it should be "%d"
for integer, or better, change your account
variable type to something that could represent the 0.50 you're surcharging. I don't recommend you to using (imprecise) float
s for money either. You don't want to withdraw 10.499999997
when you meant 10.5
. You need to think about the precision and rounding rules you'd use. AFAIK these are both mandated by laws or something.
这篇关于工作花车和整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!