2016-12-05 86 views
1

C新手和將要學習它,所以我可以開發強大的基礎知識,所以我決定在這裏註冊。感謝您的高級幫助。我的代碼有什麼問題 - 它在最後的printf()行上崩潰了?在管線爲什麼此代碼在運行時崩潰?

#include <stdio.h> 

main(int argc, char *argv[]) 
{ 
    // car loan calculator 
    int carPrice, carDownPayment, loanTerm; 
    float loanInterestRate, salesTax; 

    printf("What is the price of the car? "); 
    scanf("%d", &carPrice); 

    printf("How much down payment do you have? "); 
    scanf("%d", &carDownPayment); 

    printf("What is your loan's interest rate? "); 
    scanf("%f", &loanInterestRate); 

    printf("What is your sales tax? "); 
    scanf("%f", &salesTax); 

    printf("What is your loan term? "); 
    scanf("%d", loanTerm); 

    printf("The price of the car is %d. Your down payment is %d. Your loan interest rate is %1f. Sales tax is %2f. Your loan term is %d.", carPrice, carDownPayment, loanInterestRate, salesTax, loanTerm); 

    float monthlyPayment; 

    printf("Your monthly should be about %3.2f dollars over a term of %d months."), (carPrice * salesTax * loanInterestRate - carDownPayment/loanTerm), loanTerm; 

    return 0; 
} 
+0

什麼是你的代碼錯誤? – Danh

+0

它嘗試打印最終計算的行崩潰。 –

+0

'的printf( 「你每月應約3.2F%美元,比的%d個月的監禁。」(carPrice *銷售稅* loanInterestRate - carDownPayment/loanTerm),loanTerm);' – Danh

回答

3
  1. Scanf語法錯誤scanf("%d", loanTerm);
  2. printf的語法錯誤在

    printf("Your monthly should be about %3.2f dollars over a term of %d months."), (carPrice * salesTax * loanInterestRate - carDownPayment/loanTerm), loanTerm;

    牙套失配

`

#include <stdio.h> 

    int main(int argc, char *argv[]) 
    { 
    // car loan calculator 
    int carPrice, carDownPayment, loanTerm; 
    float loanInterestRate, salesTax; 
    float monthlyPayment; 

    printf("What is the price of the car? "); 
    scanf("%d", &carPrice); 

    printf("How much down payment do you have? "); 
    scanf("%d", &carDownPayment); 

    printf("What is your loan's interest rate? "); 
    scanf("%f", &loanInterestRate); 

    printf("What is your sales tax? "); 
    scanf("%f", &salesTax); 

    printf("What is your loan term? "); 
    scanf("%d", &loanTerm); 
    if(loanterm<=0){ 
    printf("Enter valid number \n");//can specify the range you want 
    return 0; 
    } 

    printf("The price of the car is %d.\nYour down payment is %d.\nYour loan interest rate is %1f.\nSales tax is %2f.\nYour loan term is %d.\n\n\n", carPrice, carDownPayment, loanInterestRate, salesTax, loanTerm); 



    printf("Your monthly should be about %3.2f dollars over a term of %d months.", ((carPrice + (carPrice * (salesTax/100)) + (carPrice * (loanInterestRate/100)) - carDownPayment)/loanTerm), loanTerm); 

    return 0; 
}` 
+0

昨晚一定很困。一直試圖學習如何編碼。試圖找出現在的下一步(銷售稅和貸款利率)(大致)如果(整數),然後通過移動小數點將它們轉換成%。 –

+0

還修復了最後一次打印。的printf( 「你每月應約3.2F%美元,比的%d個月的監禁。」((carPrice +(carPrice *銷售稅)+(carPrice * loanInterestRate) - carDownPayment)/ loanTerm),loanTerm); –

+0

@CharlesMullen恭喜你自己找到它。 –

0
#include <stdio.h> 

main(int argc, char *argv[]) 
{ 
// car loan calculator 
int carPrice, carDownPayment, loanTerm; 
float loanInterestRate, salesTax; 

printf("What is the price of the car? "); 
scanf("%d", &carPrice); 

printf("How much down payment do you have? "); 
scanf("%d", &carDownPayment); 

printf("What is your loan's interest rate? "); 
scanf("%f", &loanInterestRate); 

printf("What is your sales tax? "); 
scanf("%f", &salesTax); 

printf("What is your loan term? "); 
scanf("%d", &loanTerm); 

printf("The price of the car is %d.\nYour down payment is %d.\nYour loan interest rate is %1f.\nSales tax is %2f.\nYour loan term is %d.\n\n\n", carPrice, carDownPayment, loanInterestRate, salesTax, loanTerm); 

float monthlyPayment; 

printf("Your monthly should be about %3.2f dollars over a term of %d months.", ((carPrice + (carPrice * (salesTax/100)) + (carPrice * (loanInterestRate/100)) - carDownPayment)/loanTerm), loanTerm); 

return 0; 
} 
+1

您應至少記錄所做的更改。你也應該考慮接受其他答案。 –

相關問題