tags:

views:

1161

answers:

17
#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().

A: 

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

Ólafur Waage
A: 

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.

Cristina
yes..i need that output as you said..But the only condition is that there won't make even a single line code change in main().
Binu
Eh, you could define your own variable 'cout' in the global namespace, and have it print "I love you" to std::cout. :)
jalf
A: 

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...

zaczap
In C++ main() is an special function, and the only one allowed by the standard to declare a return type and not actually return anything (in which case the standard says that the compiler will perform as if a 0 was returned by the user)
David Rodríguez - dribeas
+2  A: 

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.

Tim Matthews
+1  A: 

I guess you could write an operator<< that added "I" before and "You" after the current output.

Zitrax
#include <iostream>#include <cstdio>#include <sstream>using namespace std;#define cout printf("I love you"); ostringstream os; os/*newline*/int main(){ cout << "love";}
rlbond
@rlbond erm, wouldn't that crash horribly because you're then doing printf(); << "love"; ? =D
Ed Woodcock
+79  A: 

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.

Johannes Schaub - litb
Very instructive, 'ya Smart ass.
dmckee
That is quite clever. +1!
John Feminella
Thanx..It's working..
Binu
@Binu: But do you *why* it works? If you do, you've learned something significant about c++...
dmckee
actually i dont understand..dmckeecan u be more specific?
Binu
@Binu: Well, libt explained it. It's about *when* constructors and destructors run, and what the lifetime of `c` is. Try replacing "c;" with "c, d;" and see what happens...
dmckee
wow man...too good....
Raghu
You do deserve a Guru badge for this :)
Mehrdad Afshari
@litb, The answer may not work in all the time. Is there any guarantee that cout will be constructed before your caller object? Does C++ standard mandates that cout, cin will be created before all the user global objects?
chappar
chappar, to my knowledge, there isn't any guarantee. you can however place an object of type ios_base::Init just before the struct definition.it has a counter.if it is zero, it will construct standard stream objects.In 27.3/2 however the std encourages implementations to construct them before main.
Johannes Schaub - litb
so, why don't you edit your answer? it will become more complete then!
chappar
chappar, i think you are right. i've put it in.
Johannes Schaub - litb
would vote +10 if I could
rlbond
i've really not deserved all that praise. just written a silly struct there. thanks to every1, i'm very much appreciating it :)
Johannes Schaub - litb
litb, your answer was great. it was made even complete and perfect by chappar. i think he deserves some credit as well, people are overlooking his contribution!
Chandan .
Mancs, indeed he's got a great idea making sure cout is already constructed. i'd give give him extra points on his comments if i only could!! Love You All :D:D
Johannes Schaub - litb
so modest of you. great spirit. keep up the good work.
Chandan .
Cute. Very elegant.
the_drow
Thank you so much, the_drow :) I've read some days ago that C++1x guarantees that cout and friends are constructed after #include <iostream> is included. So we don't need the stream_initializer then anymore. Until that time, i'll keep it in, of course :P
Johannes Schaub - litb
that's just wrong! cool. but wrong!
warren
oO why is it wrong, w00t?
Johannes Schaub - litb
Needs `std::cout` in `main`. :P
GMan
@dmckee BTW 'ya a long double smart ass too -.- (for those who have 10K - scroll down to that deleted answer and sigh).
Johannes Schaub - litb
@GMan hey fixed it haha
Johannes Schaub - litb
@Johannes: Ha, now it's ready to be accepted. :)
GMan
+8  A: 

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

Mehrdad Afshari
What i meant is #include <iostream>int main() { std::cout << "Love"; return 0;}now.. change the output into I Love You without making any change in main().
Binu
bad answer, not funny at all. if a 2 or 3 digit reputation holder posts this answer he'd get a -10 for it. thats SO double standard for you! :-)
Chandan .
A: 

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:

  1. Either abuse the pre-processor/compiler, or the code as seen above in litb's creative solution.
  2. Compile the code as-is so that it prints "Love", but then edit the generated binary.

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...

Steve Kemp
+32  A: 
#include <iostream>
class tclass
{
  public:
    void operator <<(char *s)
    {
          std::cout<<"I"<<s<<"You"<<std::endl;
    }
};

tclass cout;

int main()
{
  cout<<"love";
}
Warrior
This is pretty clever too.
James McMahon
very clever, no need of doing it extraordinary way, do it the simple way, thats what you have done. this is as good as litb's answer if not as cool. very well done indeed!
Chandan .
+6  A: 

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;
}
CTT
what does rdbuf(0) do? does your program print "Love" after printing "I Love You"? If it does then, could we possibly terminate the program inside foo() without ever having to execute main() at all?
chappar
It hides the output of "Love" by redirecting cout to nothing. It only prints "I Love You".
CTT
+1  A: 

The lesson is that C++ can execute code before and after main() through static constructors/destructors, eg. the code posted by litb.

Jimmy J
+9  A: 

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<<.

rlbond
Cheap but effective.
Tyler McHenry
Hackish :) Still nice.
the_drow
A: 

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").

Max Lybbert
If fact the standard does treat treat iostream (and all other standard headers) differently. The compiler is allowed to just inject the definitions in the code without actually including the header file. Then again, even if compilers are allowed to do it, most current implementations do not. Anyway, the use of angle brackets has concrete implications (with some compilers it will search for the files first in compiler and system wide directories, then locally), then again (again) other compilers just treat angle brackets and double quotes equally.
David Rodríguez - dribeas
In other words "for certain definitions of 'special'."
Max Lybbert
A: 

why dosen't to have contol to oprate the new value of the new function.......thn it controled by the input .......if u can understand to explain to me...

+4  A: 

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 ;)

nthrgeek
A: 

I'm really surprised that noone suggested #define "Love" "I love you"... :)

dinci
Probably because you can't.
GMan
This will not work since "Love" is a string and not an identifier or a symbolic name.
naivnomore