1

New to C and going to be learning it so I can develop strong fundamentals so I decided to sign up here. Thanks for the help in advanced. What's wrong with my code — it crashes on the final printf() line?

#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;
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

2 Answers2

3
  1. Scanf syntax error in line scanf("%d", loanTerm);
  2. Printf syntax error in

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

    Braces mismatch

`

#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;
}`
Dilip Kumar
  • 1,736
  • 11
  • 22
  • Must have been really sleepy last night. Been trying to learn how to code. Trying to figure out the next steps now which is (roughly) if (integer) for sales tax and loan's interest rate then convert them to % by moving decimal points. – Charles Mullen Dec 05 '16 at 04:27
  • Also fixed the last print. printf("Your monthly should be about %3.2f dollars over a term of %d months.", ((carPrice + (carPrice * salesTax) + (carPrice * loanInterestRate) - carDownPayment) / loanTerm), loanTerm); – Charles Mullen Dec 05 '16 at 04:28
  • @CharlesMullen Congrats you found it yourself. – Dilip Kumar Dec 05 '16 at 04:34
  • Watch out for divide-by-zero. – paddy Dec 05 '16 at 04:36
  • A zero check condition for loanterm after reading loanterm using scanf. i.e. `if(loanterm ==0){printf("Enter valid number\n"); return -1;}` – Dilip Kumar Dec 05 '16 at 04:41
  • Thank you. I have not considered this. – Charles Mullen Dec 05 '16 at 04:43
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;
}