need advice on switch case conditions

Hello all,
need advice pls.

I need to check 3 conditions. fc, sc and so.

I tried using both switch and while loop to check but it only works for the first condition. For example, if it's the second condition "else if (sc == xValue || so == dsc)", my vector sort will not work.

I have tested individual sorting for vectors, they works just fine but not when using in this switch case or even while loop.

my code:

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
switch (fc)
	{
		case filterP2D:
		p2dfilter();
		if (sc == xValue || so == asc) 
		sort (p2dvector.begin(), p2dvector.end(), Point2D::sortPoint2DXAsc);
		
		else if (sc == xValue || so == dsc) 
		sort (p2dvector.begin(), p2dvector.end(), Point2D::sortPoint2DXDsc);

		else if (sc == yValue || so == asc) 
		sort (p2dvector.begin(), p2dvector.end(), Point2D::sortPoint2DYAsc);
		
		else if (sc == yValue || so == dsc) 
		sort (p2dvector.begin(), p2dvector.end(), Point2D::sortPoint2DYDsc);
		
		else if (sc == length || so == asc) 
		sort (p2dvector.begin(), p2dvector.end(), Point2D::distFrOriginAsc); 

		else if (sc == length || so == dsc) 
		sort (p2dvector.begin(), p2dvector.end(), Point2D::distFrOriginDsc); 

		cout << "\n" << p2dheader << "\n";
		for (int i=0; i<p2dvector.size(); i++)
		{
			cout << left << "[" 
				<< right << setw(4) << p2dvector[i].getX() 
				<< left << "," 
				<< right << setw(4) << p2dvector[i].getY() 
				<< left << "]" << setw(4) << " " 
				<< setprecision(3) << p2dvector[i].getScalarValue() << endl;
		}
                  break;

}



Anything wrong with my code? No error. I can compile successfully.
Thanks!
Last edited on
Only one condition is being executed for fc:
case filterP2D:
are there other values for fc?
Last edited on
So, you've got:

if (sc == xValue || so == asc)

and

else if (sc == xValue || so == dsc)

etc.

You need to take a second look at that construct. As long as "sc == xValue" then the "else" clause will never be executed. Perhaps you intended to use "&&" instead of "||"? I can't tell without seeing the rest of the code.

Also, the caffeine is wearing off, so I'm sorry if I just typed a whole load of nonsense up there...
Hey both.. just an update. I used the following method instead and it's working. :)

1
2
3
4
5
6
7
8
9
10
while (fc == aaa)
{
     switch (sc == bbb)
     { 
          if (so == asc)
              // do this...
          else
              // do this...
      }
}



Last edited on
Topic archived. No new replies allowed.