Why doesn't this tertiary work?

What happened?
The compiler I'm using, Dev-C++, dumped me into a library file called ios_base.h and I have no idea what that means. I think that I wrote this tertiary corretly, but feel free to correct me if I'm wrong. I'm downloading a second compiler to see if it does the same thing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
(percent <= 59) ?
		  ( { cout << grade << endl; }  ) 
		:
		  (
		    { switch (plus_minus)
			{
			case '2':
				cout << grade << '+' << endl;
				break;
			case '1':
				cout << grade << '-' << endl;
				break;
			default:
				cout << grade << ' ' << endl;
				break;
			}   
		     }
		   ) ;		 
1) Braced group in expressions are illegal. So you cannot make that a ternary operator. And actually it is terrible misuse of it. You should use if/else here.
2) Your error is probably stems from gcc extension which allows returning a value from brace group. In this case it is trying to return copy of stream which is prohibted.
3) Other than steam copying, you would still have mismatching return types for second and third operand of ternary.
That's where I started from but my teacher wanted us to only use switch statements so I thought throwing in some braces into the tertiary would work; thanks for getting back to me! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if (grade == "F")
	cout << grade << endl;
else
	{			
	//print grade
	switch (plus_minus)
		{
		case '2':
			cout << grade << '+' << endl;
			break;
		case '1':
			cout << grade << '-' << endl;
			break;
		default:
			cout << grade << ' ' << endl;
		}
	}
Just to add: if you do not use return value of ternary, you probably do not need ternary here.
Topic archived. No new replies allowed.