#include<iostream.h>
void main()
{
cout<<"Love";
}
The question is how can we change the output of this program into "I Love You" by without making any change in main().
#include<iostream.h>
void main()
{
cout<<"Love";
}
The question is how can we change the output of this program into "I Love You" by without making any change in main().
You need to change the main, by either calling another function or by changing the text. Since main()
is the main output of your program
Can you be a little more precise?
You want the output of that piece of code to be "I love you" instead of "Love"?
Edit: I don't think you can't without changing at least one line of code in main(). You can either change from cout<<"Love" to cout<<"I love you" or just add a function that outputs that specific line.
Shouldn't your main function return an int? You're either going to need to change the method, or write another program that this one pipes into, but that's the most round about way to change a simple string...
That code has no using std but anyway it would require writing your own wrapper around cout and removing the using std if there was and replace with using mystd where the wrapper is defined.
I guess you could write an operator<< that added "I" before and "You" after the current output.
Ok, fixing your main function and iostream.h ... This is the way
#include <iostream>
// to make sure std::cout is constructed when we use it
// before main was called - thxx to @chappar
std::ios_base::Init stream_initializer;
struct caller {
caller() { std::cout << "I "; }
~caller() { std::cout << " You"; }
} c;
// ohh well, for the br0ken main function
using std::cout;
int main()
{
cout<<"Love";
}
I figured i should explain why that works. The code defines a structure that has a constructor and a destructor. The constructor is run when you create an object of the struct and the destructor is run when that object is destroyed. Now, at the end of a struct definition, you can put declarators that will have the type caller
.
So, what we did above is creating an object called c
which is constructed (and the constructor called) at program start - even before main is run. And when the program terminates, the object is destroyed and the destructor is run. In between, main
printed "Love".
That pattern actually is very well known by the term RAII
which usually claims some resource in the constructor and releases it again in the destructor call.
Like this:
#include <iostream>
int main() {
std::cout << "I Love You" << std::endl;
return 0;
}
/*
#include<iostream.h>
void main()
{
cout<<"Love";
}
*/
This way, you haven't changed anything in the main
. :-p
It seems you're saying you don't want code changes, but you want different behaviour. Since the behavior you want cannot be achieved by magic you have two real solutions:
Me? I used to break copy protection so I'd go the latter route. Build the program from the source you have but then edit the binary so that it behaves how you prefer.
Given the roundabout nature of this solution though I'd strongly wonder at your motivation. It seems like such a pointless question to ask unless you don't understand compilers, or have sinister motivation...
#include <iostream>
class tclass
{
public:
void operator <<(char *s)
{
std::cout<<"I"<<s<<"You"<<std::endl;
}
};
tclass cout;
int main()
{
cout<<"love";
}
Not as elegant as litb's, but an alternative:
#include <iostream>
using namespace std;
int foo()
{
cout << "I Love You" << endl;
return cout.rdbuf(0);
}
int i = foo();
int main()
{
cout << "Love" << endl;
}
The lesson is that C++ can execute code before and after main() through static constructors/destructors, eg. the code posted by litb.
Not as elegant as litb's, but it works
#include <iostream>
#include <cstdio>
#include <sstream>
#define cout printf("I love you\n"); std::ostringstream os; os
int main()
{
cout << "love";
}
Of course, you don't need to use a stringstream
, you could use any class with operator<<
.
Assuming this was a class assignment, I would bet the idea was that you could rewrite iostream.h
, since C++ doesn't treat it as special (for certain definitions of "special").
We can do it like this too:
#include <iostream>
#include <cstdlib>
using namespace std;
int fnfoo(int inum){
cout << "I Love You" << endl;
return (exit(0),inum);
}
int dummy = fnfoo(5);
int main()
{
cout << "Love" << endl;
}
Simple and works perfectly ;)
I'm really surprised that noone suggested #define "Love" "I love you"... :)