C贷款计算器返回全部0.00

时间:2016-04-21 02:01:18

标签: c

所有东西都以0.00的形式回归,有人能看出为什么会这么做吗?

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <math.h>
#define MONTHS 12

int main (void)
{
    int i;
    double p, r, y, rate, months;
    double old_balance, interest, principal, payment, new_balance;
    long real_payment;


    printf("This program calculates your bank loan repayment plan\n");
    printf("Please enter the principal (q to quit)\n");
    while (scanf("%f", &p) ==1)
    {
        if (p <= 0.00)
            printf("Invalid Entry\n");
        break;
    }

    printf("please enter the annual interest rate (q to quit)\n");
    while (scanf("%f", &r) ==1)
    {
        if (r > 0.00)   
            rate = r/100.00;
        else
            printf("Invalid Entry\n");
        break;
    }

    printf("Please enter the duration of the loan in years (q to quit)\n");
    while (scanf("%f", &y) ==1)
    {
        if (y > 0.00)   
            months = y*12.00;
        else
            printf("Invalid Entry\n");
        break;
    }

    payment = (p*rate)/(1.00-(pow((1.00+rate),-months)));
    old_balance = p;
    interest = old_balance*months;
    principal = payment-interest;
    new_balance = old_balance-principal;
    real_payment = ((payment*100.00) + 0.5)/100.0;

    printf("%f  %f\n", rate, months);
    printf("Month             Old Balance              Payment                  Interest                 Principal            New Balance\n");
    for (i = 0; i < MONTHS; i++)
    {
        printf( "%d   %20.2f     %20.2f     %20.2f     %20.2f %20.2f\n", i+1, old_balance, real_payment, interest, principal, new_balance);
    }

    return 0;
}

请随时提出任何问题,C新手,只是学习。提前感谢您的帮助。 :)

1 个答案:

答案 0 :(得分:2)

users = User.order(:created_at).take(3) 50.times do users.each { |user| client_name = Faker::Lorem.characters(10) contact_name = Faker::Lorem.characters(10) contact_email = Faker::Lorem.characters(10) contact_phone = Faker::Lorem.characters(10) client_address = Faker::Lorem.characters(10) user.clients.create!(client_name: client_name, contact_name: contact_name, contact_email: contact_email, contact_phone: contact_phone, client_address: client_address) } end 更改为scanf("%f",...)可以解决我的问题。表中的数据值不再输出为零。由于scanf("%lf",...)函数在代码中查找浮点数,因此它无法正确读取用户的数据,因此为零。