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

CString

Scheduled Pinned Locked Moved C / C++ / MFC
question
14 Posts 9 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.
  • S Offline
    S Offline
    speedy4711
    wrote on last edited by
    #1

    Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks

    H _ J K D 7 Replies Last reply
    0
    • S speedy4711

      Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #2

      See

      isdigit();

      How to read your string?

      _**


      **_

      WhiteSky


      K _ 2 Replies Last reply
      0
      • S speedy4711

        Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks

        _ Offline
        _ Offline
        _AnsHUMAN_
        wrote on last edited by
        #3

        speedy4711 wrote:

        CString contains only numbers?

        CString str="1142654";
        int i=atoi (str);
        CString str1;str1.Format("%d",i);
        if(str.GetLength ()==str1.GetLength ())
        	AfxMessageBox("string is pure number");
        else 
        	AfxMessageBox ("Characters exist");
        

        Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

        S 1 Reply Last reply
        0
        • H Hamid Taebi

          See

          isdigit();

          How to read your string?

          _**


          **_

          WhiteSky


          _ Offline
          _ Offline
          _AnsHUMAN_
          wrote on last edited by
          #4

          WhiteSky wrote:

          atoi(m_Str);//return value is 0

          If the string is as 12a3 then Index would be 12 and not 0 inspite of the fact that there exists a character in the third place.

          Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

          1 Reply Last reply
          0
          • H Hamid Taebi

            See

            isdigit();

            How to read your string?

            _**


            **_

            WhiteSky


            K Offline
            K Offline
            kakan
            wrote on last edited by
            #5

            CString m_Str; m_Str="123ABC"; Index=atoi(m_Str);//return value is 123 Gives 123 too, and m_Str doesn't contain only numbers...

            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

            1 Reply Last reply
            0
            • S speedy4711

              Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #6

              Here's a small function I use for validating user input:

              bool IsNumeric(const CString& str)
              {
              if(str.IsEmpty())
              return false;

              for(int i = 0; i < str.GetLength(); i++)
              	if(str\[i\] < '0' || str\[i\] > '9')
              		return false;
              
              return true;
              

              }

              It only takes decimal non-negative integers into account. It's easy to extend for other types of numbers, should you want to.

              -- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!

              S 1 Reply Last reply
              0
              • S speedy4711

                Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks

                _ Offline
                _ Offline
                _AnsHUMAN_
                wrote on last edited by
                #7

                speedy4711 wrote:

                test if a CString contains only numbers?

                here's one more solution. You can give this a try...

                CString s="253453";
                CComVariant v;
                v=s;
                if(v.ChangeType(VT_INT)==S_OK)
                {
                	CString str;str.Format ("%d",v.intVal );
                	AfxMessageBox (str);
                }
                

                Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                1 Reply Last reply
                0
                • S speedy4711

                  Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks

                  K Offline
                  K Offline
                  KarstenK
                  wrote on last edited by
                  #8

                  I looked over the responses, and I want to point to two possible problems. The first may be a leading Zero ( "0123" ) and the other is a value with a comma or a sign ( "-1,5" or "+ 2.7" ) I would resolve this with checking every char of being a digit or allowed other char.

                  Greetings from Germany

                  1 Reply Last reply
                  0
                  • _ _AnsHUMAN_

                    speedy4711 wrote:

                    CString contains only numbers?

                    CString str="1142654";
                    int i=atoi (str);
                    CString str1;str1.Format("%d",i);
                    if(str.GetLength ()==str1.GetLength ())
                    	AfxMessageBox("string is pure number");
                    else 
                    	AfxMessageBox ("Characters exist");
                    

                    Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                    S Offline
                    S Offline
                    SilentSilent
                    wrote on last edited by
                    #9

                    _AnShUmAn_ wrote:

                    if(str.GetLength ()==str1.GetLength ())

                    Doesn't work with eg. CString str="0x1142654";

                    _ 1 Reply Last reply
                    0
                    • J Jorgen Sigvardsson

                      Here's a small function I use for validating user input:

                      bool IsNumeric(const CString& str)
                      {
                      if(str.IsEmpty())
                      return false;

                      for(int i = 0; i < str.GetLength(); i++)
                      	if(str\[i\] < '0' || str\[i\] > '9')
                      		return false;
                      
                      return true;
                      

                      }

                      It only takes decimal non-negative integers into account. It's easy to extend for other types of numbers, should you want to.

                      -- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!

                      S Offline
                      S Offline
                      SilentSilent
                      wrote on last edited by
                      #10

                      Jörgen Sigvardsson wrote:

                      for(int i = 0; i < str.GetLength(); i++) if(str[i] < '0' || str[i] > '9') return false;

                      Doesn't work with eg. CString str="0x1142654";

                      J 1 Reply Last reply
                      0
                      • S SilentSilent

                        Jörgen Sigvardsson wrote:

                        for(int i = 0; i < str.GetLength(); i++) if(str[i] < '0' || str[i] > '9') return false;

                        Doesn't work with eg. CString str="0x1142654";

                        J Offline
                        J Offline
                        Jorgen Sigvardsson
                        wrote on last edited by
                        #11

                        How often do you accept hexadecimal user input? And how hard would it be to extend it? I never claimed it would recognize all possible numeric syntaxes...

                        -- Mit viel Oktan und frei von Blei, eine Kraftstoff wie Benziiiiiiin!

                        1 Reply Last reply
                        0
                        • S SilentSilent

                          _AnShUmAn_ wrote:

                          if(str.GetLength ()==str1.GetLength ())

                          Doesn't work with eg. CString str="0x1142654";

                          _ Offline
                          _ Offline
                          _AnsHUMAN_
                          wrote on last edited by
                          #12

                          SilentSilent wrote:

                          Doesn't work with eg.

                          Since this value you are entering is a string x is treated as a character only, it doesn't represent a hexadecimal number nor did I check for this.So I would try and provide a solution for this... Thanks for pointing this out...

                          Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

                          1 Reply Last reply
                          0
                          • S speedy4711

                            Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks

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

                            Why don't you just use strtol()?


                            "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

                            "Judge not by the eye but by the heart." - Native American Proverb

                            1 Reply Last reply
                            0
                            • S speedy4711

                              Hi, is there an easy way (a function) to test if a CString contains only numbers? Thanks

                              Z Offline
                              Z Offline
                              Zac Howland
                              wrote on last edited by
                              #14

                              Here is a base-10 checker that allows for ,'s and -'s (but doesn't check for placement). You can easily extend it to check for proper placement of special characters (,'s, and -'s) as well as other base numbers and floating point numbers.

                              // function to check for base-10 digits only
                              bool is_digit_base10(const CString& str)
                              {
                              	static CString allowedChars = _T("0123456789,-");
                              	const unsigned int length = str.GetLength();
                              	for (unsigned int i = 0; i < length; ++i)
                              	{
                              		char buffer[2] = {0};
                              		buffer[0] = str[i];
                              		if (-1 == allowedChars.FindOneOf(buffer))
                              		{
                              			return false;
                              		}
                              	}
                              	return true;
                              }
                              

                              If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac

                              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