[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

question about skipws manipulator

thomas

11/5/2008 12:32:00 PM

// 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?
1 Answer

Victor Bazarov

11/5/2008 1:37:00 PM

0

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