I am trying to code a program to solve quadratic equations. The problem is that after entering the input data, the program just seems to terminate and I can't figure out why. The code is:
Code:
#include <stdio.h>
#include <math.h>
int main(void)
{
/* declare and initialise variables */
char again = '\0';
int control = 0;
double a = 0.0,
b = 0.0,
c = 0.0,
radicand = 0.0,
root = 0.0,
x1 = 0.0,
x2 = 0.0,
real = 0.0,
imag = 0.0;
/* Make sure the quadratic coefficient is non-zero */
while (control == 0)
{
printf("Enter a value for \"a\": \n");
scanf("%d", &a);
printf("Enter a value for \"b\": \n");
scanf("%d", &b);
printf("Enter a value for \"c\": \n");
scanf("%d", &c);
radicand = pow(b, 2) - 4 * a * c;
if (radicand >= 0)
{
x1 = (-b + sqrt(radicand)) / (2 * a);
x2 = (-b - sqrt(radicand)) / (2 * a);
printf("x1 = %6.2lf\n", x1);
printf("x2 = %6.2lf\n", x2);
}
else
{
radicand = -radicand;
real = -b / (2 * a);
imag = sqrt(radicand) / (2 * a);
printf("x1 = %6.2lf + %6.2lf i\n", real, imag);
printf("x1 = %6.2lf - %6.2lf i\n", real, imag);
}
printf("\n\nAgain? (y/n) : ");
scanf("%c", &again);
if (again == 'y')
{
printf("\nExcellent!\n");
control = 0;
}
else
{
printf("\nThank you for using quadratic.\n");
control = -1;
}
}
return (0);
}
Does anyone know what I am doing wrong? I also tried setting the code up to use a multiple-return function but couldn't get that to work. Does anyone know of any tutorials on how to use them?
Thanks.