making wcin wcout work

I

interec

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
$
 
M

Marcus Kwok

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.
 
M

Mike Wahler

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
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top