繁体   English   中英

为什么这个代码在运行时会崩溃?

[英]Why does this code crash when run?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM