I am making an application using mfc that has a dialog box used to sort data in a spreadsheet. The dialog contains several controls including edit boxes. when the user clicks the OK button, the dialog should retrieve their preferences and close.
When getting the information from the dialog boxes, the strings containing the information are always registering as empty. I set a breakpoint at the location that the strings are located at, and the values seem to be found fine, but when I call any function to determine the size of the strings, it shows as 0.
MinVal = atof(min.c_str());
MaxVal = atof(max.c_str());
AfxMessageBox(_T("Please Enter A Minimum Value"));
AfxMessageBox(_T("Please Enter A Maximum Value"));
This is the error.
std::string min; MinEdit.GetWindowText(&min, 20);
The string has no length, and you ask GetWindowText to write into that buffer. That's an overrun and it causes undefined behavior.
Instead, allocate some space in the string, then resize it afterwards:
std::string min; min.resize(20); min.resize(MinEdit.GetWindowText(&min, min.size()));