Determine if a string is composed of numbers or characters
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: Determine if a string is composed of numbers or characters

  1. #1
    Join Date
    Nov 2009
    Posts
    10

    Determine if a string is composed of numbers or characters

    I have pulled a string from an input, and I have to execute a function on it depending on if the string is numbers or characters. I tried making a function file that can determine it, however I am getting an illegal expression error C2451:

    Code:
    int chooseMethod (string testStr){
    
    for (int I = 0; I<10; I++){
    	if (testStr.substr(1,1) = I){
    		return 2;}}
    
    return 1;
    
    }
    Then based on the return I use the corresponding function. The error lies within
    Code:
    if (testStr.substr(1,1) = I)
    . What am I doing wrong, or is there a common/easier way to do this?
    Last edited by HeliPilot; December 7th, 2009 at 02:05 PM.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,108

    Re: Determine if a string is composed of numbers or characters

    You want ==, not = for comparison, but I'm not sure what the point of comparing anything to I is.

  3. #3
    Join Date
    Nov 2009
    Posts
    10

    Re: Determine if a string is composed of numbers or characters

    Quote Originally Posted by GCDEF View Post
    You want ==, not = for comparison, but I'm not sure what the point of comparing anything to I is.
    I get the same error, just more of them when I use ==.

    The idea was to compare the first string character to the numbers 0,1,2,3...9 and if it matched any of them, the string is composed of numbers.

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,108

    Re: Determine if a string is composed of numbers or characters

    Quote Originally Posted by HeliPilot View Post
    I get the same error, just more of them when I use ==.

    The idea was to compare the first string character to the numbers 0,1,2,3...9 and if it matched any of them, the string is composed of numbers.
    You want to compare every character, not just the first to determine if it's composed of numbers.

    You could use the isdigit() function, or check if each char is >= 0 and <= 9.

  5. #5
    Join Date
    Nov 2009
    Posts
    10

    Re: Determine if a string is composed of numbers or characters

    Quote Originally Posted by GCDEF View Post
    You want to compare every character, not just the first to determine if it's composed of numbers.

    You could use the isdigit() function, or check if each char is >= 0 and <= 9.
    Well since this is with an ideal input, if the first character is a number, then they will all be numbers. So would I still have to go through every character?

    I tried the >=0 and <=9 and it just gives me more errors on build:
    Code:
    could not deduce template argument for 'const _Elem *' from 'int'

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,108

    Re: Determine if a string is composed of numbers or characters

    You'll need to show the code if you want help with errors.

  7. #7
    Join Date
    Nov 2009
    Posts
    10

    Re: Determine if a string is composed of numbers or characters

    Quote Originally Posted by GCDEF View Post
    You'll need to show the code if you want help with errors.
    This is after attempting isdigit():

    Code:
    int chooseMethod (string testString){
    
    
    	if (isdigit(testString.substr(1,1))){
    		return 1;}
    
    return 2;
    
    }
    Error:
    Code:
    1>c:\documents and settings\asdfasdf\desktop\example.cpp(325) : error C2664: 'isdigit' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Ax>' to 'int'

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,108

    Re: Determine if a string is composed of numbers or characters

    substr returns a string. isidigit wants a char.

  9. #9
    Join Date
    Nov 2009
    Posts
    10

    Re: Determine if a string is composed of numbers or characters

    Quote Originally Posted by GCDEF View Post
    substr returns a string. isidigit wants a char.
    Is there a way to pull out the first character of a string as a char? Or even convert string to char?

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,108

    Re: Determine if a string is composed of numbers or characters

    Quote Originally Posted by HeliPilot View Post
    Is there a way to pull out the first character of a string as a char? Or even convert string to char?
    You may want to look at the c_str() method of string.

  11. #11
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,526

    Re: Determine if a string is composed of numbers or characters

    ... or something like that:
    Code:
    string s("qwerty");
    isdigit(s[0]);
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  12. #12
    Join Date
    Nov 2009
    Posts
    10

    Re: Determine if a string is composed of numbers or characters

    Thanks guys.

    I just ended up using peek() to look at the first part of the string before actually pulling it out as a string.

  13. #13
    Join Date
    Apr 1999
    Posts
    27,431

    Re: Determine if a string is composed of numbers or characters

    Quote Originally Posted by HeliPilot View Post
    I just ended up using peek()
    What is this "peek" function? There is no such function defined in Visual C++.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center