Function is not calculating my coefficients

Why isn't my my function getRoots not calculating my inputs?

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
56
57
58
59
#include <iostream>
#include <cmath>

using namespace std;

int getRoots(double a, double b, double c, double &s1, double &s2)
{
	double disc = (b*b) - (4 * a*c);

		if (disc < 0)
		{
			return 0;
		}
		
		else if (disc == 0)
		{
			double s1;

				s1 = (-b - sqrt((b*b) - (4 * a*c))) / (2 * a);

					return 1;
		}

		else if (disc > 0)
		{
	
			double s1, s2;

				s1 = (-b + sqrt((b*b) - (4 * a*c))) / (2 * a);

				s2 = (-b - sqrt((b*b) - (4 * a*c))) / (2 * a);

					return 2;
		}
}

int main()
{
	double a, b, c, s1, s2;
		cout << "Enter three coefficients: " << endl;
		cin >> a >> b >> c;

	int numSolutions = getRoots(a, b, c, s1, s2);

	switch (numSolutions)
	{
	case 0: cout << "No real solutions. ";
		cout << endl;
		break;

	case 1: cout << "The solution is " << s1 << ",";
		cout << endl;
		break;

	case 2: cout << "The two solutions are " << s1 << " and " << s2 << ".";
		cout << endl;
	}
	return 0;
}
Line 19, 29, 31:
No need to calculate the discriminant again when you already have it.

Line 17, 19:
This will store the value of the root into a local variable, which will be automatically destroyed when it goes out of scope (at line 22).
I'm sure you were meant to store the value into s1 and s2 which you passed by reference. You can do this by removing your local declarations of s1 and s2.
This should fix your issue.
Thank you. This helped a lot.
Topic archived. No new replies allowed.