Connecting Tech Pros Worldwide Help | Site Map

Only number not alphabet. how? c++

 
LinkBack Thread Tools Search this Thread
  #1  
Old March 18th, 2008, 03:05 PM
kundasang@gmail.com
Guest
 
Posts: n/a
Default 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.

  #2  
Old March 18th, 2008, 03:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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


  #3  
Old March 18th, 2008, 03:05 PM
Junchen WANG
Guest
 
Posts: n/a
Default 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;
}
  #4  
Old March 18th, 2008, 07:45 PM
Paavo Helde
Guest
 
Posts: n/a
Default 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
  #5  
Old March 19th, 2008, 11:05 AM
James Kanze
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.