Debug Assertion Failed when using ispunct('ø') - c++

I am writing a program that handles large portions of text, and need to remove punctuation. I encountered a Debug Assertion Failed error, and isolated it to this: It occurs when testing ispunct() on non-English letters.
my test-program is now like this:
main.c
int main() {
ispunct('ø');
cin.get();
return 0;
}
The Debug Assertion Failed window looks like this:
Screenshot of the error
All non-English letters I have tried cause this problem, including 'æ', 'ø', 'å', 'é', etc. Punctuation and English letters do not cause the problem. It's probably something very simple that I am overlooking, so I am thankful for any help!

Character 'ø' must be representable as an unsigned char, otherwise you should use type wchar_t and std::ispunct, for example:
#include <iostream>
#include <locale>
int main()
{
const wchar_t c = L'ø';
std::locale loc("en_US.UTF-8");
std::ispunct(c, loc);
}
For your problem, you can also do this:
#include <locale>
#include <string>
#include <algorithm>
#include <functional>
int main()
{
std::wstring word = L"søme.?.thing";
std::locale loc("en_US.UTF-8");
using namespace std::placeholders;
word.erase(std::remove_if(word.begin(), word.end(),
std::bind(std::ispunct<wchar_t>, _1, loc)), word.end());
std::wcout << word << std::endl;
}

Related

c++How to use utf-8 and/or utf-32 in c++ without wchar without stuff like ICU (gcc/cl compilers)?

I have tried many things but they don't seem to work.
#include <iostream>
#include <locale>
using namespace std;
int main() {
setlocale(LC_ALL, "en-US.utf8");
std::string uni = u8"ユチエフー8";
std::cout << uni;
std::cin.get();
return 0;
}
Please note that I want utf-8/utf-32 to work not in just this situation but most of the situations without using things like wide characters.

c++ iterate through a vector of strings

So I recently discovered the use of map and vectors, however, I'm having trouble of trying to figure a way to loop through a vector containing strings.
Here's what I've tried:
#include <string>
#include <vector>
#include <stdio>
using namespace std;
void main() {
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t) {
cout<<*t<<endl;
}
}
and when I try to compile it, I get this error:
cd C:\Users\Jason\Desktop\EXB\Win32
wmake -f C:\Users\Jason\Desktop\EXB\Win32\exbint.mk -h -e
wpp386 ..\Source\exbint.cpp -i="C:\WATCOM/h;C:\WATCOM/h/nt" -w4 -e25 -zq -od -d2 -6r -bt=nt -fo=.obj -mf -xs -xr
..\Source\exbint.cpp(59): Error! E157: col(21) left expression must be integral
..\Source\exbint.cpp(59): Note! N717: col(21) left operand type is 'std::ostream watcall (lvalue)'
..\Source\exbint.cpp(59): Note! N718: col(21) right operand type is 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> (lvalue)'
Error(E42): Last command making (C:\Users\Jason\Desktop\EXB\Win32\exbint.obj) returned a bad status
Error(E02): Make execution terminated
Execution complete
I tried the same method using map and it worked. The only difference was I changed the cout line to:
cout<<t->first<<" => "<<t->last<<endl;
Add iostream header file and change stdio to cstdio.
#include <iostream>
#include <string>
#include <vector>
#include <cstdio>
using namespace std;
int main()
{
vector<string> data={"Hello World!","Goodbye World!"};
for (vector<string>::iterator t=data.begin(); t!=data.end(); ++t)
{
cout<<*t<<endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> data = {"Hello World!", "Goodbye World!"};
for (std::vector<std::string>::iterator t = data.begin(); t != data.end(); t++) {
std::cout << *t << std::endl;
}
return 0;
}
Or with C++11 (or higher):
#include <iostream>
#include <vector>
#include <string>
typedef std::vector<std::string> STRVEC;
int main()
{
STRVEC data = {"Hello World!", "Goodbye World!"};
for (auto &s: data) {
std::cout << s << std::endl;
}
return 0;
}
From the Open Watcom V2 Fork-Wiki on the C++ Library Status page:
<string>
Mostly complete. Although there are no I/O operators, all other member functions and string operations are available.
A workaround (besides implementing the << operator) would be asking the string instances for the C string:
for (vector<string>::iterator t = data.begin(); t != data.end(); ++t) {
cout << t->c_str() << endl;
}
This of course only works as long as the strings don't contain zero byte values.
When I compile your code, I get:
40234801.cpp:3:17: fatal error: stdio: No such file or directory
#include <stdio>
^
You clearly have a header called "stdio" in your include path that you haven't shown us.
If you change that line to the standard #include <iostream>, then the only reported error is that you wrote void main() instead of int main(). Fix that, and it will build and run.
In passing, note also that using namespace should be avoided.
I found a solution to my own issue. Instead of using a c_str, I used std::string and switched to using the G++ compiler instead of Open Watcom
Instead of having:
char *someString="Blah blah blah";
I instead replaced it with:
string someString="Blah blah blah";
This way is much more efficient and easier.

c++ Unable to output unicode characters even though I can write them directly

So thing is I can copy paste unicode characters like chess pieces directly to terminal( I'm using debian jessie linux) but whenever I write c++ code to do that, I get these � instead
here is my code
enter code here
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
setlocale(LC_ALL,"");
wchar_t piece='♗';
wcout<<piece;
}
I tried to use the hex or decimal code of the characters but it does not work
I also use vim to edit and it does show the characters while I'm typing.
There's no specification of what encoding should be used for wchar_t. I need to use mbstowcs function to convert that character. Like this, for example:
#include <iostream>
#include <clocale>
#include <cstdlib>
using namespace std;
int main(void) {
setlocale(LC_ALL, "");
wchar_t piece;
mbstowcs(&piece, "♗", 1);
wcout << piece << endl;
return 0;
}
assuming your source file encoding matches the encoding of your locale.
Oddly enough what worked was going at it normally and putting the special character into a string it's so ridiculously simple I didn't even think to use it.
#include<iostream>
using namespace std;
int main()
{
string piece="♗";
cout<<piece;
}

boost::trim_right_if and null characters

I have the following code:
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/assign/list_of.hpp>
#include <string>
#include <vector>
int main()
{
std::vector<char> some_vec = boost::assign::list_of('1')('2')('3')('4')('5')('\0')('\0');
std::string str(some_vec.begin(), some_vec.end());
boost::trim_right_if(str, boost::is_any_of("\0"));
}
I think that in str should be "12345", but there's "12345\0\0". Why and how can I solve it?
This code works
boost::trim_right_if(str, boost::is_any_of(boost::as_array("\0") );
The trick is to use boost::as_array
I do not know this functions boost::is_any_of but the fact that its argument is a string literal it seems it considers "\0" as an empty set of characters (en empty string literal). So the algorithm trims nothing.
It is only my supposition.

c++ string: is there good way to replace a char inside a string

I want to replace all the occurances of ' in a string to ^, but i saw string.replace is not the right function for me, do I need to write my own? It's boring.
You can use std::replace from <algorithm> instead of using string::replace from <string>
Sample code
#include <iostream>
#include <algorithm>
int main()
{
std::string s = "I am a string";
std::replace(s.begin(),s.end(),' ',',');
std::cout<< s;
}
Output : I,am,a,string