2015-10-06 62 views
0

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

任何帮助,将不胜感激。谢谢!

#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

请尽量成为[简短,独立,正确(可编译),示例](http://sscce.org)! –

+0

您是否尝试过打印'response'来确定发生了什么? – John3136

+0

打印'response'不会产生任何结果 –

回答

0

使用scanf是次优的。

我最后scanf函数前添加

while (getchar()); 

线。

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

相关问题