1

Is it possible to read multiple lines of international characters? I can do this with simple ascii strings:

#include <iostream>
#include <string>

void main ()
{
    std::string text{};
    //I cannot use std::wcin in that manner
    //std::wcout << "Enter any arbitrary text terminated by an asterisk:" << std::endl;
    //std::wstring wtext{};
    //std::getline (std::wcin, wtext, '*');THIS SPECIFIC LINE DOESN'T COMPILE
    std::cout << "Enter any arbitrary text terminated by an asterisk:" << std::endl;
    std::getline (std::cin, text, '*');
}

How can I achieve the same using a string type that supports international characters?

As requested, I included the line that wouldn't compile. My doubt is: is there another way?

The error is: no instance of overloaded function "std::getline" matches the argument list

Solved. Please note the presence of an L prefixing '*' in the accepted answer. In my commented out call to std::getline using wcin and wstring (the one that would generate the compile error) I forgot to use a wchar_t literal and used a char literal instead. Thus the types were mismatched (string is composed of char, wstring of wchar_t - aka wide char). To represent a wide char literal, one needs to add an L as a prefix, i.e. L'*'.

Davi
  • 398
  • 2
  • 12
  • 3
    *I cannot use std::wcin in that manner* Why not? – NathanOliver Jun 29 '21 at 19:25
  • 1
    Also, C++ has terrible unicode support. If you want to use unicode, you should get a library dedicated to it to make your life easier. – NathanOliver Jun 29 '21 at 19:27
  • ``std::getline`` doesn't have a corresponding overload for ``wstrings`` and ``wcin`` – Davi Jun 29 '21 at 19:28
  • Please create a [mcve] of the program you have problem with, and then include the errors you get from that program. If there are build errors, then copy-paste (as text) the full and complete build output into the question, and add comments on the lines where you get the errors. – Some programmer dude Jun 29 '21 at 19:28
  • 4
    [`std::getline`](https://en.cppreference.com/w/cpp/string/basic_string/getline) is a *template* using the base template [`std::basic_string`](https://en.cppreference.com/w/cpp/string/basic_string). And as such can handle any kind of character, as long as both the stream and the string uses the same character type. – Some programmer dude Jun 29 '21 at 19:30
  • 2
    Perhaps the problem is that you still use the narrow-character `'*'` instead of the wide `L'*'`? The delimiter character must be the same character type as for the stream and the string. – Some programmer dude Jun 29 '21 at 19:32
  • 2
    [Works just fine here](http://coliru.stacked-crooked.com/a/4086eb6dc41b5d1e). Did you by chance forget to change `'*'` to `L'*'` in your call to `getline`? – NathanOliver Jun 29 '21 at 19:32
  • for `std::wstring` you should use `std::wcin`. – SHR Jun 29 '21 at 19:34
  • @Someprogrammerdude You are right! God, that was the only thing wrong. Should I keep the question up, everyone? The issue was rather stupid, I don't know if it's worth keeping the question. I believe it is. I don't have a monopoly on stupidity; someone else could find themselves in this exact same situation someday. – Davi Jun 29 '21 at 19:40

1 Answers1

2

Here an example using std::wstring, std::wcin and std::wcout

By the way: you shouldn't mix std::cin, std::cout, std::cerr and std::wcin, std::wcout, std::wcerr. if you choose to use std::wcin, use only std::wcin, std::wcout, std::wcerr

EDIT: if you use std::wcin in std::getline, all arguments must be wide, since the std::getline gets only one template argument for the character type, and used it several times: for the stream, the output string, and the delimiter.

#include <iostream>
#include <string>

int main ()
{
    std::wstring text{};
    
    std::wcout << L"Enter any arbitrary text terminated by an asterisk:" << std::endl;
    std::getline (std::wcin, text, L'*');
}
SHR
  • 7,940
  • 9
  • 38
  • 57
  • Yes, the lack of L in from of ``'*'`` was the problem. – Davi Jun 29 '21 at 19:42
  • @paulsm4 it's there. I put emphasis on it, maybe it was not very clear. – Davi Jun 29 '21 at 19:46
  • @SHR can you specifically mention the lack of L in front of ``*`` to make it a wide char? That was what was wrong with my code. I already know the thing about not mixing the streams, that's why those lines are commented out. – Davi Jun 29 '21 at 19:54