From: Mike Barnard on
On Sun, 09 May 2010 13:44:15 +0100, Francis Glassborow
<francis.glassborow(a)btinternet.com> wrote:

>Mike Barnard wrote:

>> I'm trying to use online tutorials to teach myself C. (I'm starting
>> with the basics before eventually trying for Objective C.) I have

Hi Francis.

>OK I have read the responses and none of them seem to tackle what I
>consider to be fundamental. How to get a single digit from the keyboard.
>Here is some simple code from me that you might like to consider.

OK...

>int validate_input(char c){
> int temp = c-'0';
> if(temp < 0 || temp > 9){
> puts("Your input was not one of the digits from 0 to 9\n");
> temp = -1;
> }
> return temp
>}
>
>int main(){
> char c;
> int i;
> scanf("Input a single digit and press return. %c", &c);
> i=validate_input(c);
> if(i == -1) // do whatever you want with invalid input
> else // do whatever you want with valid input
> return 0;
>}

As you say, simple but succinct. Thanks. It shows me a couple of
things...

But first, (to all readers, not aimed at you Francis) I have no tutor.
I have little experience and in the abscence of those I'm using my
logic and using what I have, online tutorials. A proper book is on the
horizon. (I get paid in a week time; hooray fresh food! :)) So if I'm
not doing it right, or don't yet know what I should be doing it's
called learning the hard way.

The aim of my little program that gave me the error was not so much
about getting input from the keyboard but about writing functions. I
deliberately wrote a function for everything I could think of, just to
practice writing them. To be able to set the return to a value and to
catch it and use it. To send a value to a function and use it. I'm
only a few hours into the game, not years like most of you. (I should
have started younger, much younger!)

>And before you all start howling,

(Listens...)

> I would not actually use that code not
>least because I do not use arbitrary constants without making them
>manifest.

(Manifest... Clearly apparent to the sight or understanding; obvious.)
Aha.

>I would also use such library functions as isdigit()

First hit... http://www.elook.org/programming/c/isdigit.html. Aha
again. Actually, I would like to see 'inside' isdigit(). Would this be
written in assembler or another language? (Googles)Searching for
source code for isdigit() just returns examples of how it can be used,
BUT C compilers being written in C starts in about 1971. So C then.
Know of any libraries where I could see inside someone's compiler
code? :)

>for this
>special case. We also need to clear the keyboard buffer to cover cases
>where the user has typed in more than one character before pressing return.

I understand the idea, but later for this.

>However the principle point is that when validating input we have to get
> characters and process them (otherwise we need to know how to deal
>with rejected input that sits clogging the keyboard buffer)

>Finally, I am curious as to why the poster is learning C if he wants to
>use Objective C. Furthermore, why is he planning to learn Objective C
>which is a language that is very little used outside the Apple Mac world?

I wish to start with C because C is the language that Objective C and
C++ are built on. I have not found an online Objective C tutorial that
starts with the absolute basics for a newbie... "Now this is a
function, and this is a variable," etc. They all presuppose that you
know the basics and are converting. Mostly converting from C++.

As to WHY I want to look at Objective C, it's iPhone apps. Or rather,
my brother's wife has something that she would like to be converted
from paper to the phone so I said I'd look at it. (There is no urgency
and we haven't the money to hire an outside programmer, and I want to
learn.) Laugh, but that's it. And we will make it work, eventually.

Thanks for answering, but also for breaking it down into English. I
feel that some programmers have been coding for so long they've
forgotten how to speak it! This is alt.comp.lang.LEARN.c-c++ after
all.

It's late now, for me, so goodnight. I'm off to dream, of what I know
not. :) Then go to work... :(

Mike.
From: Mike Barnard on
On Sun, 09 May 2010 13:30:50 +0100, Paul Bibbings
<paul.bibbings(a)gmail.com> wrote:

>> I think I might find out how to make the underscore replace another
>> key. Which one is the dirtyest, the least touched!
>
>From what we've gathered already, I would say that that would be the
>SHIFT key! :-)

Heh, maybe. But actually, there is a key I never touch. To the left of
the digit 1...

