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. ATL / WTL / STL
  4. C++/MFC solution Convert string to integer only such that if it is alphanumeric or float it should show as invalid values.

C++/MFC solution Convert string to integer only such that if it is alphanumeric or float it should show as invalid values.

Scheduled Pinned Locked Moved ATL / WTL / STL
c++json
6 Posts 5 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
    Abinash Mohanty
    wrote on last edited by
    #1

    Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.

    L E S S 4 Replies Last reply
    0
    • A Abinash Mohanty

      Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      You could use strtol()[^] and its variants, as it will give you a pointer to the character that stops the scan of the string. You can then check this character and decide what actions to take. I do not know of any MFC class that offers a more comprehensive solution, but a search of the MFC library should tell you.

      One of these days I'm going to think of a really clever signature.

      1 Reply Last reply
      0
      • A Abinash Mohanty

        Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.

        E Offline
        E Offline
        evaj03
        wrote on last edited by
        #3

        Not sure if this is applicable to MFC, but the stringstream library will do this:

        #include
        #include

        int main( )
        {
        std::string inString( "10.88" );
        int outNum;

        std::stringstream ss( inString );

        ss >> outNum;

        std::cout << "As Number [" << outNum << "]" << std::endl;
        }

        Sample output would be: As Number [10]

        L 1 Reply Last reply
        0
        • E evaj03

          Not sure if this is applicable to MFC, but the stringstream library will do this:

          #include
          #include

          int main( )
          {
          std::string inString( "10.88" );
          int outNum;

          std::stringstream ss( inString );

          ss >> outNum;

          std::cout << "As Number [" << outNum << "]" << std::endl;
          }

          Sample output would be: As Number [10]

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          This will not work, you cannot read a floating point variable and correctly store it in an integer variable.

          One of these days I'm going to think of a really clever signature.

          1 Reply Last reply
          0
          • A Abinash Mohanty

            Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.

            S Offline
            S Offline
            SoMad
            wrote on last edited by
            #5

            You could simply use atoi() to convert to an integer, then format that value to a string and compare to your original string. Since you are not asking for anything super efficient or elegant, this should do the trick.

            CString str = "10.88";

            int nValue = atoi(str);

            CString strValue;
            strValue.Format("%d", nValue);
            if (strValue != str)
            {
            nValue = 0;
            }

            Soren Madsen

            1 Reply Last reply
            0
            • A Abinash Mohanty

              Hi All, I want to covert a string to integer in MFC.Consider the string as char str[10]; and integer as int num; My case is as following:- 1) Suppose str = "10" num should be = 10 2) Suppose str = "10.88" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. 3) Suppose str = "10ab" num should be 0 or any invalid number so that we can identify that its not an integer but any other value. Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values. Or I have to try it by creating my own method.

              S Offline
              S Offline
              Shivanand Gupta
              wrote on last edited by
              #6

              CString str=10.27; CString res = str.SpanIncluding(_T("0123456789")); if(res == str) { int nValue = atoi(str); } else { MessageBox("string is not integer"); } //Cheers

              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