Decimals are all zeros?

I've heard that there is an assignment in school that says you have to write a code for the quadratic equation. I decided to try my hand at it. The equation seems to be losing decimals some where in the equation.

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
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;


int main ()
{
    int a,b,c,d,e,f,g,j;
    double h;
    cout << "Enter a";
    cin >> a;
    cout << "Enter b";
    cin >> b;
    cout << "Enter c";
    cin >> c;
    system("PAUSE");
    d = b-2*b;
    e =(b*b)-(4*a*c);
    f = sqrt(e);
    g = f + d;
    h = g/(2*a);
    cout.precision(5);
    cout << fixed << "If you add, x = "  << h <<endl;
    system("PAUSE");
    f = sqrt(e);
    g = f - d;
    h = g/(2*a);
    cout << "If you subtract, x = " << h << endl;
   system("PAUSE"); 
  return 0;  
        
}

When I get the answer, the only true value are the number before the decimal point. After that, they are all zeros.
You're doing integer arithmetic. try changing all your ints to doubles.
Thanks, it worked.
I have a new problem though. It's not showing if the value is negative. I tried making it
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
int main ()
{
    double j,a,b,c,d,e,f,g,h;
    cout << "Enter a";
    cin >> a;
    cout << "Enter b";
    cin >> b;
    cout << "Enter c";
    cin >> c;
    system("PAUSE");
    d = b-2*b;
    e =(b*b)-(4*a*c);
    f = sqrt(e);
    g = f + d;
    h = g/(2*a);
    cout.precision(5);
    if
    (h < 0)
    {cout << "If you add, x = -"  << h <<endl;
     system("PAUSE");
    f = sqrt(e);
    g = f - d;
    h = g/(2*a);
    cout << "If you subtract, x = -" << h << endl;
   system("PAUSE"); 
  return 0; } 
    if
    (h >= 0)
    {cout << fixed << "If you add, x = "  << h <<endl;
    system("PAUSE");
    f = sqrt(e);
    g = f - d;
    h = g/(2*a);
    cout << "If you subtract, x = " << h << endl;
   system("PAUSE"); 
  return 0; } 
        
}

but for some reason, the program itself is only dealing in positives.
Forget the 20 local variables you have. Simplicity is the key
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
#include <iostream>
#include <cmath>

using namespace std;


int main ()
{
	double a, b, c;
	
	double answer = 0;
	
	double discriminant = 0;
	
	cout << "Enter a";
	cin >> a;
	
	cout << "Enter b";
	cin >> b;
	
	cout << "Enter c";
	cin >> c;
    
	// calculate the discriminant
	discriminant = pow(b, 2) - (4 * a * c);

	// d < 0 = no roots, d = 0 = 1 root, d > 0 = 2 roots
	if(discriminant < 0)
	{
		cout << "No real roots, skipping calculation.\n";
	}
	else if(discriminant == 0)
	{
		cout << "1 real root, calculating value...\n";
		answer = (-b) / (2 * a);
		cout << "Answer = " << answer << endl;
	}
	else if(discriminant > 0)
	{
		cout << "2 real roots, calculating values...\n";
		answer = (-b + sqrt( (pow(b, 2) - (4 * a * c))) ) / (2 * a);
		cout << "Answer+ = " << answer << endl;
		
		answer = (-b - sqrt( (pow(b, 2) - (4 * a * c))) ) / (2 * a);
		cout << "Answer- = " << answer << endl;
	}

	system("pause");

	return 0;
        
}
Topic archived. No new replies allowed.