Jump to content
  • Advertisement
Sign in to follow this  
TEUTON

Is there any way to do this

This topic is 4613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Huh!! It was not a home work...It just couldn't catch up with people's reply...otherwise I would have explained the problem if anybody would have asked for it. anyways here's my question again How can I change the code int main() { std::cout<<"Hello World!\n"; return 0; } to produce this output: Initialize Hello World! Clean Up Do not change main() in any way. What I thought was(I didn't saw anyone's reply)....if I do this #include <iostream.h> class my1 { public: my1(){cout<<"Initialize\n";} ~my1(){cout<<"Clean Up\n";} }; class my2 { public: static my1 a; }; my1 my2::a; //definition of static onject, thus space will be allocate and constructor will be called int main() { std::cout<<"Hello World!\n"; return 0; //at exit, static object destructor will be called. } But output was only Initialize Hello World! Is there any way that clean up should also be outputted

Share this post


Link to post
Share on other sites
Advertisement
It works on my machine. (BTW, #include <iostream>, not <iostream.h>)

Maybe it's a problem with buffered output? Try cout << "Clean Up" << std::endl;, that should flush the buffer immediately, or use cerr maybe.

EDIT: Just for kicks, this is how your teacher whoever made the exercise would not want it solved:


#include <iostream>

using std::cout;
#define std cout<<"Initialize!\n";std
#define return cout<<"Clean Up!\n";return 0;

int main()
{
std::cout<<"Hello World!\n";
return 0;
}



EDIT2: Or this!


#include <iostream>

int main()
{
std::cout << "Initialize!\nHello World!\nClean Up!\n";
}

#define main this_is_not_main

int main()
{
std::cout<<"Hello World!\n";
return 0;
}

Share this post


Link to post
Share on other sites
I don't know enough about namspaces, but perhaps you could somehow block iostream's "std" namespace and create your own class with "std" namespace, and make your own "cout" which passes arguments to the "real cout", and overload "<<". Just another idea.


discman1028

Share this post


Link to post
Share on other sites
Quote:
Original post by load_bitmap_file
It works on my machine. (BTW, #include <iostream>, not <iostream.h>)

Maybe it's a problem with buffered output? Try cout << "Clean Up" << std::endl;, that should flush the buffer immediately, or use cerr maybe.


I tried on VC6.0 and DEV C++(latest version)...it didn't work.

I tried what you suggested...but still it's not working.

Share this post


Link to post
Share on other sites
This works just fine...


struct SDummy
{
SDummy(void)
{
std::cout << "Foo" << std::endl;
}

~SDummy(void)
{
std::cout << "Bar" << std::endl;
}
};

SDummy g_pDummy;

void main(void)
{
std::cout << "Test" << std::endl;
}




Share this post


Link to post
Share on other sites
I would move the "my1 my2::a;" line inside of main(), rather than outside...

Because you are making a static declaration, the constructor and destructor function pointers are being simply added to the ctor and dtor lists. It is possible that whatever library implementation you are using might have its dtor called before yours, shutting down the library before it can output your text.

Edit: Then I re-read the post and see "Do not change main() in any way." *sigh* Oh well, I tried :)

Share this post


Link to post
Share on other sites
Quote:
Original post by raz0r
This works just fine...

*** Source Snippet Removed ***


Except that ::main must return an int.

Share this post


Link to post
Share on other sites
Wow load_Bitmap_File
Very nice solutions.(added to rep)

raz0r your code stillnot giving desired output

Thank You

Share this post


Link to post
Share on other sites
Well, in this instance, I used "void main...", it doesn't matter really, just saying that it works :|

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!