`


Who EVER uses these characters? (Googles for keyboard mapping...)

http://webpages.charter.net/krumsick/ Goes to play for a while.
From: Mike Barnard on
On Sun, 09 May 2010 14:11:36 +0100, Paul Bibbings
<paul.bibbings(a)gmail.com> wrote:

>Mike Barnard <m.barnard.trousers(a)thunderin.co.uk> writes:
>
>> On Sun, 09 May 2010 12:05:18 +0100, Paul Bibbings
>> <paul.bibbings(a)gmail.com> wrote:
>>
>>>Mike Barnard <m.barnard.trousers(a)thunderin.co.uk> writes:
>>>
>>>>
>>>> I don't like the underscores purely because of the two handed key
>>>> input. Lazy? Me?
>>>
>>>It certainly seems like an oddly randam rejection of a common
>>>convention; unless, of course, you have a keyboard that otherwise
>>>permits `one-handed' input of !, ", %, ^, &, *, (, ), +, {, }, :,
>>>~, <, > and ?. Or will you not be using these either? ;-)
>>
>> Aha, if it doesn't already exist I see a niche in the market for a
>> programmers keyboard!
>>
>> This_is_a_variable. Thats three underscores, by the number of times
>> the variable is used. != is only one shift keystroke for example. I
>> think I might find out how to make the underscore replace another key.
>> Which one is the dirtyest, the least touched!
>
>Setting `humour' aside for a second response, it's worthwhile at this
>early stage in your learning to give you the opportunity to evaluate for
>yourself what is, perhaps, more common than you might imagine, and that
>is the `ease-of-typing' school of programming. The following won't be
>directly relevant to you as you are choosing to learn C, but even Bjarne
>Stroustrup - the creator of C++, which originated from C via `C with
>classes' - espouses in his important work `The C++ Programming
>Language' (Special Edition, �C.13.5):
>
> "The /typename/ keyword can also be used as an alternative to class
> in template declarations:
>
> template<typename T> void f(T);
>
> Being an indifferent typist and always short of screen space, I
> prefer the shorter:
>
> template<class T> void f(T);"
>
>This is not to question Stroustrup's choice here, nor even his reason
>for so choosing, in this context. However, you will encounter the same
>reasoning to varying degrees propping up the coding choices of
>programmers of all levels of expertise and experience in contexts where
>it matters more. In the example from Stroustrup his choice does not
>affect readability, but you will likewise come across many instances
>where it this is just the very quality of code that is sacrificed, and
>to what? To saving a few keystrokes here and there?
>
>As a beginner, I would want to suggest that you hold off choices based on
>`laziness', as you put it in your case, until you have learnt enough to
>be able to evaluate what /other/ consequences arise from such a choice.
>Follow convention, /until/ your learning has reached a point where you
>know enough to be able to work out where it might be abandoned without
>impact upon other factors such as readability, ease of understanding
>(and hence of maintenance), and the like.
>
>To take the `lazy' view at *this* stage of your learning would be like
>calling yourself a marathon running whilst walking casually across the
>tarmac from the start to the finish line, or, as a racing driver, to
>prefer the cut across the grass verge to the chicane, or as a lover, to
>woo with "come on love, let's get this over with!" In all such cases,
>something is inevitably lost.
>
>Regards
>
>Paul

Hi again.

As a beginner, I take in all I can and will try to believe that the
experience of thousands of others is worthwhile. I'm not a teenager
who knows it all after all! (Fifty mumble...).

Mike.
From: Richard Heathfield on
Mike Barnard wrote:

> Actually, I would like to see 'inside' isdigit(). Would this be
> written in assembler or another language?

C, in all likelihood.

Here are a couple of ways of writing isdigit:

int isdigit(int ch)
{
return ch >= '0' && ch <= '9';
}

int isdigit(int ch)
{
return _isdigit[(unsigned char)ch]; /* see below */
}

int isdigit(int ch)
{
return _ctype[(unsigned char)ch] & _IS_DIGIT; /* likewise */
}

Every is* and to* function must be able to handle EOF (a flag returned
by getc and friends to tell us that a request for further input could
not be met). The isdigit function yields 0 for EOF. The first of the
above two demonstrations ignores EOF completely, and simply focuses on
whether the input is a digit. If so, the test is true, and the function
therefore yields 0. Otherwise, the test is false, and the function
returns 0. EOF isn't a digit, so it fails the test and the function
therefore correctly returns 0 for that case.

In the second example, however, we have a slightly different way of
testing the input - an array lookup. The idea here is that we have an
internal array maintained by the implementation, which contains a 0 in
every element except for _isdigit['0'], _isdigit['1], ...,
_isdigit['9']. The cast is there to make EOF behave itself.

In the third example, the implementation is jamming all the
characteristics of each character into a single integer, using one bit
for each characteristic. I would guess that this might be an explanation
too far, though, at this stage in your learning. It's not actually
difficult, but beginners have a huge amount to take in, and this doesn't
really qualify as essential at this stage. Ask again when your learning
curve flattens out a bit (pun sort of intended).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within
From: Paul Bibbings on
Mike Barnard <m.barnard.trousers(a)thunderin.co.uk> writes:

> On Sun, 09 May 2010 13:30:50 +0100, Paul Bibbings
> <paul.bibbings(a)gmail.com> wrote:
>
>>> I think I might find out how to make the underscore replace another
>>> key. Which one is the dirtyest, the least touched!
>>
>>From what we've gathered already, I would say that that would be the
>>SHIFT key! :-)
>
> Heh, maybe. But actually, there is a key I never touch. To the left of
> the digit 1...
>
> `
> �
>
> Who EVER uses these characters? (Googles for keyboard mapping...)

I actually use that key quite a lot. I don't have a direct use for it
in programming code as I don't use any languages frequently enough that
make use of it (the ` can be used in Lisp, I believe, but the little
Lisp that I find myself writing is merely to keep my Emacs ticking
along).

Having said that, I do use '`' in the wider programming environment.
You may not need to, but I program using Cygwin, which is a Linux-like
environment for Windows, and I certainly use '`' in the makefiles that I
write that control how my code is built. So, you never know, you may
one day too.

In other contexts, ` is used in LaTeX to produce a left single quote as
distinct from ', which produces a right single quote (and likewise with
`` and '' for left-" and right-").

None of this has anything to do with the C language, but merely to
suggest that you might just need it one day, when you've finished that
vast coding project and are adding the final touches to your thesis.
:-)

Regards

Paul Bibbings