I'm writing a C++ wxWidgets calculator application, and I need to store the characters for the operators in an array. I have something like int ops[10] = {'+', '-', '*', '/', '^'};
. What if I wanted to also store characters such as √
, ÷
and ×
in said array, in a way so that they are also displayable inside a wxTextCtrl
and a custom button?

- 555,201
- 31
- 458
- 770

- 73
- 2
- 7
-
wxWidgets natively uses `wxString` which supports both narrow and wide strings. For narrow strings, it uses the default locale, which is dangerous, so I'd recommend preferring to use wide strings/characters when possible. – Mooing Duck Jun 04 '21 at 18:03
2 Answers
This is actually a hairy question, even though it does not look like it at first. Your best option is to use Unicode Control Sequences instead of adding the special characters with your Source Code Editor.
wxString ops[]={L"+", L"-", L"*", L"\u00F7"};
You need to make sure that characters such as √, ÷ and × are being compiled correctly.
Your sourcefile (.cpp) needs to store them in a way that ensures the compiler is generating the correct characters. This is harder than it looks, especially when svn, git, windows and linux are involved.
Most of the time .cpp files are ANSI or 8 bit encoded and do not support Unicode constants out of the box.
You could save your sourcefile in UTF-8 codepage so that these characters are preserved. But not all compilers accept UTF-8
The best way to do that is to encode these using Unicode control characters. wxString div(L"\u00F7"); is the string for ÷. Or in your case perhaps wxChar div('\u00F7'). You have to look up the Unicode control sequences for the other special chars. This way your source file will contain ANSI chars only and will be accepted by all compilers. You will also avoid code page problems when you exchange source files with different OS platforms.
Then you have to make sure that you compile wxWidgets with UNICODE awareness (although I think this is the default for wx3.x). Then, if your OS supports it, these special characters should show up.
Read up on Unicode controls (Wikipedia). Also good input is found in utf8everywhere.org. The file .editorconfig
can also be of help.

- 1,969
- 8
- 17
-
I followed your instructions and wrote wxString ops[10] = { L"+", L"-", L"U+00D7", L"U+00F7", L"^", L"U+221A" }; Then I tried displaying the multiplication symbol on a wxTextCtrl using ExpText->SetLabel(ops[2]); to see if it was working, and instead of displaying the symbol it literally displayed U+00D7. – iKebab897 Jun 05 '21 at 08:46
-
Try wxString div=L"\u221A". U+221A is the standard way of writing this in normal texts. In a C literal it should probably be \u instead of U+ . See "Unicode Support in wxWidgets". – Hajo Kirchhoff Jun 05 '21 at 09:45
-
It works now, but I'm having trouble comparing the wxString from ops array to single characters from the wxTextCtrl string. I have [this function](https://pasteboard.co/K5c1Cf7.png) that only return `true` on + symbol for some reason. [Here's the code that I use to call it.](https://pasteboard.co/K5c2i5K.png) – iKebab897 Jun 05 '21 at 11:37
-
What is ops->length() for? Does this return the size of the array or the length of the first string in the array? If ops is defined as in your example above, ops->length() will dereference the first string in the array and call `length` for this string. (same as `ops[0].length`). Probably not what you wanted :) You could use modern C++: ``` std::vector
ops={"+", "-", "\u00D7"...}; for (const auto &j:ops) { if (j==query) return true; } return false; } ``` or at least use `std::size(ops)` to retrieve the size of the array. – Hajo Kirchhoff Jun 05 '21 at 18:22 -
*Note:* length is usually used with strings, whereas size is usually used with arrays. – Hajo Kirchhoff Jun 05 '21 at 18:22
Prefer to use wchar_t
instead of int
.
wchar_t ops[10] = {L'+', L'-', L'*', L'/', L'^', L'√', L'÷', L'×'};
These trivially support the characters you describe, and trivially and correctly convert to wxString
s.

- 64,318
- 19
- 100
- 158
-
-
@MooingDuck, also in the cross-platform environment you need to understand the proper encoding... – Igor Jun 04 '21 at 19:45
-
@Igor: usually not, with wide characters. It occasionally comes up with emoji, but that's not common. – Mooing Duck Jun 04 '21 at 20:21
-
@MoolingDuck, what I mean is - when woring in Windows vs Linux, you need to be careful, because on Windows (IIUC), std::string is already UNICODE aware, while on Linux it is not. Or vice versa. And that's why std::wstring was invented. Which IIUC, is based off of wchar_t, while std::string is based on char *. – Igor Jun 04 '21 at 22:14
-
Linux uses UTF8 everywhere, including in `std::string`, and Windows uses the current locale's code page. `std::wstring` and `wchar_t` are UTF16 on Windows and UCS32 on Linux. None of that should relate to this post directly, but it's good for people to be aware. – Mooing Duck Jun 04 '21 at 22:33