# Quick Question

• 10-03-2012
Lavendershoe
Quick Question
This is for a class. I am NOT asking for someone to do this for me. I just can't find out an issue and would appreciate it if anyone could point out its cause.

This is the program.
Code:

```int main(void) { //Variable declaration double a, b, c, discriminant, r1, r2; //Display title prompt printf("This program finds the roots of the quadratic equation.\n"); //Prompt user for coefficients of the quadratic equation printf("Input the a, b, and c coefficients of the quadratic equation\n in this form: (a)x^2 + (b)x + (c) \n"); //Read user values and assign to appropriate variables scanf("%dx^2+%dx+%d", &a, &b, &c); //Find root type discriminant = (b*b-4*a*c); //Find roots using the variables and display if (discriminant>0) { r1 = -b+sqrt(discriminant)/(2*a); r2 = +b+sqrt(discriminant)/(2*a); printf("The two roots are real: "); printf("%d %d\n"); } else if (discriminant==0) { r1 = -b+sqrt(discriminant)/(2*a); r2 = +b+sqrt(discriminant)/(2*a); printf("The two roots are equal: "); printf("%d %d\n"); } else { r1 = -b+sqrt(discriminant)/(2*a); r2 = +b+sqrt(discriminant)/(2*a); printf("The two roots are complex and may not be correct: "); printf("%d %d\n"); } //Termination statements system ("PAUSE"); return 0; }```
My problem is that, no matter the variables, I always get the last else statement as the answer "The roots are complex...." even when I KNOW they're real or equal. Can't see why. Any help?
• 10-03-2012
Lavendershoe
Any guesses? I've only just started learning, so if it's a simple error, don't hold it against me.
• 10-03-2012
oogabooga
You're using the format for integers in scanf. The format for double is lf (that's a lowercase L).
Code:

`scanf("%lfx^2+%lfx+%lf", &a, &b, &c);`
• 10-03-2012
Lavendershoe
I fixed that. It just made the wrong answer longer. Changed everything to float and put %f everywhere.

Any other ideas?
• 10-03-2012
oogabooga
Post your current code.
EDIT:
You are not only using the wrong format in your printf's, you also don't have any variables listed!

So this
Code:

`                printf("%d %d\n");`
should be
Code:

`                printf("%f %f\n", r1, r2);`
• 10-03-2012
Lavendershoe
Also changed discriminant to d. and some other syntax things that doesn't matter.
Current code:
Code:

```int main(void) { //Variable declaration float a, b, c, d, r1, r2; //Display title prompt printf("This program finds the roots of the quadratic equation.\n"); //Prompt user for coefficients of the quadratic equation printf("Input the a, b, and c coefficients of the quadratic equation\n in this form: (a)x^2 + (b)x + (c) \n"); //Read user values and assign to appropriate variables scanf("%fx^2+%fx+%f", &a, &b, &c); //Find root type d = (b*b)-(4*a*c); //Find roots using the variables and display if (d>0) { r1 = (-b+(sqrt(d)))/(2*a); r2 = (b+(sqrt(d)))/(2*a); printf("The two roots are real: "); printf("%f %f\n"); } else if (d==0) { r1 = (-b+(sqrt(d)))/(2*a); r2 = (b+(sqrt(d)))/(2*a); printf("The two roots are equal: "); printf("%f %f\n"); } else { r1 = (-b+(sqrt(d)))/(2*a); r2 = (b+(sqrt(d)))/(2*a); printf("The two roots are complex and may not be correct: "); printf("%f %f\n"); } //Termination statements system ("PAUSE"); return 0; }```
• 10-03-2012
Lavendershoe
Quote:

Originally Posted by oogabooga
Post your current code.
EDIT:
You are not only using the wrong format in your printf's, you also don't have any variables listed!

So this
Code:

`        printf("%d %d\n");`
should be
Code:

`        printf("%f %f\n", r1, r2);`

Oh..... lemme try fixing that.
• 10-03-2012
Lavendershoe
Still wrong.

Code:

`//code deleted`
• 10-03-2012
Lavendershoe
I figured out what I needed.

Thank you, oog. Appreciated.
• 10-04-2012
Salem