The teacher wanted us to make a simple calculator. But he wants it as short as possible. Can I go shorter than these? Input: 3+5 Output: 3+5=8 is a must.
Shorter by what metric? Number of characters? Number of lines? Number of semi-colons? Number of assembly instructions output? Your code looks fine to me...
It is important to note, shorter does not mean better. It's really case-by-case. But anyway, those weren't rhetoric questions, I actually don't understand.
shorter code has merits, so its a good exercise, but there is a happy medium. Too short can be unreadable... the biggest problem with long code is it has other problems, like unnecessary work or extra complexity that isn't needed, the first being a coder error of sorts and the second being just as unreadable as too short. Most code should look like what you have: easy to follow and easy to work on if it needs changes.
and that gets you down to the question of what is being counted.
that is 4 "lines of code" with 4 lines of variable declarations and the rest is your includes (you can cut the using out and give the code an std to save that line) and main + brackets.
but its still a lot of characters.
@Ganado @jonnin @seeplus @lastchance (Idk how to mention on this site but I hope this works.)
Thanks for your all kind helps. I'm such a beginner. So nowadays it's hard for me to understand what you guys wrote down here.
And it looks like our teacher was not wanting a shorter code. He didn't give any feedback about revisions and we thought it would be a shorter code.
He was like wanting additions like you can't divide with 0 and 0/0 is an undefined thing. Idk really but we'll see tomorrow. And sorry for bothering anyone who came up with this topic.
good luck!
I was trying to say that originally ... too short usually hard to follow/read.
in short .. hah .. it computes all the values and then picks the one the user wanted if any. So it is wasteful on top of being unreadable. You can circle back to it for a laugh when you are farther along.
As for the others..
if(num1 == 0 && num2 ==0)
// 0/0 message or action
else //you don't want both actions, so putting else here prevents that.
if (num2 == 0)
//then divide by zero action (cout something?)
and similar for any other conditions you want to add -- you can get fancy and detect overflow / underflow perhaps, not much else to do on these.