扫描仪不工作(C代码)

时间:2014-05-17 19:36:21

标签: c

这是我的代码:

#include <stdio.h>

float calculateBalance(float payment, float balance, float rate);

int main(void){
    float loanAmount, interestRate, monthlyPayment;

    printf("Enter amount of loan:");
    scanf("%.2f", &loanAmount);

    printf("\nEnter interest rate:");
    scanf("%.1f", &interestRate);

    printf("\nEnter monthly payment:");
    scanf("%.2f", &monthlyPayment);

    loanAmount = calculateBalance(monthlyPayment, loanAmount, interestRate);
    printf("Balance remaining after first payment: %.2f\n", loanAmount);

    loanAmount = calculateBalance(monthlyPayment, loanAmount, interestRate);
    printf("Balance remaining after second payment: %.2f\n", loanAmount);

    loanAmount = calculateBalance(monthlyPayment, loanAmount, interestRate);
    printf("Balance remaining after third payment: %.2f\n", loanAmount);
}

float calculateBalance(float payment, float balance, float rate){
    return (balance-payment + balance*rate);
}

我试图在三次付款后计算余额;但是,当我尝试在终端中运行代码时,它不会让我输入任何内容,而只是读取随机数,然后在三次付款后计算余额(我希望能够输入内容,但它不是允许我)。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:2)

问题出在fmt字符串本身。作为@ T.C。在评论中说

尝试使用

scanf("%f", &var);

代替。如果只有2个dec值,那么它只会读取它。

答案 1 :(得分:-1)

您可以尝试在每次扫描后添加getc(stdin);