我希望我的程序有一个是,没有提示询问是否应该再次运行该程序。现在,它完全跳过了用户输入。我怎样才能有效地做到这一点?
任何帮助将不胜感激。谢谢!
#include <stdio.h>
#include <math.h>
double Months(double principal,double annualInterestRate,double monthlyPayment)
{
double x = ((log(monthlyPayment)-log(monthlyPayment-annualInterestRate/1200.0*principal))/log(annualInterestRate/1200.0+1.0));
return x;
}
int calculateMonths(double y)
{
double principal, annualInterestRate, monthlyPayment;
y = Months(principal, annualInterestRate, monthlyPayment);
return y;
}
int main()
{
double principal, annualInterestRate, monthlyPayment;
char response = 'Y';
printf("** Welcome to the CPSC 1010-S3 Payment Calculator **\n\n");
do{
printf("Enter the principal amount:\t\t\t");
scanf("%lf", &principal);
printf("Enter the annual interest rate (in%%):\t\t");
scanf("%lf", &annualInterestRate);
printf("Enter the monthly payment:\t\t\t");
scanf("%lf", &monthlyPayment);
printf("\nCalculating...\n\n");
int roundedMonths = Months(principal, annualInterestRate, monthlyPayment)+.5;
printf("Total # of months needed to pay off:\t\t %d\n", roundedMonths);
double years = Months(principal, annualInterestRate, monthlyPayment)/12;
printf("Approcimate # of years needed to pay off: \t %.1f\n", years);
double amountPaid = roundedMonths * monthlyPayment;
double interestPaid = amountPaid-principal;
printf("Total interest paid:\t\t\t\t $%.2f\n", interestPaid);
printf("Total amount paid:\t\t\t\t $%.2f\n", amountPaid);
double overpay = amountPaid - Months(principal, annualInterestRate, monthlyPayment)*monthlyPayment;
printf("You overpaid:\t\t\t\t $%.2f\n", overpay);
int extraMonthlyPayment = monthlyPayment*.1;
int monthsEarly = ((log((monthlyPayment*1.1))-log((monthlyPayment*1.1)-annualInterestRate/1200.0*principal))/log(annualInterestRate/1200.0+1.0));
int newMonths = Months(principal, annualInterestRate, monthlyPayment)-monthsEarly;
printf("Did you know if you paid an additional $%d per month, you would pay off your loan %d months earlier?\n\n", extraMonthlyPayment, newMonths);
printf("Do you wish to calculate another payoff? (Y/N):\t");
scanf("%c", &response);
}
while(response == 'Y' || response == 'y');
printf("Thank you for using the Payment Calculator!\n\n");
return(0);
}
答案 0 :(得分:0)
使用scanf不是最理想的。
我添加
while (getchar());
在最后一次扫描之前的行。
由于输入缓冲区中的换行符,请确保您不会错过或破坏其他数据。