Jump to content
  • Advertisement
Sign in to follow this  
dacosi

A C++ problem

This topic is 4023 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

I got a C++ problem and I have no idea how to do this: The problem goes like this: Write a C++ program that prints out its own source code without any inputs or file processing ( reading a file, that is ). Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Since you're new here, I won't flame you for posting (what seems like) a homework question. Just be warned that doing so, without any effort shown to solve the problem, typically results in p/o mods / locked threads / r-- / etc.

That being said, in my quite jet lagged state, I couldn't quickly think up how to do this, so a quick google turned up this.

Share this post


Link to post
Share on other sites
Wow, is that homework?? That's the most evil homework assignment I've heard of.

I think the easiest method is:

1) Declare a string. Put the source code of your file (which you write next) inside the string. Except, don't write the actual literal of the string inside the string (because you would be typing forever). Instead give yourself a way to insert the value of the string later (for example, write %s so that you can insert the string with printf)

2) Print out the string, inserting the value of the string inside the string.

Edit: I just tried to write one, I guess you also need to figure out some clever way to escape the quote marks inside the string. Oh well. Good luck!

Share this post


Link to post
Share on other sites
Quote:
Original post by pinacolada
That's the most evil homework assignment I've heard of.
I agree!

Share this post


Link to post
Share on other sites
Write the following phrase twice, the second time in quotes: "Write the following phrase twice, the second time in quotes:"

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Write the following phrase twice, the second time in quotes: "Write the following phrase twice, the second time in quotes:"


This is an illustration of the general technique.

The "phrase" depends on the syntax of the programming language. It's best to design things in such a way that you set up the "write twice, the second time in quotes" logic first, accepting some placeholder; then you figure out what the placeholder needs to contain.

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!