#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:)
#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.
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.
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?
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 <<.
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:(
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:
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:)
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.
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:(
@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.