Newbie question on strings

J

James Kanze

So if my program is supposed to output something that must be processed
by a C/C++ implementation, then I need a newline. That says nothing
about the program I wrote.

No. If your program outputs to a stream opened in text mode,
the last character output must be a new line. It starts with
the definition of a text stream: "A text stream is an ordered
sequence of characters composed into lines, each line consisting
of zero or more characters plus a terminating new-line
character." C++ (or rather C---C++ just refers to the C
standard here) defines text streams as a sequence of lines. It
also defines a line as being *terminated* by a new-line
character. It then goes on to say: "Whether the
last line requires a terminating new-line character is
implementation-defined." In other words, an implementation may
(but is not required to) do the equivalent of implicitly
appending a '\n' to the text file if it does not end in one.

I suspect that part of the problem is that some Windows programs
treat the 0x0D,0x0A sequence in a file as a line separator,
whereas most (all?) C++ implementations treat it as a line
terminator. And I've been unable to find any official
documentation as to which is correct. (If it is a separator,
then a C++ implementation should output nothing, rather than
0x0D,0x0A for the final '\n'.)
However, given the fact that one of the reference programs mentioned
ended with a newline (the other didn't produce any output at all,) I
should probably have done so as well. Thus, using the same argument I
used to show that a blank string on bad input is correct, I show that
not putting a newline at the end is incorrect. :)

Well, if we wanted to be really correct, we'd start by writing a
requirements specification first, and reviewing that, before
writing a single line of code. That's the only professional way
to procede:). In practice, given a block of code without error
handling, I tend to suppose that the lack of error handling is
oversight, not intent, unless there is an explicit comment
explaining that just ignoring the error does result in the
required behavior. More generally, checking for errors should
be an ingrained automatism when writing code.
 
J

James Kanze

struct non_digit {
bool operator()(int d) {
return !std::isdigit(d);

Note that the implicit conversion of char to int (which occurs
when you call this function) may result in a negative value on
many systems, which in turn results in undefined behavior.

Declare the argument type unsigned char, and it's OK.
 
J

Jerry Coffin

[ ... ]
Note that the implicit conversion of char to int (which occurs
when you call this function) may result in a negative value on
many systems, which in turn results in undefined behavior.

Declare the argument type unsigned char, and it's OK.

Oops -- you're absolutely right. Thanks for the correction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top