char [0-9] to int [0-9]

A

Alex Buell

Oh yes. Don't need an else, but I'd put in a return -1 just in case
input isn't valid.
 
K

Keith Thompson

Alex Buell said:
Oh yes. Don't need an else, but I'd put in a return -1 just in case
input isn't valid.

Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.
 
A

Alex Buell

Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.

Eh? Are you saying the below won't work? It should return -1 for values
not in the 0..9 range.

short char2digt(char c)
{
if (c => '0' && c <= '9)
return c - '0';

return -1;
}
 
P

pete

Alex said:
I'd do something like this:

short char2digit(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
}

The meaning of the code is simpler
if you use type int for your parameter and return type,
otherwise you have implicit conversions all over the place.
The number of standard library functions which use
either char or short, for parameter type or return type,
is zero.
 
A

Alex Buell

The meaning of the code is simpler
if you use type int for your parameter and return type,
otherwise you have implicit conversions all over the place.
The number of standard library functions which use
either char or short, for parameter type or return type,
is zero.

Yes I know but I wasn't the OP ;o)
 
K

Keith Thompson

Alex Buell said:
Eh? Are you saying the below won't work? It should return -1 for values
not in the 0..9 range.

short char2digt(char c)
{
if (c => '0' && c <= '9)
return c - '0';

return -1;
}

No, I just misunderstood. When you said "Don't need an else", I
thought you meant leaving the code as it was originally (which falls
off the end of the function if the argument is out of range).

I'd probably put the "return -1;" in an else clause, but that's just a
matter of style.

I'd also have the function return int rather than short.
 
P

pete

Keith said:
No, I just misunderstood. When you said "Don't need an else", I
thought you meant leaving the code as it was originally (which falls
off the end of the function if the argument is out of range).

I'd probably put the "return -1;" in an else clause, but that's just a
matter of style.

I'd also have the function return int rather than short.

int char2digt(int c)
{
return c => '0' && c <= '9 ? c - '0' : -1;
}
 
J

jjf

Cy said:
If you ever have a job interview and get asked this, don't use a macro where
an ordinary function will do. The interviewer might be me and I'd show you
the door in one second flat.

Why? And which language are you interviewing in?
 
R

Richard Heathfield

Keith Thompson said:
I don't think I'd bother with the macros. Just using c-'0' or d+'0'
seems clear enough to me. YMMV.

In fact, this is a case where the code is actually clearer than the macro
names that purport to represent it. Why, for example, is there a number 2
in those names?
 
P

pete

Richard said:
Keith Thompson said:


In fact, this is a case where the code
is actually clearer than the macro
names that purport to represent it.
Why, for example, is there a number 2
in those names?

atoi
a2i

I rhyme "atoi" with "patois", but most people don't.
 
R

Richard Heathfield

Keith Thompson said:
Yes, you need an else. If you're going to assume the input is valid,
you don't need the if. If you're not going to assume the input is
valid, you need the if *and* the else.

Nonsense, Thompson - utter nonsense. The following code uses neither, and
yet manages to return 0-9 for '0'-'9' inputs, or a negative value if the
input is invalid.

int char_to_digit(int ch)
{
return (2 * (ch >= '0' && ch <= '9') - 1) * (ch - '0');
}
 
D

Dale Henderson

RH> Nonsense, Thompson - utter nonsense. The following code uses
RH> neither, and yet manages to return 0-9 for '0'-'9' inputs, or
RH> a negative value if the input is invalid.

RH> int char_to_digit(int ch)
RH> {
RH> return (2 * (ch >= '0' && ch <='9') - 1) * (ch - '0');
RH> }

I think you mean

int char_to_digit(int ch)
{
return (2 * (ch >= '0' && ch <='9') - 1) * abs(ch - '0');
}
 
J

Jordan Abel

Tomás said:
Cy Edmunds posted:




At which point, I, the programmer, would propose

[snip]

Hold it right there. Once I show you the door you ain't proposing
anything!

Well, you shouldn't be showing people the door based on an incorrect
assumption, namely [that a call to an ordinary function can be used in
all contexts where a use of a macro can].
 
B

Bo Persson

Jordan Abel said:
Tomás said:
Cy Edmunds posted:


If you ever have a job interview and get asked this, don't use a
macro
where an ordinary function will do. The interviewer might be me
and
I'd show you the door in one second flat.


At which point, I, the programmer, would propose

[snip]

Hold it right there. Once I show you the door you ain't proposing
anything!

Well, you shouldn't be showing people the door based on an incorrect
assumption, namely [that a call to an ordinary function can be used
in
all contexts where a use of a macro can].

It is based on the assumption that a macro can interfere with other
code in a lot of places where a function cannot.

(This is the comp.lang.c++ answer).


Bo Persson
 
P

Pete Becker

Robert said:
That's your justification for using a macro? You've got to be joking.

That's an example of why the "rule" that functions should always be used
instead of macros is silly. Context matters.
 
P

Pete Becker

Alf said:
In C++ it's no big deal to define a non-macro conversion that yields a
compile time constant:

template< char digit >
struct IntFromDigit { enum{ value = digit - '\0' }; };

...
int array[IntFromDigit<'7'>::value];

If you swallow a tiny piece of a toad one day, a larger one the next,
and you keep gradually increasing the size of the pieces day by day,
after a while you'll be able to swallow an entire toad. That doesn't
make it something I want to do. This code is bloated and hard to read.
If anyone came up with it as their first solution in a job interview I'd
show them the door in one second flat.
If that conversion were ever required, it would be in template code, so
a template solution is very fitting.

A simpler solution---with a macro---is even better, despite slogans to
the contrary.
However, normally you'd just do

std::vector<int> array(intFromDigit('7'));

If anyone proposed this as a solution to the original problem, which was
to get a compile-time constant, I'd show them the door in one second
flat. Normal or otherwise, it does't solve the problem.
 
P

Pete Becker

Pete said:
If anyone proposed this as a solution to the original problem, which was
to get a compile-time constant, I'd show them the door in one second
flat. Normal or otherwise, it does't solve the problem.

Whoops, fingers got ahead of brain. The original problem wasn't to get a
compile-time constant.
 

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
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top