Newbie in need of help.

M

MrX.nin

Hi to anyone that is reading this post,

I'm currently working out of a book called 'Teach Yourself C++'
They have given me an exersize at the end of a section that I can't
understand and do not know how to do it with out the code being 5
pages long. The question is:

3. Write a program that gets a character from the user and categorizes
it into lowercase letter, upper case letter, number or other. You can
make use of the fact that each of these groups have consecutive ASCII
codes; the digit characters are within codes 48 to 58, for instance.

The section has taught me how to do, if, else, else if and switch
statements, though I can't figure out how to do what it is asking me
to do.
Any advice would be greatly appreciated,

Thanks in advance,
MRXNIN
 
V

Victor Bazarov

I'm currently working out of a book called 'Teach Yourself C++'
They have given me an exersize at the end of a section that I can't
understand and do not know how to do it with out the code being 5
pages long. The question is:

3. Write a program that gets a character from the user and categorizes
it into lowercase letter, upper case letter, number or other. You can
make use of the fact that each of these groups have consecutive ASCII
codes; the digit characters are within codes 48 to 58, for instance.

The section has taught me how to do, if, else, else if and switch
statements, though I can't figure out how to do what it is asking me
to do.

It seems that the exercise is asking you to do something in line with

input a character
if (character is within a certain range)
classify it as number
else if (character is within another range)
classify it as lowercase letter
... // and so one

V
 
O

osmium

I'm currently working out of a book called 'Teach Yourself C++'
They have given me an exersize at the end of a section that I can't
understand and do not know how to do it with out the code being 5
pages long. The question is:

3. Write a program that gets a character from the user and categorizes
it into lowercase letter, upper case letter, number or other. You can
make use of the fact that each of these groups have consecutive ASCII
codes; the digit characters are within codes 48 to 58, for instance.

The section has taught me how to do, if, else, else if and switch
statements, though I can't figure out how to do what it is asking me
to do.

You have four cases so he may expect you to set up an enumerated variable.
ASCII is a red herring, in a way, the important part is that there are no
important breaks in contiguity. So if you have a candidate char in ch,

if(ch >= '0' && ch <= '9')
// it's a digit
else if(ch>= 'a' && ch <= 'z')
// lc letter
else if(ch>= 'A' && ch <= 'Z')
// uc letter
else
// it's one of the residual cases

Note that there are no magic numbers above (such as 48), which makes for
better documentation.

If you are still perplexed about ASCII, the character code is often given in
an appendix in programming books. If not there are literally thousands of
such listings on the internet. The author was basically saying "this is not
EBCDIC" which is the only code known that has non-contiguous alphabetic
characters.
 
J

Jerry Coffin

[ ... ]
3. Write a program that gets a character from the user and categorizes
it into lowercase letter, upper case letter, number or other. You can
make use of the fact that each of these groups have consecutive ASCII
codes; the digit characters are within codes 48 to 58, for instance.

Translating to something closer to code, this means that a digit is
greater than or equal to '0' and less than or equal to '9'. Likewise, a
lower case letter is greater than or equal to 'a' and less than or equal
to 'z' (etc. for the other types).
 

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,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top