[C++] Fun Excercise

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

[C++] Fun Excercise

Post by TheBuzzSaw »

Here is your starting code.

Code: Select all

#include <iostream>
using namespace std;

class Blargh
{
    public:
        Blargh()
        {
            mIndex = index++;
            cout << spaces << mIndex << " is born!\n";
            *spaceUpdate++ = ' ';
        }

        ~Blargh()
        {
            *--spaceUpdate = '\0';
            cout << spaces << mIndex << " dies!\n";
        }

    private:
        static size_t index;
        static char spaces[128];
        static char* spaceUpdate;
        size_t mIndex;
};

size_t Blargh::index = 0;
char Blargh::spaces[128] = {};
char* Blargh::spaceUpdate = spaces;

int main(int argc, char** argv)
{
    // ???

    return 0;
}
Here is your target output.

Code: Select all

0 is born!
 1 is born!
  2 is born!
   3 is born!
    4 is born!
     5 is born!
      6 is born!
       7 is born!
        8 is born!
        8 dies!
       7 dies!
      6 dies!
      9 is born!
       10 is born!
       10 dies!
      9 dies!
     5 dies!
    4 dies!
   3 dies!
  2 dies!
 1 dies!
I don't know why it won't render properly, but the second row onward (starting with "1 is born!") should be indented by one more space.

Write the code for main. Good luck. :)
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: [C++] Fun Excercise

Post by Ginto8 »

This is kids' stuff. Also, I'm presuming you accidentally left out he "0 dies!":
Click here to see the hidden message (It might contain spoilers)

Code: Select all

int main() {
    Blargh a;
    {
        Blargh b,c,d,e,f;
        {
            Blargh g,h,i;
        }
        {
            Blargh g,h;
        }
    }
    exit(0);
}

7 lines of code added to main.
EDIT: redid it because apparently the 0 isn't supposed to die. Also, I didn't know exit() ignored deconstruction
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: [C++] Fun Excercise

Post by N64vSNES »

Ginto8 wrote:This is kids' stuff.
Showoff :lol:
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: [C++] Fun Excercise

Post by Ginto8 »

N64vSNES wrote:
Ginto8 wrote:This is kids' stuff.
Showoff :lol:
I just prefer puzzles that are actually challenging ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
TheBuzzSaw
Chaos Rift Junior
Chaos Rift Junior
Posts: 310
Joined: Wed Dec 02, 2009 3:55 pm
Current Project: Paroxysm
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: [C++] Fun Excercise

Post by TheBuzzSaw »

Ginto8 wrote:I just prefer puzzles that are actually challenging ;)
...says the guy who (A) made a faulty assumption about the requirements and (B) learned something about the behavior of exit().
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: [C++] Fun Excercise

Post by N64vSNES »

TheBuzzSaw wrote:
Ginto8 wrote:I just prefer puzzles that are actually challenging ;)
...says the guy who (A) made a faulty assumption about the requirements and (B) learned something about the behavior of exit().
Why does every thread I touch begins a flame war? :(
D-e-X
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 39
Joined: Tue Dec 28, 2010 6:49 pm

Re: [C++] Fun Excercise

Post by D-e-X »

N64vSNES wrote:
TheBuzzSaw wrote:
Ginto8 wrote:I just prefer puzzles that are actually challenging ;)
...says the guy who (A) made a faulty assumption about the requirements and (B) learned something about the behavior of exit().
Why does every thread I touch begins a flame war? :(
I think you need to look up the definition of flaming. ;)

Because your presence makes people rage with fury. (Now that's flaming) :)
You don't know me, so just be aware that I'm joking, and I have nothing (so far) against you.
I remember when I used to be into nostalgia.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: [C++] Fun Excercise

Post by GroundUpEngine »

Ginto8 wrote:
N64vSNES wrote:
Ginto8 wrote:This is kids' stuff.
Showoff :lol:
I just prefer puzzles that are actually challenging ;)
High five! :lol:
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: [C++] Fun Excercise

Post by N64vSNES »

D-e-X wrote:I think you need to look up the definition of flaming. ;)
I bet it was a start....
D-e-X wrote:Because your presence makes people rage with fury. (Now that's flaming) :)
:lol:
You don't know me, so just be aware that I'm joking, and I have nothing (so far) against you.
Oh but I do know you.
1- Why throw half a pizza out?
2- I like your new furniture.

And sounds like my sense of humor....could be the reason behing the flame wars....
Post Reply