Hello,

here is my code, I have problem with Max operator

For instance, 13 M 47
would be 47? how can I do that !

I don't know how I can use it
don't tell me as function, is there other way !

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````int main () { cout <<" Fixed-point expression (the available 7 operations are):" <>num1 >>op >>num2; if (!cin) die("Input failure"); switch (op) { case '+': ans = num1 + num2; break; case '-': if (num2 < 0) die("The ans cannot be nagative"); ans = num1 - num2; break; case '*': ans = num1 * num2; break; case '/': if (num2 == 0) die("You cannot divide by zero"); // ok = false; ans = num1 / num2; break; case '%': if (num2 == 0) die("You cannot use the operation"); ans = num1 % num2; break; case '<': ans = num1 < num2; break; case 'Max': if (num1 > num2) cout << num1 << endl; else cout << num2 <
Last edited on
` if (num2 < 0) die("The ans cannot be nagative");` this would only be true if num2 > num1.

As far as your problem 1) 'Max' would be a string not a character. So you will have to find a new name for it maybe 'M.' As for fixing this simply replace the output (cout) statements with `ans = variable;`
Last edited on
 ``4445464748`` ``````case 'M': if (num1 > num2) cout << num1 << endl; else cout << num2 <
Still didn't fix the problem I don't get errors but also cannot get the right answer

 ``12345`` ``````case 'M': if (num1 > num2) ans = num1; else ans = num2;``````
If you do it that way or the way smac did you will have to create a special case where you don't output this: `cout << num1 << " " << op << " " << num2 << " = " << ans <<endl;` if that is the problem you are referring to. A simple if statement should do the trick.
You forgot a break statement:

 ``1234`` ``````case 'M': case 'm': ans = num1 > num2 ? num1 : num2; break;``````
thank you guys, it worked

 ``123456`` ``````case 'M': if (num1 > num2) ans = num1; else ans = num2; break;``````
Topic archived. No new replies allowed.