Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Better error checking using sscanf

Better error checking using sscanf

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiontutorial
11 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    acerunner316
    wrote on last edited by
    #1

    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

    S D F N I 5 Replies Last reply
    0
    • A acerunner316

      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

      S Offline
      S Offline
      Saurabh Garg
      wrote on last edited by
      #2

      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

      S 1 Reply Last reply
      0
      • A acerunner316

        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

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        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

        A 1 Reply Last reply
        0
        • A acerunner316

          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

          F Offline
          F Offline
          fefe wyx
          wrote on last edited by
          #4

          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.

          D 1 Reply Last reply
          0
          • F fefe wyx

            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.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            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

            A 1 Reply Last reply
            0
            • D David Crow

              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

              A Offline
              A Offline
              acerunner316
              wrote on last edited by
              #6

              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.

              D 1 Reply Last reply
              0
              • D David Crow

                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

                A Offline
                A Offline
                acerunner316
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • S Saurabh Garg

                  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

                  S Offline
                  S Offline
                  Saurabh Garg
                  wrote on last edited by
                  #8

                  I you don't mind using a library then boost have lexical_cast and numeric_cast which will solve your problem. lexical_cast[^] -Saurabh

                  1 Reply Last reply
                  0
                  • A acerunner316

                    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

                    N Offline
                    N Offline
                    Naveen
                    wrote on last edited by
                    #9

                    This 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]

                    1 Reply Last reply
                    0
                    • A acerunner316

                      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

                      I Offline
                      I Offline
                      Iain Clarke Warrior Programmer
                      wrote on last edited by
                      #10

                      Could 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.

                      1 Reply Last reply
                      0
                      • A acerunner316

                        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.

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups