Connecting Tech Pros Worldwide Forums | Help | Site Map

Only number not alphabet. how? c++

kundasang@gmail.com
Guest
 
Posts: n/a
#1: Mar 18 '08
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
Guest
 
Posts: n/a
#2: Mar 18 '08

re: Only number not alphabet. how? c++


kundasang@gmail.com wrote:
Quote:
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(sale) && std::isalpha(sale)) ...

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


Junchen WANG
Guest
 
Posts: n/a
#3: Mar 18 '08

re: Only number not alphabet. how? c++


On 3月18日, 下午10时55分, kundas...@gmail.com wrote:
Quote:
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;
}
Paavo Helde
Guest
 
Posts: n/a
#4: Mar 18 '08

re: Only number not alphabet. how? c++


kundasang@gmail.com wrote in news:a6a5021d-b4da-49a8-8016-8b63e90c9e32
@i29g2000prf.googlegroups.com:
Quote:
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?
Quote:
because if my sale is 98 it will output error because b is 98.
>
The if clause above does not output any error :-S
Quote:
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
James Kanze
Guest
 
Posts: n/a
#5: Mar 19 '08

re: Only number not alphabet. how? c++


On Mar 18, 8:41 pm, Paavo Helde <nob...@ebi.eewrote:
Quote:
kundas...@gmail.com wrote in news:a6a5021d-b4da-49a8-8016-8b63e90c9e32
@i29g2000prf.googlegroups.com:
Quote:
Quote:
how to do simple code for:
Quote:
Quote:
if(alphabet)
output ERROR
Quote:
Quote:
because char is int I cannot do like this:
if(sale >= 'a' && sale =< 'z')
Quote:
Why not?
Because lower case characters are not guaranteed to be
contiguous. (Nor are they with EBCDIC.)
Quote:
Quote:
because if my sale is 98 it will output error because b is
98.
Quote:
The if clause above does not output any error :-S
Quote:
Quote:
I want number, not character.
Quote:
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.kanze@gmail.com
Conseils en informatique orient锟絜 objet/
Beratung in objektorientierter Datenverarbeitung
9 place S锟絤ard, 78210 St.-Cyr-l'锟絚ole, France, +33 (0)1 30 23 00 34
Closed Thread