"Matthew David Hills" <hi***@Stanford.EDU> wrote in message
news:bj**********@news.Stanford.EDU...
If setw() is being used to limit how many characters are pulled from
a stream, should the # of characters taken from the stream depend on
whether the stream is being sent to a std::string or a char*?
string s1;
char s2[256];
cin >> setw(4) >> s1;
cin >> setw(4) >> s2;
(I've found there to be a difference between compilers -- on some,
they results are identical (ie, 3 characters pulled), and on others, the
string receives a 4th character)
Thanks,
Matt
Its a good point, and the answer is yes the number should vary. The reason
is that setw is being used for subtly different reasons in the two cases.
For std::string setw is that maximum numbers of characters to be extracted,
for char* setw is intended as the size of the array that the char* is
pointing at. Since C style strings require a null terminator only setw - 1
characters can be extracted.
john