Why doesnt my quadratic formula program work properly?

I am trying to create a program that calculates the quadratic formula just for practice and fun but it has been years since i have actually used it in math so im not sure if its my math that is wrong or my programming but can someone please help?
code:

#include <iostream>
#include <math.h>
using namespace std;

int main()
{

cout << "Enter in A's value: ";
double a;
cin >> a;
cout << "Enter in B's value: ";
double b;
cin >> b;
cout << "Enter in C's value: ";
double c;
cin >> c;
cout << "X is equal to: ";
double x;
x = ((-b)+ sqrt((b*b)-4*(a*c)))/(2*a);
//x = ((-b)- sqrt((b*b)-4*(a*c)))/(2*a);
cout << x;
cout << endl;
cout << "Enter any number to terminate the program";
cin >> a;
}

thanks in advance
What's The problem?
it doesnt calculate the answer properly, at least i dont think so, i tested it with an example from google and it worked but then i got my brother in highschool who could use this program frequently to try it and it calculated the answer wrong, at least i think it did...
To solve a problem, first the problem must be identified. If you don't know whether or not there's a problem, the first step would be finding out if there actually is a problem.

If there is a problem, the next step is reproducing it.

So... try a bunch of different values out. If you get incorrect output... then tell us the following:

1) What input you gave it (values for a, b, and c)
2) What output the program gave you
3) What output is actually correct

If you try a bunch of values out and do not get incorrect output... then you have no reason to think that there's anything wrong with the program.


EDIT:
If your brother is telling you he got incorrect output. Then get the above information from him.
Last edited on
closed account (18hRX9L8)
*Ignore this I am reminding myself that when I get home from work I should check this out and give you some code that I have to help you.*

EDIT: okay... here is some code (its in C, but if you can convert it into C++ that would be nice!)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* quad.c
 * ----------
 * Quadratic Equation Problem Phase #4.
 */
 
#include <stdio.h>                                                              //include
#include "genlib.h"
#include "simpio.h"
#include "math.h"

main()
{
      float answer1,answer2,a,b,c,bsq,fac,ta,nob,lin;                               //assign variables
      
      printf ("Quadratic Equation Problem Phase #4.\n");                        //Get Numbers
      printf ("Enter \"a\" value.  ");
      a=GetInteger();
      printf ("Enter \"b\" value.  ");
      b=GetInteger();
      printf ("Enter \"c\" value.  ");
      c=GetInteger();

      bsq=b*b;                                                                  //initialization
      fac=4*a*c;
      ta=2*a;
      nob=-1*b;
      lin=-1*(c/b);
      
      if (bsq<fac)                                                    //check for imaginaries
          printf ("Imaginary Solution(s)");
      else if (a==0) {
          if (b==0){
             if (c==0){
                  printf ("All Real Numbers.");}
             else {
             printf ("Undifined Solution(s)");  }}
          else {                                                    //check for undefined solutions
          printf ("The linear solution is: %.2f.\n",lin);}}
      else if (bsq==fac)                                                        //check for one solution
      {
          answer1=(nob-(sqrt(bsq-fac)))/ta;
          printf ("Only one solution: %.2f\n",answer1*-1);
      } 
      else                                                                      //otherwise continue!
      {
          answer1=(nob-(sqrt(bsq-fac)))/ta;
          answer2=(nob+(sqrt(bsq-fac)))/ta;
          printf ("1st Solution = %.3f\n",answer1*-1);
          printf ("2nd Solution = %.3f\n",answer2*-1);
      }
      
getchar();                                                                      //safely exit
}

                                                                                //party! 


if you need me to explain anything, just ask
Last edited on
Topic archived. No new replies allowed.