I been working on this problem for so long, when I compile, it give me an error message that I misplace else at "else if (a==0 && b==0 && c!=0)"

when I remove the else at that line, it mess up my code, it won't display like it suppose to , but it display "floating point error" , that mean it doesn't divide by zero, but I need to override dividing by zero in order to display the messages that I want it to display in my code.

please help.

#include <stdio.h>
#include <math.h>

int main()
{

  float a;
  float b;
  float c;
  float d;
  float e;
  float f;
  float x1;
  float x2;
  int opselect;
  float sqr;
  char more = 'y';

  while (more == 'y' || more == 'Y')
   {
    clrscr();

    printf ("\n\n\t\t\tSolving Quadratic Equation");
    printf ("\n\n\t\t\tPlease enter a number for A: ");
    scanf  ("%f", &a);
    printf ("\n\n\t\t\tPlease enter a numner for B: ");
    scanf  ("%f", &b);
    printf ("\n\n\t\t\tPlease enter a number for C: ");
    scanf  ("%f", &c);


    if (a==0 && b==0 && c==0) {
    opselect = 1;
    else if (a==0 && b==0 && c!=0)
    opselect = 2;
    else if (a==0 && b!=0 && c!=0)
    opselect = 3;
    else if (sqr < 0)
    opselect = 4;
    else if (sqr == 0)
    opselect = 5;
    else
    opselect = 6;
    }

    switch (opselect)
    {
      case 1:
	printf ("\n\n\t\t\tInfinite Solution!!");
	break;
      case 2:
	printf ("\n\n\t\t\tContradict Solution!!");
	break;
      case 3:
	x1 = -b/a;
	printf ("\n\n\t\t\tSingle Root\n\t\t\tx= %4.2f", x1);
	break;
      case 4:
	d = (sqrt(b*b) - (4.0*a*c));
	e = -b/(2*a);
	f = d/(2*a);
	printf ("\n\n\t\t\t\tTwo complex root\n\n\t\t\t x1 = %4.2f + %4.2fi", e, f);
	printf ("\n\n\t\t\t x2 = %4.2f - %4.2fi", e, f);
	break;
      case 5:
	x1 = (-b + (sqrt(b*b) - (4.0*a*c))) / (2.0 * a);
	printf ("\n\n\t\t\tRepreated Root\n\t\t\tx = %4.2f", x1);
	break;
      case 6:
	x1 = (-b + (sqrt(b*b) - (4.0*a*c))) / (2.0 * a);
	x2 = (-b - (sqrt(b*b) - (4.0*a*c))) / (2.0 * a);
	printf ("\n\n\t\t\t\tTwo real root\n\n\t\t\t x1 = %4.2f\t\tx2 = %4.2f", x1, x2);
	break;
    }
    printf ("\n\n\t\t\tDo more (Y/N) ? ");
    scanf  ("%s", &more);
    }
  return 0;
}

This part is wrong...

if (a==0 && b==0 && c==0) {
    opselect = 1;
    else if (a==0 && b==0 && c!=0)
    opselect = 2;
    else if (a==0 && b!=0 && c!=0)
    opselect = 3;
    else if (sqr < 0)
    opselect = 4;
    else if (sqr == 0)
    opselect = 5;
    else
    opselect = 6;
    }

The curly brace is the issue...

if (a == 0 && b == 0 && c == 0)
	opselect = 1;
else if (a == 0 && b == 0 && c != 0)
	opselect = 2;
else if (a == 0 && b != 0 && c != 0)
	opselect = 3;
else if (sqr < 0)
	opselect = 4;
else if (sqr == 0)
	opselect = 5;
else
	opselect = 6;

Keep it simpler, thuyh83 ;):

if (a != 0) {
    double d = b*b - 4.0*a*c;
    if (d > 0) {
        /* Two real roots */
    } else if (d < 0) {
        /* Two complex roots */
    } else {
        /* Two equal roots */
    }
} else if (b != 0) {
    /* Single root */
} else if (c != 0) {
    /* No roots */
} else {
    /* Any x */
}

The last free advice: avoid exclamations and other decorations (\n\n\t\t...) in a user interface.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.