ATM CodeChef仅返回0.00

时间:2015-09-03 21:22:02

标签: c

我是编程新手并试图解决C中的CodeChef问题。以下是问题的链接:https://www.codechef.com/problems/HS08TEST程序应从ATM中读取提款金额并显示新余额减去提款费用。原始余额是< = 2000.以下是我目前拥有的代码。所有提款金额的输出目前为0.00。

int main() {

int withdrawAmount; 
float withdrawFee = 0.5;
const int beginAccountBalance = 2000;
float endAccountBalance; 

printf("How much do you wish to withdraw? ");
scanf("%", withdrawAmount); 

if((withdrawAmount %5 == 0)&&(0 < withdrawAmount <= beginAccountBalance))
{ 
    endAccountBalance = beginAccountBalance - withdrawAmount - withdrawFee; 
    printf("Account Balance is %.2f", endAccountBalance);
}
else {
    printf("Account Balance is %.2f", beginAccountBalance);
}    
return 0;

}

1 个答案:

答案 0 :(得分:1)

事情进展顺利的主要原因是您使用了scanf

scanf("%", withdrawAmount);  

您的使用存在两个问题:

  1. %之后,您忘记输入标识符(标识符与您在printf次调用中使用的标识符不同)。在您的情况下,您需要做的就是将%更改为%d%d基本上告诉scanf扫描一个整数,这是withdrawAmount所属的数据类型。
  2. 如果您需要有关标准C函数的信息,cplusplus.com可作为函数用法的绝佳参考。我会在scanf上看到他们的页面,以了解scanf使用的标识符。

    1. 当您将withdrawAmount作为参数传递给scanf时,您没有在变量之前放置&。变量前面的&运算符基本上检索它所使用的变量的地址。你需要在scanf中使用它,因为scanf基本上取你给它的变量的内存地址,当它扫描标准输入(在你的情况下是终端条目)时,它会隐藏数据的值找到你给它的内存地址。
    2. 因此,

      scanf("%", withdrawAmount);  
      

      应改为

      scanf("%d", &withdrawAmount);  
      

      我让你弄清楚你的实现是否在数学上合理。继续努力!

相关问题