Thread: Maybe A Cast Problem?

  1. #16
    Registered User
    Join Date
    May 2006
    Posts
    903
    An integer cannot have characters. What do you want ? You want to find if a certain digit is in a certain number ?
    Code:
    // 1 means found
    // 0 means not found
    // -1 means error
    int search_digit(int num, int digit)
    {
      if(num == 0 && digit == 0) return 1;
      if(digit >= 10 || digit < 0) return -1;
      while(num > 0)
      {
        if((num % 10) == digit) return 1;
        else num /= 10;
      }
      return 0;
    }

  2. #17
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    You don't even need a switch for that. It can be accomplished in a simple pair of if statements:

    Code:
    	if ( numberhold >= 'A' && numberhold <= 'Z' )
    		std::cout<<"uppercase character is "<<static_cast<char>(numberhold)<<std::endl;
    	else if ( numberhold >= 'a' && numberhold <= 'z' )
    		std::cout<<"lowercase character is "<<static_cast<char>(numberhold)<<std::endl;

  3. #18
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by Desolation
    An integer cannot have characters. What do you want ? You want to find if a certain digit is in a certain number ?
    Code:
    int search_digit(int num, int digit)
    {
      if(digit >= 10 || digit < 0) return -1;
      while(num > 0)
      {
        if((num % 10) == digit) return 1;
        else num /= 10;
      }
      return 0;
    }
    integers can be represented as characters, by interpreting them with their equivelant ascii value; http://www.lookuptables.com/

  4. #19
    Me
    Join Date
    Jul 2006
    Posts
    71
    No, sorry I explained it wrong. I want to see if the user inputs any thing other than an integer it says an error message. Hold on I have an idea.

    Edit:Nope I tried this but it gives a fatal error every time and crashes the program.

    Code:
           if(isdigit(numberhold)!=1)
            {
             cout<<"Error, Please Input A Whole Number Between 1 and 5";
            }
    Last edited by relyt_123; 07-29-2006 at 10:55 PM.

  5. #20
    Registered User
    Join Date
    May 2006
    Posts
    903
    I know that. However, integers cannot contain characters. That is my point.
    Last edited by Desolation; 07-29-2006 at 10:52 PM.

  6. #21
    Registered User
    Join Date
    May 2006
    Posts
    903
    Code:
    std::string tmp;
    std::cin >> tmp;
    
    for(int i = 0; i < tmp.size(); i++)
    {
      if(!isnum(tmp[i]))
      {
        // not a number, do something
      }
    }

  7. #22
    Me
    Join Date
    Jul 2006
    Posts
    71
    Implicit Declaration of function 'int isnum(...)'

  8. #23
    Registered User
    Join Date
    May 2006
    Posts
    903
    Oh well, can't find what header has it.
    Code:
    inline bool isnum(char c)
    {
      return (c >= 48 || <= 57);
    }

  9. #24
    Me
    Join Date
    Jul 2006
    Posts
    71
    Hmm...well now if I input anything it just exits the program.

  10. #25
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Oh well, can't find what header has it.
    That's because isnum isn't a standard function. You're thinking of isdigit from <cctype>. An isnum function would be particularly awkward because it implies that any legal character from any numeric type would return true. That includes non-digit characters that are valid in certain numeric representations such as floating-point and hexadecimal.

    >return (c >= 48 || <= 57);
    That's kind of broken. You forgot to put c in the second half of the expression. The logic is also faulty because if c is greater than 48, regardless of how much greater, the test will return true. Something like this would be better:
    Code:
    inline bool isnum(char c)
    {
      return (c >= 48 && c <= 57);
    }
    You also have two portability issues. The first is that isnum is a reserved name (specifically, any name that begins with "is" and is followed by at least one lower case letter). The second issue is that you assume the values of the digits in your character set. You also assume that they're consecutive, which is okay in this case, but you can't make the same assumption for the rest of the character set. The good news is that you can write this function portably:
    Code:
    bool is_num ( char ch )
    {
      return '0' <= ch && ch <= '9';
    }
    Then again, isdigit gives you the same solution without having to worry about locales either.
    My best code is written with the delete key.

  11. #26
    Registered User
    Join Date
    May 2006
    Posts
    903
    Huh, I think I had a brain fart there and completely changed isdigit to isnum =/ I think I was pretty tired because that function now looks quite stupid to me with the binary OR and the missing 'c' =/ Oh well, thanks for pointing out =]

  12. #27
    Registered User
    Join Date
    May 2006
    Posts
    30
    case 'a': blabla?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Converting Double to Float
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 06-17-2006, 02:46 PM
  3. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  4. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  5. Microsoft Visual C++ compiler, cast problem?
    By jonnie75 in forum C Programming
    Replies: 5
    Last Post: 11-10-2001, 08:53 AM