gpt4 book ai didi

c - 是/否提示重新运行程序

转载 作者:行者123 更新时间:2023-11-30 17:10:12 27 4
gpt4 key购买 nike

我希望我的程序有一个是或否提示来询问程序是否应该再次运行。现在,它完全跳过用户输入。我怎样才能有效地做到这一点?

如有任何帮助,我们将不胜感激。谢谢!

#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);
}

最佳答案

使用 scanf 不是最佳选择。

我想添加一个

while (getchar());

最后一个 scanf 之前的行。

请确保您不会因输入缓冲区中的换行符而丢失或损坏其他数据。

关于c - 是/否提示重新运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32980767/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com