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:
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.
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
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
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?
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.
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.
Edited 2 Years Ago by iamthwee
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.
Hi, as I was told that my code doesn’t scale well at all, I thought perhaps I’d try to get a better understanding of interfaces/abstract classes and classes and the relationship between them.
I don’t want at this stage work on a big separate project as I've already got plenty ...
OK, so HostGator for some reason no longer allows gcc/g++ access unless you have a Designated Server account, which is a lot of money to spend just to compile my "Hello World" program. Thus I figured I'd compile at home, then upload. Program is your regular old bare-bones Hello World ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.
Private Sub btn_addline_Click(ByVal ...