Better error checking using sscanf
-
So I am trying to take user input from an editbox for my dialog based app. The value should be a single float. I retrieve the value using GetWindowText() and sscanf(). I check that sscanf returns 1 to show that it in fact succesfully scanned in one
CString ValueStr; float ValueFloat; GetDlgItem(IDC_EDITBOX)->GetWindowText(ValueStr); if (1 == sscanf(ValueStr, "%f", ValueFloat)) { //valid data, processing it... } else { MessageBox("Invalid input value."); }
The problem is that this method of error checking is not the best. For example, if the user enters "20.5p", that will not flag an error, but instead will just take 20.5 as a valid value and ignore "p". How can I do more thorough error checking, such that I check to make sure the input is ONLY a single float and no trailing characters? Thanks -
So I am trying to take user input from an editbox for my dialog based app. The value should be a single float. I retrieve the value using GetWindowText() and sscanf(). I check that sscanf returns 1 to show that it in fact succesfully scanned in one
CString ValueStr; float ValueFloat; GetDlgItem(IDC_EDITBOX)->GetWindowText(ValueStr); if (1 == sscanf(ValueStr, "%f", ValueFloat)) { //valid data, processing it... } else { MessageBox("Invalid input value."); }
The problem is that this method of error checking is not the best. For example, if the user enters "20.5p", that will not flag an error, but instead will just take 20.5 as a valid value and ignore "p". How can I do more thorough error checking, such that I check to make sure the input is ONLY a single float and no trailing characters? ThanksAs far as I know you will have to do this yourself. You will have to write a function which parses the input one char at a time and check if it is valid number. -Saurabh
-
So I am trying to take user input from an editbox for my dialog based app. The value should be a single float. I retrieve the value using GetWindowText() and sscanf(). I check that sscanf returns 1 to show that it in fact succesfully scanned in one
CString ValueStr; float ValueFloat; GetDlgItem(IDC_EDITBOX)->GetWindowText(ValueStr); if (1 == sscanf(ValueStr, "%f", ValueFloat)) { //valid data, processing it... } else { MessageBox("Invalid input value."); }
The problem is that this method of error checking is not the best. For example, if the user enters "20.5p", that will not flag an error, but instead will just take 20.5 as a valid value and ignore "p". How can I do more thorough error checking, such that I check to make sure the input is ONLY a single float and no trailing characters? ThanksHave you considered a masked edit control?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
So I am trying to take user input from an editbox for my dialog based app. The value should be a single float. I retrieve the value using GetWindowText() and sscanf(). I check that sscanf returns 1 to show that it in fact succesfully scanned in one
CString ValueStr; float ValueFloat; GetDlgItem(IDC_EDITBOX)->GetWindowText(ValueStr); if (1 == sscanf(ValueStr, "%f", ValueFloat)) { //valid data, processing it... } else { MessageBox("Invalid input value."); }
The problem is that this method of error checking is not the best. For example, if the user enters "20.5p", that will not flag an error, but instead will just take 20.5 as a valid value and ignore "p". How can I do more thorough error checking, such that I check to make sure the input is ONLY a single float and no trailing characters? ThanksYou can set a property of the TextBox, allowing only the input of float numbers. Then users can't input letters into the TextBox. Sorry I can't provide the details right now. Or if you are only dealing with float numbers, maybe atof() will help. It will reject any input that is not a valid float.
-
You can set a property of the TextBox, allowing only the input of float numbers. Then users can't input letters into the TextBox. Sorry I can't provide the details right now. Or if you are only dealing with float numbers, maybe atof() will help. It will reject any input that is not a valid float.
fefe.wyx wrote:
You can set a property of the TextBox, allowing only the input of float numbers.
Maybe this is a feature of VS200x, because VS6 has no such property for edit controls.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Have you considered a masked edit control?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
not quite sure what a masked edit control is. But reason I didnt fix the editbox to a certain datatype is because I reuse that editbox for several different inputs. Sometimes I want a float as in the example above. Sometimes I want HEX, in which case, I do error checking like so:
if (1 == sscanf(ValueStr, "%X", ValueInt))
Sometimes I want int. etc... Which is why I kept input free from any restricted datatype, and handle input on a case by case basis. -
fefe.wyx wrote:
You can set a property of the TextBox, allowing only the input of float numbers.
Maybe this is a feature of VS200x, because VS6 has no such property for edit controls.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
I am using VS6 also. I believe fefe.wyx meant using class wizard to assign a float variable to the edit control. Then it will be restricted to that type, and to retrieve data you can call UpdateData(true). But I can't use this method because of the reason in my above reply.
-
As far as I know you will have to do this yourself. You will have to write a function which parses the input one char at a time and check if it is valid number. -Saurabh
I you don't mind using a library then boost have lexical_cast and numeric_cast which will solve your problem. lexical_cast[^] -Saurabh
-
So I am trying to take user input from an editbox for my dialog based app. The value should be a single float. I retrieve the value using GetWindowText() and sscanf(). I check that sscanf returns 1 to show that it in fact succesfully scanned in one
CString ValueStr; float ValueFloat; GetDlgItem(IDC_EDITBOX)->GetWindowText(ValueStr); if (1 == sscanf(ValueStr, "%f", ValueFloat)) { //valid data, processing it... } else { MessageBox("Invalid input value."); }
The problem is that this method of error checking is not the best. For example, if the user enters "20.5p", that will not flag an error, but instead will just take 20.5 as a valid value and ignore "p". How can I do more thorough error checking, such that I check to make sure the input is ONLY a single float and no trailing characters? ThanksThis is what MFC does to find the input text is float or not..( from DLGFLOAT.CPP )
AFX_STATIC BOOL AFXAPI _AfxSimpleFloatParse(LPCTSTR lpszText, double& d)
{
ASSERT(lpszText != NULL);
while (*lpszText == ' ' || *lpszText == '\t')
lpszText++;TCHAR chFirst = lpszText\[0\]; d = \_tcstod(lpszText, (LPTSTR\*)&lpszText); if (d == 0.0 && chFirst != '0') return FALSE; // could not convert while (\*lpszText == ' ' || \*lpszText == '\\t') lpszText++; if (\*lpszText != '\\0') return FALSE; // not terminated properly return TRUE;
}
nave [OpenedFileFinder]
-
So I am trying to take user input from an editbox for my dialog based app. The value should be a single float. I retrieve the value using GetWindowText() and sscanf(). I check that sscanf returns 1 to show that it in fact succesfully scanned in one
CString ValueStr; float ValueFloat; GetDlgItem(IDC_EDITBOX)->GetWindowText(ValueStr); if (1 == sscanf(ValueStr, "%f", ValueFloat)) { //valid data, processing it... } else { MessageBox("Invalid input value."); }
The problem is that this method of error checking is not the best. For example, if the user enters "20.5p", that will not flag an error, but instead will just take 20.5 as a valid value and ignore "p". How can I do more thorough error checking, such that I check to make sure the input is ONLY a single float and no trailing characters? ThanksCould you do...
if (1 == sscanf (ValueStr, _T("%f%s"), ValueFloat, sometcharbuffer);
{
...
}
else
{
...
}? You may have to check for a return value of 2, and an empty overflow buffer, but I leave that as an exercise for the reader. Iain.
Iain Clarke appears because CPallini still cares.
-
not quite sure what a masked edit control is. But reason I didnt fix the editbox to a certain datatype is because I reuse that editbox for several different inputs. Sometimes I want a float as in the example above. Sometimes I want HEX, in which case, I do error checking like so:
if (1 == sscanf(ValueStr, "%X", ValueInt))
Sometimes I want int. etc... Which is why I kept input free from any restricted datatype, and handle input on a case by case basis.In this case, I would use three separate masked edit controls, and hide the two that aren't being used.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne