ascii numbers

D

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
 
P

Pete C.

Dan said:
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
 
D

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

Pete C.

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
 
G

Gernot Frisch

template said:
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
 
D

Dan

Gernot Frisch said:
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
 
R

Rob Williscroft

Gernot Frisch wrote in in
comp.lang.c++:
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.
 
J

John Harrison

Gernot Frisch said:
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
 
R

Rob Williscroft

Rob Williscroft wrote in 130.133.1.4 in comp.lang.c++:
Gernot Frisch wrote in in
comp.lang.c++:


T is deduced.


Yes they do.



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

John Harrison

John Harrison said:
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
 
M

MiKy

John Harrison said:
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
 
P

Pete C.

Pete said:
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
 
T

Thomas Matthews

Dan said:
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
 
M

Mike Wahler

Dan said:
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
 
T

Thomas Matthews

#include said:
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
 
M

Michiel Salters

Pete C. said:
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
 
P

Pete C.

Michiel said:
"Pete C." <[email protected]> wrote in message
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
 

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

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top