By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,412 Members | 993 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,412 IT Pros & Developers. It's quick & easy.

ascii numbers

P: n/a
Dan
Some knows how to verify an ascii code.
I want to verify a char for the list of ascii codes between 64 to 122.
Mainly in other words, check a char, look if its in a range of ascii code
and output a bool, true or false.
thanks

D
Jul 22 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Dan wrote:
Some knows how to verify an ascii code.
I want to verify a char for the list of ascii codes between 64 to 122.
Mainly in other words, check a char, look if its in a range of ascii
code and output a bool, true or false.
thanks

D


template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}

int main()
{
bool c_between_64_122 = isbetween('c', 64, 122);
}

As a bonus, it works for any type where operators >, >=, and <= are defined.

- Pete
Jul 22 '05 #2

P: n/a
Dan
template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}

int main()
{
bool c_between_64_122 = isbetween('c', 64, 122);
}

As a bonus, it works for any type where operators >, >=, and <= are defined.
- Pete

Thats pretty cool stuff.
I can see that return c >= l && c <= u will return a bool value for values
between 64 and 122 only if the conditions above are met. What I want to know
is your 'c' , I guess this is you input ? do you declare it as a char
array? so as to make it

int main()
{
char c;

cout<<"Press a key " ;
cin>> c;

bool c_between_64_122 = isbetween('c', 64, 122);
if (bool == true) { cout<< "It is in there" <<endl; }
if (bool == false) {cout<< "It is not in there" <<endl; }


Jul 22 '05 #3

P: n/a
Dan wrote:
template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}
<snip>
Thats pretty cool stuff.
I can see that return c >= l && c <= u will return a bool value for
values between 64 and 122 only if the conditions above are met. What
I want to know is your 'c' , I guess this is you input ? do you
declare it as a char array? so as to make it

int main()
{
char c;

cout<<"Press a key " ;
cin>> c;

bool c_between_64_122 = isbetween('c', 64, 122);
if (bool == true) { cout<< "It is in there" <<endl; }
if (bool == false) {cout<< "It is not in there" <<endl; }


'c' is a character constant I randomly picked to demo it, to make your
example work correctly:

#include <iostream>
using std::cout;
using std::endl;

#include <algorithm>

template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}

int main()
{
char c;
cout << "Enter a character: " ;
cin >> c;
cout << endl;

bool c_between_64_122 = isbetween(c, 64, 122);
if (c_between_64_122)
cout << "It is in there" << endl;
else
cout << "It is not in there" << endl;

return 0;
}

- Pete
Jul 22 '05 #4

P: n/a
> template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}
bool c_between_64_122 = isbetween('c', 64, 122);


Now, I didn't get this... Where do you define T for the clas template?
Or do all have to be of the same type? I would have expected something
like:
isbetween<char>('c', 64, 122)
No?
You're puzzling me here... And I thought I understood templates...
-Gernot
Jul 22 '05 #5

P: n/a
Gernot Frisch wrote in news:2j************@uni-berlin.de in
comp.lang.c++:
template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}
bool c_between_64_122 = isbetween('c', 64, 122);
Now, I didn't get this... Where do you define T for the clas template?


T is deduced.
Or do all have to be of the same type?
Yes they do.
I would have expected something
like:
isbetween<char>('c', 64, 122)
No? You're puzzling me here... And I thought I understood templates...


The bit you have missed is that 'c' is passed as an int, so template
argument deduction succeds with T = int. The call is effectivly:

isbetween( int >( 'c', 64, 122 );

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #6

P: n/a
Rob Williscroft wrote in news:Xns950864EDDF118ukcoREMOVEfreenetrtw@
130.133.1.4 in comp.lang.c++:
isbetween( int >( 'c', 64, 122 );


isbetween< int >( 'c', 64, 122 );

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7

P: n/a

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2j************@uni-berlin.de...
template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}
bool c_between_64_122 = isbetween('c', 64, 122);


Now, I didn't get this... Where do you define T for the clas template?
Or do all have to be of the same type? I would have expected something
like:
isbetween<char>('c', 64, 122)
No?
You're puzzling me here... And I thought I understood templates...
-Gernot


Yes that should be

bool c_between_64_122 = isbetween('c', (char)64, (char)122);

or as you had it

bool c_between_64_122 = isbetween<char>('c', 64, 122);

or alternatively

template<class T, class U>
bool isbetween(T c, U l, U u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}

john
Jul 22 '05 #8

P: n/a
I stand corrected.

john
Jul 22 '05 #9

P: n/a
Dan

"Gernot Frisch" <Me@Privacy.net> wrote in message
news:2j************@uni-berlin.de...
template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}
bool c_between_64_122 = isbetween('c', 64, 122);


