首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在C: 2打印输出时,我只需要一个输出。

在C: 2打印输出时,我只需要一个输出。
EN

Stack Overflow用户
提问于 2015-11-13 23:58:43
回答 2查看 66关注 0票数 2

这个程序应该通过询问原始金额来平衡一个帐户,然后询问他们想要什么样的交易。当我只需要一个调用时,printf调用会被调用两次。输出:“输入事务:输入事务:”。否则效果很好。代码:

代码语言:javascript
运行
复制
#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;

}

会非常感谢您的任何见解!谢谢你!!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-11-14 00:11:17

scanf(" %c", &letter);而不是scanf("%c", &letter);

原始(没有空格)将将Enter解释为输入。

票数 2
EN

Stack Overflow用户

发布于 2015-11-14 00:20:49

您还可以通过从stdin输入缓冲区清除输入(先前的输入按下)来解决这个问题。添加这一行

代码语言:javascript
运行
复制
fflush(stdin);

在此之前

代码语言:javascript
运行
复制
scanf("%c", &letter);

但是,建议使用@artm提供的解决方案,但根据平台的不同,我的解决方案可能无法工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33703591

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档