Shortest code a+b

Pages: 12
the shortest one that I know is:
1
2
3
4
5
6
#include <fstream>
main(){
int a,b;
std::ifstream ("input.txt")>>a>>b;
std::ofstream ("output.txt")<<a+b;
}

(if comp. allows main without int & return 0;)
yeah, as you see, input and output are files.
(sorry for my english)
and unfortunately I need a C++03 code:(
I need an idea to shortest, not fastest or "rightest"!
just for fun(and competition:)
Last edited on
Why not:
1
2
3
4
5
6
7
#include <iostream>
int main() {
   int a, b;
   std::cin >> a >> b;
   std::cout << a + b;
   return 0;
}

It's faster and compliant with the standard. I don't think any compilers allow main to not have a data type. None that I've used in the last 10 years anyways.
main without return type is not C++03 code ;)
First of all you shall specify return type int for the main. So you code will look

1
2
3
4
5
6
#include <fstream>
int main(){
int a,b;
std::ifstream ("input.txt")>>a>>b;
std::ofstream ("output.txt")<<a+b;
}


What about this code?

1
2
3
4
5
6
#include <fstream>
int main( int, char **s ){
int a,b;
std::ifstream ( s[1] )>>a>>b;
std::ofstream ( s[2] )<<a+b;
}



But your code and my code are incorrect! Because rvalue cannot be binded with non-const reference.:) I did not test them but I think that a C++ Standard compliant compiler shall issue an error.
Last edited on
Yes, I know that main without int was a long time ago but it works
thx, but last one
int main( int, char **s )
isn't error, but stops the program(couldn't open file maybe and I willn't enter filename in the command string) :(
So, I know that someone wrote a 54-symbols solutions( with input.txt & output.txt!)
And compiler is MSVC 6.0 :)))Old
Maybe the input.txt is only one file in the folder - could it help?
Last edited on
I said that your program shall not be compiled because it does not satisfiy the C++ standard. It can be only compiled by MS VC++ which has a bug.

As for int main( int, char **s ) you should specify two parameters, "input.txt" and "output.txt", when you run the program.
Last edited on
but it willn't be Me , it will be testing system:(
1.we have 2 files (input.txt and output.txt)
2.in intput.txt we have first int, space, second int
3.we should write sumof those ints into output.txt
4.Nothing else:)
One more your program is incorrect. temporary object ( ifstream( "input.txt" ) or ofstream( "output.txt) ) may not be binded to a non-const reference in operator >> and operator <<.
Last edited on
It could be not incorrect but believe me, I used it more than once!
MS VC++ supports it.(of course it's not right, and in usual programm I never use such construction).
I have no idea's what to cut or change:(
1
2
3
4
5
6
7
8
#include "h.h"

m()
{
	i a, b;
	is >> a >> b;
	os << a+b;
}


h.h
1
2
3
4
5
6
#pragma once
#include <fstream>
#define m() int main()
#define i int
#define is std::ifstream("input.txt")
#define os std::ofstream("output.txt") 

Couldn't you just use fstream instead of ifstream/ofstream? it only saves 2 characters, but still. You could also eliminate all non essential white space as so:
1
2
#include<fstream>
main(){int a,b;std::fstream("input.txt")>>a>>b;std::ofstream("output.txt")<<a+b;}


It's still non compliant, but using what you had that should work for you and if I counted correctly it's 99 characters. Not quite 54, which I'm not sure it's possible to write something that short to do this, not without using a custom header.
@Catfish2
You can't use #define i and #define is, it won't work properly as far as I know, it will read the first i it comes to and change it to int, turning is to ints. I might be wrong.
Catfish2,
it's a usual testing system and I forget about I should use standard headers only.
So, summary of header's and programm's code more than in my first example:)
' ' and '\t' and '\n' willn't be counted
about fstream : not works:(
so, we need to save about 45 characters!!:)
I think it's fine to call operator<< and operator>> on the temporaries because they are class members.
Last edited on
safoex wrote:
about fstream : not works:(

You should be able to substitute ifstream with fstream. You can't do that with ofstream unless you pass additional arguments to the constructor but that would only increase the number of characters.
Microsoft Visual C++ Toolkit 2003
full name and surname:)
You could abuse the preprocessor to #include the input file itself... but that won't make your program shorter.

@ Volatile Pulse: I test my code before posting, usually. Don't you?
I think the CPP works on tokens separated by whitespace.
@safoex (7)

It could be not incorrect but believe me, I used it more than once!
MS VC++ supports it.(of course it's not right, and in usual programm I never use such construction).
I have no idea's what to cut or change:(


A MS VC++ has a bug and this bug is well-known.
@Catfish2
I test most of my programs as well. I was unaware that the #define macros had delimiters in them, but I've never tried to shortcut something that much either.

But let's look at the minimum safoex, you need the follow to make your code work:
#include<fstream>
main(){
int a,b;
input.txtab;
output.txta+b;
}
There is already 58 characters there and it is no where close to working. It is impossible to make this program only be 54 characters long using ONLY standard header files. Your dreams have been dashed.
Pages: 12