Now, I didn't get this... Where do you define T for the clas template?
Or do all have to be of the same type? I would have expected something
like:
isbetween<char>('c', 64, 122)
No?
You're puzzling me here... And I thought I understood templates...
-Gernot


after compiling
error C2782: 'bool __cdecl isbetween(T,T,T)' : template parameter 'T' is
ambiguous
D
Jul 22 '05 #10

P: n/a
Rob Williscroft wrote in news:Xns950864EDDF118ukcoREMOVEfreenetrtw@
130.133.1.4 in comp.lang.c++:
Gernot Frisch wrote in news:2j************@uni-berlin.de in
comp.lang.c++:
template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}
bool c_between_64_122 = isbetween('c', 64, 122);


Now, I didn't get this... Where do you define T for the clas template?


T is deduced.
Or do all have to be of the same type?


Yes they do.
I would have expected something
like:
isbetween<char>('c', 64, 122)
No?

You're puzzling me here... And I thought I understood templates...


The bit you have missed is that 'c' is passed as an int, so template
argument deduction succeds with T = int. The call is effectivly:

isbetween( int >( 'c', 64, 122 );


Ignore the above its wrong.

The template matches on the actual type *not* the promoted type
so the explict invocation (or 1 or 2 casts) is required.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #11

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2j************@uni-berlin.de...
I stand corrected.


Or not, as the case may be. Templates are confusing, I found the book C++
Templates by Josuttis and someone else (apologies) to be very helpful.

John
Jul 22 '05 #12

P: n/a
Pete C. wrote:
Dan wrote:
Some knows how to verify an ascii code.
I want to verify a char for the list of ascii codes between 64 to
122. Mainly in other words, check a char, look if its in a range of
ascii code and output a bool, true or false.
thanks

D


template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}

int main()
{
bool c_between_64_122 = isbetween('c', 64, 122);
}

As a bonus, it works for any type where operators >, >=, and <= are
defined.

- Pete


Sorry about all of the confusion, I never compiled it figuring that it was
simple enough that the OP would have no problem fixing it if there were such
a problem. Below is a version that compiles and runs fine under VC7.1 with
extensions off. It did indeed report "ambiguous template parameters" with
the old program.

#include <iostream>
using std::cout;
using std::cin;
using std::endl;

#include <algorithm>
using std::swap;

template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) swap(l, u);
return c >= l && c <= u;
}

int main()
{
char c;
cout << "Enter a character: ";
cin >> c;
cout << endl;

if (isbetween<char>(c, 64, 122))
cout << "It is in there" << endl;
else
cout << "It is not in there" << endl;

return 0;
}

BTW, I should warn the OP that checking for ranges in characters like that
is not portable. It will only correctly run on systems that use ASCII.

- Pete

Jul 22 '05 #13

P: n/a

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2j************@uni-berlin.de...

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2j************@uni-berlin.de...
I stand corrected.


Or not, as the case may be. Templates are confusing, I found the book C++
Templates by Josuttis and someone else (apologies) to be very helpful.

John

problem is that the Program always return 1 no matter what..
Mike P
Jul 22 '05 #14

P: n/a
Dan wrote:
Some knows how to verify an ascii code.
I want to verify a char for the list of ascii codes between 64 to 122.
Mainly in other words, check a char, look if its in a range of ascii code
and output a bool, true or false.
thanks

D


According to the ASCII encoding scheme:
64 == 0x40 == '@'.
65 == 0x41 == 'A'.
....
122 == 0x7A == 'z'.

This range encapsulates the uppercase and lowercase letters,
plus a few symbols. You may want to look at the functions
std::isalpha, std::ispunct, std::isdigit.

Have you tried:
bool result;
char value;

result = (value >= 64) && (value <= 122);

