C++ Store Edit Box Value Into A Int - c++

I need to Read the value that is typed into a EditBox and save into into a Int.
Does anyone know how to do this?? this is the code i have so far..
case EditAge: {
if (HIWORD(wParam) == EN_CHANGE)

Well, that's a start, but did you even try to retrieve the text? Or do you have issues converting the text to a number? For future questions, try to be more specific. You're most likely looking for something like this line (don't forget to check to see if you're in the right text box!):
GetDlgItemText(dialoghandle, LOWORD(wParam) /* the control id */, text /* pointer where the text should go */, 256 /* max number of elements */);
Once you've got the text, you can use e.g. strtol() to parse the string and retrieve a number.
Edit:
You could actually use GetDlgItemInt(), documented here, to retrieve an integer right away.

Related

Unicode and Text Controls Not Converting to UTF8 correctly

there. I am trying to pass a Text Field contents into a database call (mysql). When I look at the field that gets returned from Text->getvalue.(funct) call - I simply do not get the text that was entered into the field - via any of the UTF functions in WXWidgets. I have tried the following:
wxChar buffer = ((wxTextCtrl*)FindWindow(wxID_TITLE))->GetValue().mb_str(wxConvUTF8);
//GooglyGook for whole thing
wxChar buffer = ((wxTextCtrl*)FindWindow(wxID_TITLE))->GetValue().mb_str();
//NULL it fails completely
wxChar buffer = ((wxTextCtrl*)FindWindow(wxID_TITLE))->GetValue().ToUTF8();
//More GOoblygook
wxChar buffer = ((wxTextCtrl*)FindWindow(wxID_TITLE))->GetValue().utf8_str();
//More Gooblygook
message.Printf(_T("The title saved as wxCharBuffer =%s"),buffer.data());
wxMessageBox(message,_("Rivendell"), wxICON_ERROR|wxOK);
The message box is how I am trying to display what is in the wxChar buffer,
but I am running in debug so I can simply look at it during the run and confirm that it is incorrect. Please note that I have tried these wxChar buffer lines one at a time separately (not like they are listed here). Just wanted to show things I had tried.
What is the correct way to do this? The type of characters I am attempting to save in the db looks like:"check Todd 1 乞: 乞丐 qǐgài, 乞求 qǐqiú, 乞讨 qǐtǎo."
The gooblygook output looks like Chineese characters etc.. even in the English part of the field (Check Todd)...
Anyone who has an idea of how to do this please let me know. Thanks...
Tb
I appreciate the help provided, and after trying some things I found an answer.
The correct way to do this seems to be the following: Put the TextCtrl field into a wxString using wx_str(). Then put the wxString into a wxCHarBuffer via toUTF8() function. Then use the data() function of the wxCHarBuffer to pass a char pointer.
Part of my problem stemmed from trying to display what was in those fields via Visual Studio Debugger, and/or wxMessage boxes - so sometimes my conversions were wrong (as noted by the previous poster).
I was able to set the wxString (s) to Unicode characters and have it be handled correctly by the mysql call (i.e. Call would crash before). The chk_title variable returned seems to be correctly encoded into UTF8 and escaped.
Thanks.

Does Serial.find Clears Buffer If It Can't Find Anything

I'm trying to look for keywords in serial buffer in Arduino.
if (Serial.find("SOMETHING"))
{
// do something
}
else if (Serial.find("SOMETHING ELSE"))
{
// do another thing
}
But only the first if works. Even if I send "SOMETHINGELSE" it doesn't check at all. Does find function clear buffer completely even if it can't find anything ? If yes, what can i do in this situation?
Serial.find(); reads Serial buffer and removes every single byte from it, up to the point where it can find specified by you String or Character.
If you use it in an conditional statement like in your example, it will always find "SOMETHING" even if "SOMETHING ELSE" exist because everything up to the point of "SOMETHING" is removed from buffer ( if "SOMETHING" actually arrived before "SOMETHING ELSE" ).
If we assume that your data arrives in order SOMETHING and then SOMETHING ELSE, your Serial buffer will look like this: SOMETHING ELSESOMETHING
in which case:
It will find "SOMETHING" and stop in there as first condition to meet is to search for this word exactly.
I assumed that you don't actually mean to send "SOMETHING" so lets say that first String to look for is StringA and then StringB. Your buffer then will look like this: StringBStringA however based on your conditional statement it will still only find StringA. This will happen because StringA still exist in buffer and now when first condition is checked you basically ask to search for StringA and by doing this you are removing StringB using Serial.find(StringA) - it simply skips StringB because its not aware that you are going to ask about it in your else if later on.
Solution to your problem depends on data that you expect to receive. You can tag beginning of data that you are awaiting for with some specific character or sequence of characters:
For example lets assume that you await for String type of data. Before you send it to your Serial put each message in a specific format like $START$SOMETHING$
You can then use this to find first command that starts with your tag and load content of the message to String so you can compare it with expected results using conditional statement.
Note!!! Code below will stop on first message with $START$ tag so if you want to look into your Serial buffer for other messages you don't want to break while(Serial.available > 0) and use arrays to store each result.
char myCharacter;
String myIncomingData;
if(Serial.find("$START$"))
{
while (Serial.available() > 0) {
// Reads byte of Serial at the time
myCharacter = Serial.read();
// Stops at the end of data
if (myCharacter == "$") {
break;
}
// Adds each character to String with your data
myIncomingData += myCharacter;
}
if (myIncomingData == "SOMETHING") {
// Do whatever you like to with your data
} else if (myIncomingData == "SOMETHING ELSE") {
// Do whatever you like to with your data
}
I would use this solution only if you want to use Serial.find(), Im sure that you can get your results in many different ways as well, at the end you can always go through entire 64 bytes of you buffer byte by byte using your own code :D.

Delete content in a text file between two specific characters

I'm making a simple bug tracker and am using a text file as the database. Right now I'm reading in all the information through keys and importing them into specific arrays.
for (int i = 0; i < 5; i++)
{
getline(bugDB, title[i], '#');
getline(bugDB, importance[i], '!');
getline(bugDB, type[i], '$');
getline(bugDB, description[i], '*');
}
Here is what's in my (terribly unreadable) file
Cant jump#Moderate!Bug$Every time I enter the cave of doom, I'm unable
to jump.*Horse too expensive#Moderate!Improvement$The horses cost way
too much gold, please lower the costs.*Crash on startup#Severe!Bug$I'm
crashing on startup on my Win8.1 machine, seems to be a 8.1
bug.*Floating tree at Imperial March#Minimal!Bug$There is a tree
floating about half a foot over the ground near the crafting
area.*Allow us to instance our group#Moderate!Improvement$We would
like a feature that gives us the ability to play with our groups alone
inside dungeons.*
Output:
This works great for me, but I'd like to be able to delete specific bugs. I'd be able to do this by letting the user choose a bug by number, find the corresponding * key, and delete all information until the program reaches the next * key.
I'd appreciate any suggestions, I don't know where to start here.
There is no direct mechanism for deleting some chunk of data from the middle of the file, no delete(file, start, end) function. To perform such a deletion you have to move the data which appears after the region; To delete ten bytes from the middle of a file you'd have to move all of the subsequent bytes back ten, looping over the data, then truncate to make the file ten bytes smaller.
In your case however, you've already written code to parse the file into memory, populating your arrays. Why not just implement a function to write the contents of the arrays back to a file? Truncate the file (open in mode "w" rather than "w+"), loop over the arrays writing their contents back to the file in your preferred format, but skip the entry that you want to delete.
its only possible by manually copying the data from input file to output file and leaving out the entry you want to delete.
but: i strongly encourage the usage of some small database for keeping the informations (look at sqlite)
Also its a bad bugtracker if solving the bug means "delete it from database" (its not even is a tracker). give it a status field (open, refused, duplicate, fixed, working, ...).
Additional remarks:
use one array that keeps some structure with n informations and not n arrays.
please remind that someone may use your delimiter characters in the descriptions (use some uncommon character and replace its usage in saved text)
explanation for 1.:
instead of using
std::vector<std::string> title;
std::vector<int> importance;
std::vector<std::string> description;
define a structure or class and create a vector of this structure.
struct Bug{
std::string title;
int importance; // better define an enum for importance
std::string description;
};
std::vector<Bug> bugs;

Adding a string to a listbox results in weird characters

I have made a function that send strings to listbox using WIN32
char data[] = "abcd";
addToList(hWnd,data);
void addToList(HWND hWnd,char data[] ){
SendMessage(GetDlgItem(hWnd,IDC_LISTBOX),LB_ADDSTRING,0,(LPARAM)data);
}
when I execute this it's send data to list box but the problem they appeared in weird characters, I have tried wchar_t also but the problem still issued
First of all, you should be checking your API calls for errors. You need to check the return values of all your calls to API functions.
That said, given the code in the question,
SendMessage(GetDlgItem(hWnd,IDC_LISTBOX),LB_ADDSTRING,0,(LPARAM)data);
If that results in an item being added to the list box, then it means that GetDlgItem did indeed return a valid window handle, and data did indeed point to valid memory. In which case the only explanation for what you report is that the text encoded did not match.
So, we can assume that the SendMessage macro evaluates to SendMessageW. And since you are passing ANSI encoded text, that mismatch explains the symptoms. The function treats the text as UTF-16 encoded.
One obvious solution is to use SendMessageA instead. However, a better solution, in my view, would be to pass UTF-16 encoded data.
wchar_t data[] = L"abcd";
....
void addToList(HWND hWnd, const wchar_t *data)
{
SendMessage(GetDlgItem(hWnd,IDC_LISTBOX), LB_ADDSTRING, 0, (LPARAM)data);
}
Obviously your code would add in the error checking that I mentioned at the start.

How to get displayed width of a string?

When you have non-fixed width characters (such as \t) in a string , or escape codes, such as those for ANSI color (such as \1xb[31m), these characters add to the .length() of an std::string, but do not add to the displayed length when printed.
Is there any way in C++ to get the displayed width of a string in *nix?
For instance:
displayed_width("a\tb") would be 4 if the displayed tab width is 2
displayed_width("\1xb[33mGREEN") would be 5
Most commonly, a tab asks the terminal program to move the cursor to a column that's a multiple of 8, though many terminal programs let you configure that. With such behaviour, how much width a tab actually adds depends on where the cursor was beforehand relative to the tab stops. So, simply knowing the string content is not enough to calculate a printable width without some assumption or insight regarding prior cursor placement and tab stops.
Non-printable codes also vary per terminal type, though if you only need ANSI colour then that's pretty easy. You can move along the string counting characters; when you see an ESCAPE skip through to the terminating m. Something like (untested):
int displayed_width(const char* p)
{
int result = 0;
for ( ; *p; ++p)
{
if (p[0] == '\e' && p[1] == '[')
while (*p != 'm')
if (*p)
++p;
else
throw std::runtime_error("string terminates inside ANSI colour sequence");
else
++result;
}
return result;
}
Nothing built in. The "displayed width" of the tab character is an implementation detail, as are console escape sequences. C++ doesn't care about platform-specific things like that.
Is there something in particular you're trying to do? We may be able to suggest alternatives if we know what particular task you're working on.
Not with standard methods to my knowledge. C++ does not know about terminals.
My guess would be to use NCURSES for that. Dunno if boost has something up the sleeve for that though.
Display length on what device? A console that uses a fixed-width font? A window that uses a proportional font? This is highly device-dependent question. There is no fixed answer. You will have to use the tools associated with the target output device.