A+B using C++ language

1

I'm golfing a program to sum two numbers in C++.

Example:

10 3
13

I found out that this problem can be solved in 54 symbols (without spaces and so on) using C++. My code (63 symbols):

#include <iostream>
main() {
    int a, b;
    std::cin >> a >> b;
    std::cout << a + b;
}

I have no idea how to make my program shorter. Can you help me, please?

eaglemango

Posted 2017-06-05T19:07:15.260

Reputation: 29

Do the numbers have to come from STDIN? – James – 2017-06-05T19:09:43.470

DJMcMayhem, yes. – eaglemango – 2017-06-05T19:11:13.037

I don't think you can beat that if you have to use STDIN. – Magic Octopus Urn – 2017-06-05T19:18:28.693

I saw the 54-symbols result on site, where you can read data only from STDIN or file. – eaglemango – 2017-06-05T19:20:21.327

Could you link to where you saw a 54 byte solution? If you have seen a 54 byte solution why can't you use that? – Notts90 supports Monica – 2017-06-05T20:04:15.990

I haven't seen the solution code. I saw only statistics like run time, used memory and length (54 symbols without spaces and etc.) – eaglemango – 2017-06-05T20:59:02.453

3Your program can not run according to c++ standard. the function main should have a return type. – rahnema1 – 2017-06-06T02:25:23.223

I'm voting to close this question as off-topic because it's not a programming puzzle or code golf challenge. It's not a general programming question either, though. – CalculatorFeline – 2017-06-06T02:53:34.833

@rahnema1 yes, main function have to be declared with a return type. Some compilers support main() { ... } declaration. I used that in order to make my code shorter. – eaglemango – 2017-06-06T07:35:03.627

@CalculatorFeline why do you think that's not a challenge? A goal is to write working A+B program, that will be shorter than mine. – eaglemango – 2017-06-06T07:39:26.937

9@CalculatorFeline This is a perfectly fine [tag:tips] question, no reason to close it. – TheLethalCoder – 2017-06-06T09:59:05.317

Answers

5

48 bytes

How about...

#include<cstdlib>
main(){system("tr \\  +|bc");}

...not using C++?


  • The call to system runs commands on your shell (<cstdlib> declares system).
  • tr \\ + executes the command tr, translating spaces to + (so the input34 45 becomes 34+45).
  • | pipes tr's STDOUT into bc's STDIN.
  • bc performs equations in STDIN and prints the result to STDOUT.

jimmy23013

Posted 2017-06-05T19:07:15.260

Reputation: 34 042

Can you explain how does it work? I tried to run that program on Linux, but the next error ocured: bc: not found – eaglemango – 2017-06-06T11:02:39.450

4@eaglemango Well, you have to install bc. – jimmy23013 – 2017-06-06T11:06:02.753

Mind if I edit in an explanation and some links? – MD XF – 2017-12-28T19:39:27.370

@MDXF Fine. (15 chars) – jimmy23013 – 2017-12-29T03:53:35.073