mardi 27 juin 2017

wcin and cin in same program to read user input

I know it is not recommended to use both narrow and wide character versions of the same stream (e.g., cin and wcin), but I need exactly this functionality.

How can I read user input safely in the same program into both narrow and wide characters?

Here is a (bad) sample program, which after reading in and printing out wch, is done. Does not wait for inputting ch, possibly reads some garbage, and does not print anything.

#include <iostream>
using namespace std;

int main()
{
    wchar_t wch[10];
    char     ch[10];

    wcin >> wch;
    wcout << wch;
    cin >> ch;
    cout << ch;
}

What would be an acceptable version of the above program that handles narrow and wide inputs (and outputs)? (Don't bother with size safety!) Thanks!

Aucun commentaire:

Enregistrer un commentaire