Accelerated C++ Solution to Exercise 0-10

Exercise 0-10

Rewrite the Hello World program so that a newline occurs everywhere that white-space is allowed in the program.

Solution

The question essentially asks us to replace any space characters with the newline character \n.

#include <iostream>
int main()
{
    std::cout << "Hello,\nWorld!" << std::endl;
}

Result

The program compiles okay in Code::Block. It gives the followings in the console output window. Note that the newline character, \n, creates a new line after the “Hello,”.

Hello,
World!

Process returned 0 (0x0)   execution time : 0.174 s
Press any key to continue.

This concludes all 10 chapter zero end-of-chapter exercises!

Reference

Koenig, Andrew & Moo, Barbara E., Accelerated C++, Addison-Wesley, 2000

5 thoughts on “Accelerated C++ Solution to Exercise 0-10”

  1. I think you misunderstood this one. It actually wants you to add newlines in the code: here’s what i got (it’s ugly and it’s supposed to be!)

    #include 
    int
    main()
    {
    std::cout
    <<
    "Hello, World!"
    <<
    std::endl
    ;
    return
    1
    ;
    }
    
  2. You can add more.

    #include 
    int 
    main
    (
    )
    {
        std
        ::
        cout 
        << 
        "Hello, world!" 
        << 
        std
        ::
        endl
        ;
        return 
        0
        ;
    }
    

    I just started this book. Loving it so far. No blather and straight to the point.

Leave a reply