Page 1 of 1

[C++] Fun Excercise

Posted: Wed Jan 12, 2011 8:51 pm
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. :)

Re: [C++] Fun Excercise

Posted: Wed Jan 12, 2011 9:09 pm
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

Re: [C++] Fun Excercise

Posted: Tue Jan 25, 2011 3:50 pm
by N64vSNES
Ginto8 wrote:This is kids' stuff.
Showoff :lol:

Re: [C++] Fun Excercise

Posted: Tue Jan 25, 2011 4:06 pm
by Ginto8
N64vSNES wrote:
Ginto8 wrote:This is kids' stuff.
Showoff :lol:
I just prefer puzzles that are actually challenging ;)

Re: [C++] Fun Excercise

Posted: Wed Jan 26, 2011 1:35 pm
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().

Re: [C++] Fun Excercise

Posted: Wed Jan 26, 2011 3:00 pm
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? :(

Re: [C++] Fun Excercise

Posted: Wed Jan 26, 2011 3:37 pm
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.

Re: [C++] Fun Excercise

Posted: Wed Jan 26, 2011 4:14 pm
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:

Re: [C++] Fun Excercise

Posted: Wed Jan 26, 2011 4:19 pm
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....