473,396 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ascii numbers

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
19 2251
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
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
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
> 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
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
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

"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
I stand corrected.

john
Jul 22 '05 #9
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
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

"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
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

"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
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

"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
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

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Doug Fort | last post by:
This is an excerpt from a much longer post on the python-dev mailing list. I'm responding here, to avoid cluttering up python-dev. <snip> >Some English readers might not really imagine, but it...
37
by: chandy | last post by:
Hi, I have an Html document that declares that it uses the utf-8 character set. As this document is editable via a web interface I need to make sure than high-ascii characters that may be...
6
by: Willem | last post by:
What is the best way to calculate an ascii string into an integer (not talking about an atoi conversion): For examle if I have the ascii string: "/b" then in hex it would be 2F7A and if I...
12
by: IamIan | last post by:
I searched the archives but couldn't find anyone else with this problem. Basically I'm grabbing all ASCII files in a directory and doing geoprocessing on them. I need to calculate a z-factor based...
3
by: Willing 2 Learn | last post by:
Hey, I'm trying to teach myself C++ and I came across 3 problems. I understand the concept of FSA but getting the C++ code to do it as become an issue. Only thing is im clueless as to how to do...
19
by: many_years_after | last post by:
Hi,everyone: Have you any ideas? Say whatever you know about this. thanks.
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
14
by: Peter Sprenger | last post by:
Hello, I want to efficient convert floating point numbers (IEEE754) into a string. I have no library routines that do the job (like sprintf etc.), because I work in an embedded environment. ...
4
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. ...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.