0

am getting this error warning: left operand of comma operator has no effect after running following code

if ( marks1 > ,marks2 , marks4 , marks5 , marks6 ) { cout << "The highest is" << marks1; return marks1; } else if (marks2 > marks1, marks3, marks4, marks5, marks6) { cout << "The highes is " << marks2; return marks2; } else if (marks3 > marks1, marks2, marks4, marks5, marks6) { cout << "The highest is " << marks3; return marks3; } else if (marks4 > marks1, marks2, marks3, marks5, marks6) { cout << "The highest is " << marks4; return marks4; } else if (marks5 > marks1, marks2, marks3, marks4, marks6) { cout << "The highest is" << marks5; return marks5; } else if (marks6 > marks1, marks2, marks3, marks4, marks5) { cout << "The highest is " << marks6; return marks6; }

22nd Mar 2020, 9:40 PM
kuda kapita
kuda kapita - avatar
3 Respuestas
+ 4
Please make this code in Sololearn Playground and share your code here so we can check.
22nd Mar 2020, 9:47 PM
A͢J
A͢J - avatar
+ 2
The comma operator has a different meaning in C++ and can not be used to apply a function (comparison in this case) to multiple elements. If you want to chain multiple comparisons together, you have to use logical operators: https://www.sololearn.com/learn/CPlusPlus/1619/ However, this approach quickly becomes very tedious as the number of marks increases. A much simpler approach would be to store the different marks in an array, that way you could easily loop over the array to find the highest mark.
22nd Mar 2020, 10:10 PM
Shadow
Shadow - avatar
+ 1
Check out this link..although it relented to 'C' programming....it still applicable to C++:- https://www.youtube.com/watch?v=mhmnb80ZDBM&list=PLBlnK6fEyqRhX6r2uhhlubuF5QextdCSM&index=34&t=0s
22nd Mar 2020, 10:18 PM
rodwynnejones
rodwynnejones - avatar