How do I get my main function to switch from case 0 - 2 with my int function?

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
#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 s1;	  
		}	 	 	 
		else if (disc == 0)
		{
			double s1;

				s1 = (-b - sqrt((b*b) - (4*a*c))) / (2*a);
	
	 				return s1;
		}
		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 s1, s2;
	}
}
	
int main() 
{
		double a, b, c, s1, s2;
			cout << "Enter three coefficients: ";
				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;
}
Last edited on
Hi,

line 30, you can't return 2 variables like that. You already have them assigned to their references. Consider returning a 0 1 or 2 instead.

Line 12 and 20 provide confusion by returning a double when an int (0,1,2) is expected.

Re-declaring s1 and s2 is not a plan either.

Edit: neither is sending un-initialised variables to a function.

Good Luck !!
Last edited on
Thank you so much!
Last edited on
Topic archived. No new replies allowed.