For "optimization" you could declare it as an inline function
and place it in a header file:
inline bool In_Range(char x)
{ return (x >= 64) && (value <= 122);}

However, when dealing with ASCII codes, character literals
are preferred to decimal or hexadecimal values:
{ return (x >= '@') && (value <= 'z');}
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #15

P: n/a

"Dan" <le*********@yah00.c0m> wrote in message
news:uI**************@news20.bellglobal.com...
Some knows how to verify an ascii code.
I want to verify a char for the list of ascii codes between 64 to 122.
Those values do fall inside the set of ASCII values. But note that
that set is not all-inclusive. (ASCII character values range from
zero through 127.
Mainly in other words, check a char, look if its in a range of ascii code
and output a bool, true or false.
thanks


#include <iostream>

bool is_in_range(char c, char lo = 64, char hi = 122)
{
return c > = lo && c < == hi)
}

-Mike
Jul 22 '05 #16

P: n/a
Mike Wahler wrote:
"Dan" <le*********@yah00.c0m> wrote in message
news:uI**************@news20.bellglobal.com...
#include <iostream>
bool is_in_range(char c, char lo = 64, char hi = 122)
{
return c > = lo && c < == hi)
}

-Mike


Is this real C or C++ code?
What is "< ==" operator?

I thought no spaces were allowed between the symbols.
--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

Jul 22 '05 #17

P: n/a

"Thomas Matthews" <Th****************************@sbcglobal.net> wrote in
message news:Ln*******************@newssvr31.news.prodigy. com...
Mike Wahler wrote:
"Dan" <le*********@yah00.c0m> wrote in message
news:uI**************@news20.bellglobal.com...
#include <iostream>

bool is_in_range(char c, char lo = 64, char hi = 122)
{
return c > = lo && c < == hi)
}

-Mike


Is this real C or C++ code?


Um, nope.
What is "< ==" operator?
Brain fart.

I thought no spaces were allowed between the symbols.


A loud one. :-)

Sorry about that.

-Mike
Jul 22 '05 #18

P: n/a
"Pete C." <x@x.x> wrote in message news:<jf******************@newsread2.news.pas.eart hlink.net>...
Dan wrote:
Some knows how to verify an ascii code.
I want to verify a char for the list of ascii codes between 64 to 122.
Mainly in other words, check a char, look if its in a range of ascii
code and output a bool, true or false.
thanks

D


template<class T>
bool isbetween(T c, T l, T u)
{
if(l > u) std::swap(l, u);
return c >= l && c <= u;
}

int main()
{
bool c_between_64_122 = isbetween('c', 64, 122);
}

As a bonus, it works for any type where operators >, >=, and <= are defined.


And of course std::swap(T,T). It isn't be needed:

template<class T>
bool isbetween(T c, T l, T u)
{
if(l < u)
return c >= l && c <= u;
else
return c >= u && c <= l;
}

However, it's un-C-like to use inclusive upper bounds.

To solve the Template Argument Deduction problem, use

template<class T> struct not_deduced{ typedef T type; };
template<class T> bool
isbetween(T c, not_deduced<T>::type l, not_deduced<T>::type u) {

Now only the first T is used. The actual arguments for l and u
will be cast to the type of c, if possible. This also allows
std::string s;
std::cin << s;
isbetween( s, "A", "B" );

Regards,
Michiel Salters
Jul 22 '05 #19

P: n/a
Michiel Salters wrote:
"Pete C." <x@x.x> wrote in message <snip>
And of course std::swap(T,T). It isn't be needed:

template<class T>
bool isbetween(T c, T l, T u)
{
if(l < u)
return c >= l && c <= u;
else
return c >= u && c <= l;
}
It of course can be done without swap, but I like my version better - I
prefer to not have the same logic in two places like that, even for such
simple things.

However, it's un-C-like to use inclusive upper bounds.

To solve the Template Argument Deduction problem, use

template<class T> struct not_deduced{ typedef T type; };
template<class T> bool
isbetween(T c, not_deduced<T>::type l, not_deduced<T>::type u) {

Now only the first T is used. The actual arguments for l and u
will be cast to the type of c, if possible. This also allows
std::string s;
std::cin << s;
isbetween( s, "A", "B" );
Thanks, I'm adding that class to my util lib :)

- Pete

Regards,
Michiel Salters


Jul 22 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.