2015-10-06 43 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()); 

線。

請確保您不會因輸入緩衝區中的換行符而丟失或損壞其他數據。

相關問題