Is this a real code in Dev c ++

I never seen this before example cout<< Concert4 Program Answers.\n\n";

Thanks
Carmen

Recommended Answers

All 6 Replies

It's syntactically incorrect since the string literal wasn't properly started, but otherwise the code is legit C++:

cout << "Concert4 Program Answers.\n\n";

What concerns in particular do you have about the statement?

As indicated by deceptikon, with the leading double quote, it is legimate C++, but not GOOD C++ code. The newline tokens will not flush the output stream. Instead of the trailing "\n\n" tokens, it should be << endl << endl which will cause the output stream to print the data to the target (console or terminal in this case).

The newline tokens will not flush the output stream.

And they likely don't need to do so. This particular statement strikes me as one of the following:

  1. Part of a prompt for input. After the prompt there will be a request, probably through a tied stream, and thus endl isn't required because the input stream will do a preliminary flush of tied output streams.

  2. An early line in a result report. There's little reason to flush until the report has been completed.

My recommended guideline is to prefer '\n' over endl except in cases where an explicit flush is absolutely needed and can be defended in a code review. Obviously this means that the programmer must understand when and where C++ will help you by flushing automatically.

Beginners can use endl as a crutch until properly learning the nuances of flushing, but too often that crutch is never abandoned.

Instead of the trailing "\n\n" tokens, it should be << endl << endl

At the very most, assumung a flush is required, only one endl or flush would be needed at the end. Otherwise you'd be flushing unnecessarily at least one time:

cout << "Concert4 Program Answers.\n" << endl; // Flush once, not twice
cout << "Concert4 Program Answers.\n\n" << flush; // More clear intentions?

@decepticon
Your observations are true enough, but I was trying to point out that newline characters in the output stream aren't enough to flush the output. Your approach is also valid, but remember that without the flush or endl output manipulator, your output could be intermixed with output from other (background) processes - this has happened to me in the past. If the output stream is cerr, then it is mute since that is automatically flushed on each character, much like a typewriter. Also, output to cout can be emitted after some number of characters fill the output buffer, in which case cout can flush as needed.

Member Avatar for iamthwee

Or you could just accept everything as strings and parse as necessary and you can avoid racking your brains with these nuances... But let's not derail the OP's thread.

but remember that without the flush or endl output manipulator, your output could be intermixed with output from other (background) processes

Thus why one should understand when and why flushing occurs automatically so as to know when to do it explicitly. The key lesson is that programming isn't a mindless activity. If you throw in endl, or any potentially unnecessary operation for that matter, "just to be safe" then that constitutes mindless programming and risks inefficient solutions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.