这个程序应该通过询问原始金额来平衡一个帐户,然后询问他们想要什么样的交易。当我只需要一个调用时,printf调用会被调用两次。输出:“输入事务:输入事务:”。否则效果很好。代码:
#include <stdio.h>
int main(void)
{
float amount, old, new;
char letter;
printf("Please enter your current balance: ");
scanf("%f", &old);
printf("Enter the kind of transaction you would like: W - withdrawl, D - deposit, F - finished. \n");
scanf("%c", &letter);
while (letter != 'F'){
if (letter == 'D'){
printf("Amount: ");
scanf("%f", &amount);
new = old + amount;
old = new;
}
if (letter == 'W'){
printf("Amount: ");
scanf("%f", &amount);
new = old - amount;
old = new;
}
printf("Enter transaction: ");
scanf("%c", &letter);
}
if (letter == 'F')
printf("Your ending balance is %f.\n", old);
return 0;}
会非常感谢您的任何见解!谢谢你!!
发布于 2015-11-14 00:11:17
scanf(" %c", &letter);而不是scanf("%c", &letter);
原始(没有空格)将将Enter解释为输入。
发布于 2015-11-14 00:20:49
您还可以通过从stdin输入缓冲区清除输入(先前的输入按下)来解决这个问题。添加这一行
fflush(stdin);在此之前
scanf("%c", &letter);但是,建议使用@artm提供的解决方案,但根据平台的不同,我的解决方案可能无法工作。
https://stackoverflow.com/questions/33703591
复制相似问题