Only number not alphabet. how? c++

K

kundasang

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.
 
V

Victor Bazarov

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 <cctype> header.
Something like

if (std::islower(sale) && std::isalpha(sale)) ...

V
 
J

Junchen WANG

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;
}
 
P

Paavo Helde

(e-mail address removed) wrote in @i29g2000prf.googlegroups.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
 
R

Robbie Hatley

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?

That depends on how your incoming data is being read and stored.
The best way to read data from either keyboard or a file is
one line at a time using the "getline()" function, which gets data
from a stream and writes it to a std::string:

std::istream& std::getline ( std::istream& is, std::string& str );

If you're reading from keyboard, your stream is std::cin, so you might do
something like:

std::string InputString;
std::getline (std::cin, InputString);

To see whether the user entered alphabetic or numeric characters,
Look at the characters you get in your string using the character-type
functions in the <cctype> header (inherited from C), such as isalpha(),
islower(), isdigit(), etc. For example, if you're expecting only
positive integers, you can stipulate that unless every character passes
the isdigit() test, it's an error. Or, if you want to allow positive
or negative real numbers, also allow periods and minus signs.

Or, you can take a whole different approach and just use the atoi()
or atof() functions

std::string InputString;
double Number;

getline (std::cin, InputString);
Number = atof(InputString.c_str());

If the user enters garbage, Number will be zero. So if Number is zero,
then you know an error occured, unless zero is within your "allowable"
range of input numbers.

And there's other ways to do it as well. As usual, TIMTOWTDI.
 
J

James Kanze

(e-mail address removed) wrote in @i29g2000prf.googlegroups.com:

Because lower case characters are not guaranteed to be
contiguous. (Nor are they with EBCDIC.)
The if clause above does not output any error :-S
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,498
Latest member
log5Sshell/alfa5

Latest Threads

Top