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
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!)
You are probably right. Thanks!
You can add more.
I just started this book. Loving it so far. No blather and straight to the point.
Thanks @MadLogicDev. Agreed!
This is awesome!
For more C++ exercise, please visit : http://ccphplusplus.blogspot.com/
Maybe we can exaggerate it a little more:
#include “stdafx.h”
#include
using namespace std;
int main()
{
cout
<<
“H”
“e”
“l”
“l”
“o”
“,”
“w”
“o”
“r”
“l”
“d”
“!”
<<
endl
;
}