R
Red Dragon
I am self study C student. I got stuck in the program below on quadratic equation and will be most grateful if someone could help me to unravel the mystery.
Why does the computer refuse to execute my scanf ("%c",&q);
On input 3 4 1 (for a,b and c) I had real roots OK
On input 1 8 16 I had same real roots OK.
However on 4 2 5, (for imaginary roots ) the computer cannot see the scanf ("%c",&q); statement. It just jumps over it.
How can I make the computer not to ignore this statement? I am on Visual C++ platform.
Thanks
Khoon.
/* Roots of a Quadratic Equation.
12.10.05 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void)
{
int a; int b; int c; float x1; float x2; int E; int E1; float R; float I;float S;
char p; char q; char y;
printf ("Please key in the value of constant a,b and c for finding the roots of quadratic");
printf ("equation ax%c+bx+c=0 :",253);
scanf ("%d%d%d", &a,&b,&c);
E =(b*b)-(4*a*c);
if ( E > 0)
{
x1 = (float)(-b+sqrt(E))/(2*a);
x2 = (float)(-b-sqrt(E))/(2*a);
printf ("\nYour quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x2);
}
else if (E == 0)
{
x1 = (float)(-b+sqrt(E))/(2*a);
printf ("\nYour quadratic equation has two same: x1=x2=%1.6f\n",x1);
}
else
{
p = 'y';
printf ("Your quadratic equation has two distinct imaginary roots. Do you want to know\n");
printf ("the values of the imaginary roots (Y/N)?");
scanf ("%c",&q);
printf ("\nq = %c\n",q);/* Test statement*/
if (p==q)
printf ("OK I will show your the imaginary roots tomorrow.\n");
else
printf ("Good bye\n");
return 0;
}
}
Why does the computer refuse to execute my scanf ("%c",&q);
On input 3 4 1 (for a,b and c) I had real roots OK
On input 1 8 16 I had same real roots OK.
However on 4 2 5, (for imaginary roots ) the computer cannot see the scanf ("%c",&q); statement. It just jumps over it.
How can I make the computer not to ignore this statement? I am on Visual C++ platform.
Thanks
Khoon.
/* Roots of a Quadratic Equation.
12.10.05 */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (void)
{
int a; int b; int c; float x1; float x2; int E; int E1; float R; float I;float S;
char p; char q; char y;
printf ("Please key in the value of constant a,b and c for finding the roots of quadratic");
printf ("equation ax%c+bx+c=0 :",253);
scanf ("%d%d%d", &a,&b,&c);
E =(b*b)-(4*a*c);
if ( E > 0)
{
x1 = (float)(-b+sqrt(E))/(2*a);
x2 = (float)(-b-sqrt(E))/(2*a);
printf ("\nYour quadratic equation has two distinct real roots: x1=%1.6f ,x2=%1.6f",x1,x2);
}
else if (E == 0)
{
x1 = (float)(-b+sqrt(E))/(2*a);
printf ("\nYour quadratic equation has two same: x1=x2=%1.6f\n",x1);
}
else
{
p = 'y';
printf ("Your quadratic equation has two distinct imaginary roots. Do you want to know\n");
printf ("the values of the imaginary roots (Y/N)?");
scanf ("%c",&q);
printf ("\nq = %c\n",q);/* Test statement*/
if (p==q)
printf ("OK I will show your the imaginary roots tomorrow.\n");
else
printf ("Good bye\n");
return 0;
}
}