By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,434 Members | 1,858 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,434 IT Pros & Developers. It's quick & easy.

question about skipws manipulator

P: n/a
// ws manipulator example
#include <iostream>
#include <sstream>
using namespace std;

int main () {
char a[10], b[10], c[10];

istringstream iss (" one \n \t two");
iss >noskipws;
iss >a >skipws >b >c;
cout << a << "," << b <<"."<<c<< endl;

return 0;
}
----------------------------
I expected the above code will output " ,one.two",
but it is ",."
I think a=" "; -noskipws
b="one"; --skipws
c="two"; --skipws

Anything I missed?
Nov 5 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
thomas wrote:
// ws manipulator example
#include <iostream>
#include <sstream>
using namespace std;

int main () {
char a[10], b[10], c[10];

istringstream iss (" one \n \t two");
iss >noskipws;
iss >a >skipws >b >c;
cout << a << "," << b <<"."<<c<< endl;

return 0;
}
----------------------------
I expected the above code will output " ,one.two",
but it is ",."
I think a=" "; -noskipws
b="one"; --skipws
c="two"; --skipws

Anything I missed?
You didn't initialise your arrays. You didn't check the failbit after
the input operation. Try this:
===========================
// ws manipulator example
#include <iostream>
#include <sstream>
using namespace std;

int main () {
char a[10] = {}, b[10] = {}, c[10] = {};

istringstream iss (" one \n \t two");
iss >noskipws;
if (iss >a) {
if (iss >skipws >b) {
if (iss >c)
cout << "everything was converted OK\n";
else
cerr << "error reading 'c'\n";
}
else
cerr << "error reading 'b'\n";
}
else
cerr << "error reading 'a'\n";

cout << a << "," << b <<"."<<c<< endl;

return 0;
}
===========================
I get "error reading 'a'", and am guessing you need to read more about
'noskipws' and its effects on the input operations...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 5 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.