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

making wcin wcout work

P: n/a
I have a simple program in which I want to read a wstring using wcin
and then output the same string using wcout. The program takes a few
words separated by spaces as input. The problem is that only the first
word is outputted. Everything after the first space seems to be lost
in the output. My OS is redhat hat linux 4 ES. gcc 3.4.6.

How do I fix this and why its not working? Thanks
Code
====

#include <iostream>
#include <iomanip>
#include <locale>
#include <string>

using namespace std;

int main()
{
locale l("en_US.UTF-8");
wcin.imbue(l);
wcout.imbue(l);

wstring sSql;
wcout << L"Enter Sql Statement: "; // input
wcin >sSql;

wcout << endl << endl;

wcout << L"You entered: " << sSql << endl; // output

return 0;
}

Sample Run
=========

$ g++ two.cpp
$ ./a.out
Enter Sql Statement: select a from b
You entered: select <-- what happened to everything after first
space ????$*#@X
$

Mar 23 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
in*****@interec.net wrote:
I have a simple program in which I want to read a wstring using wcin
and then output the same string using wcout. The program takes a few
words separated by spaces as input. The problem is that only the first
word is outputted. Everything after the first space seems to be lost
in the output. My OS is redhat hat linux 4 ES. gcc 3.4.6.

How do I fix this and why its not working? Thanks
Code
====
[snip]
wstring sSql;
wcout << L"Enter Sql Statement: "; // input
wcin >sSql;
I don't use the wide streams, but I am pretty sure the behavior is the
same as with the regular streams.

The operator>>(istream&, string&) by default stops reading at the first
whitespace. To read a whole line, you use std::getline(). I am not
sure if there is a version for wide streams and wide strings, but my
intuition says that it should work, since getline() is a template
function.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Mar 23 '07 #2

P: n/a

<in*****@interec.netwrote in message
news:11********************@e65g2000hsc.googlegrou ps.com...
>I have a simple program in which I want to read a wstring using wcin
and then output the same string using wcout. The program takes a few
words separated by spaces as input. The problem is that only the first
word is outputted. Everything after the first space seems to be lost
in the output. My OS is redhat hat linux 4 ES. gcc 3.4.6.

How do I fix this and why its not working? Thanks
Code
====

#include <iostream>
#include <iomanip>
#include <locale>
#include <string>

using namespace std;

int main()
{
locale l("en_US.UTF-8");
wcin.imbue(l);
wcout.imbue(l);

wstring sSql;
wcout << L"Enter Sql Statement: "; // input
wcin >sSql;
Change this to:

getline(wcin, sSql);

( The >operator stops on whitespace, 'getline()' stops on
delimiter given as third argument, which has default
of '\n' )

-Mike
>
wcout << endl << endl;

wcout << L"You entered: " << sSql << endl; // output

return 0;
}

Sample Run
=========

$ g++ two.cpp
$ ./a.out
Enter Sql Statement: select a from b
You entered: select <-- what happened to everything after first
space ????$*#@X
$

Mar 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.