Only number not alphabet. how? c++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kundasang@gmail.com

    Only number not alphabet. how? c++

    Hello,

    how to do simple code for:

    if(alphabet)
    output ERROR

    because char is int I cannot do like this:
    if(sale >= 'a' && sale =< 'z')

    because if my sale is 98 it will output error because b is 98.

    I want number, not character.
    how?

    Thank you.
  • Victor Bazarov

    #2
    Re: Only number not alphabet. how? c++

    kundasang@gmail .com wrote:
    how to do simple code for:
    >
    if(alphabet)
    output ERROR
    >
    because char is int I cannot do like this:
    if(sale >= 'a' && sale =< 'z')
    >
    because if my sale is 98 it will output error because b is 98.
    >
    I want number, not character.
    how?
    See the family of 'std::is...' functions from <cctypeheader .
    Something like

    if (std::islower(s ale) && std::isalpha(sa le)) ...

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Junchen WANG

      #3
      Re: Only number not alphabet. how? c++

      On 3ÔÂ18ÈÕ, ÏÂÎç10ʱ55·Ö, kundas...@gmail .com wrote:
      Hello,
      >
      how to do simple code for:
      >
      if(alphabet)
      output ERROR
      >
      because char is int I cannot do like this:
      if(sale >= 'a' && sale =< 'z')
      >
      because if my sale is 98 it will output error because b is 98.
      >
      I want number, not character.
      how?
      >
      Thank you.
      // Enable RTTI and you can use typeid operator to get the type info.
      // compile with: /GR /EHsc
      #include <iostream>
      #include <typeinfo.h>
      using namespace std;

      int main() {

      char a;
      int b;
      cout << typeid( a ).name() << endl;
      cout << typeid( b ).name() << endl;
      }

      Comment

      • Paavo Helde

        #4
        Re: Only number not alphabet. how? c++

        kundasang@gmail .com wrote in news:a6a5021d-b4da-49a8-8016-8b63e90c9e32
        @i29g2000prf.go oglegroups.com:
        Hello,
        >
        how to do simple code for:
        >
        if(alphabet)
        output ERROR
        >
        because char is int I cannot do like this:
        if(sale >= 'a' && sale =< 'z')
        >
        Why not?
        because if my sale is 98 it will output error because b is 98.
        >
        The if clause above does not output any error :-S
        I want number, not character.
        how?
        Sorry, can't really understand what you want. The char and int types are
        both holding numbers in C++ and can be converted easily into each other (if
        values fit). It depends on the program if it interprets the values as
        numbers or as ASCII character codes. The iostream facility generally
        interprets chars as ASCII character codes and ints as numbers, if this is
        your concern you should convert the numbers from one type to another.

        Post more code if this did not help!

        regards
        Paavo

        Comment

        • James Kanze

          #5
          Re: Only number not alphabet. how? c++

          On Mar 18, 8:41 pm, Paavo Helde <nob...@ebi.eew rote:
          kundas...@gmail .com wrote in news:a6a5021d-b4da-49a8-8016-8b63e90c9e32
          @i29g2000prf.go oglegroups.com:
          how to do simple code for:
          if(alphabet)
          output ERROR
          because char is int I cannot do like this:
          if(sale >= 'a' && sale =< 'z')
          Why not?
          Because lower case characters are not guaranteed to be
          contiguous. (Nor are they with EBCDIC.)
          because if my sale is 98 it will output error because b is
          98.
          The if clause above does not output any error :-S
          I want number, not character.
          Sorry, can't really understand what you want. The char and int
          types are both holding numbers in C++ and can be converted
          easily into each other (if values fit). It depends on the
          program if it interprets the values as numbers or as ASCII
          character codes. The iostream facility generally interprets
          chars as ASCII character codes and ints as numbers, if this is
          your concern you should convert the numbers from one type to
          another.
          If the question is how to determine if a particular value is
          alphabetic or not, there's always:
          #include <locale>

          if ( std::isalpha( character, std::locale() ) )
          or,
          #include <cctype >

          if ( std::isalpha( static_cast< unsigned char >( character ) ) )
          (The first should work for any value in a char
          variable---if the type containing the value is not a char, you
          need to cast it to one first. The second will work for any
          value in the range [0...UCHAR_MAX], or EOF---if the value is in
          a char value, you must cast it to unsigned_char first, to ensure
          that no negative values occur.)

          --
          James Kanze (GABI Software) email:james.kan ze@gmail.com
          Conseils en informatique orient�e objet/
          Beratung in objektorientier ter Datenverarbeitu ng
          9 place S�mard, 78210 St.-Cyr-l'�cole, France, +33 (0)1 30 23 00 34

          Comment

          Working...