Accelerated C++ Solution to Exercise 0-7

Exercise 0-7

Is this a valid program? why or why not?

#include <iostream>
int main{}
{
    /* This is a comment that extends over several lines
       because it uses /* and */ as its starting and ending delimiters */
       std::cout << "Does this work?" << std::endl;
       return 0;
}

Solution

No. This is not a valid C++ Program.

The issue is the way the code treats comment. A correct block-comment looks like this.

/* this is a comment */

i.e. a correctly written comment is surround by the /* and */. Anything in between is a comment. Anything outside is not a comment.

In this case, the actual comment part is highlighted in blue below (and the remaining red part is not qualified as a comment)

/* This is a comment that extends over several lines
because it uses /* and */ as its starting and ending delimiters */

I would correct the comment to something like this (using the line comment //), should we wish to keep the comments.

// This is a comment that extends over several lines
// because it uses /* and */ as its starting and ending delimiters

Reference

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

One thought on “Accelerated C++ Solution to Exercise 0-7”

Leave a reply