Issues with input

S

Spartan815

is there another function or way of going about getting input from the
command line besides getch(). Right now, Im just doing a simple while
loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display. I know thats rather open-ended, so really my better question
is can anyone point me to some good C resources online so I can
actually read about it...
 
P

Peter Pichler

Spartan815 said:
is there another function or way of going about getting input from the
command line besides getch().

From the command line? Wazzat? If you meant "standard input", there is a
multitude of standard functions:

[f]getc()
getchar()
fgets()
[f]scanf() /* better not */
gets() /* DEFINITELY not! */

On the other hand, there is no getch().
Right now, Im just doing a simple while
loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display.

s/display/displayed

I am not sure I understand what you are trying to do. To save N lines
of text you need an array of N strings. You could do it dynamically
with a linked list. They are dead easy to use as a stack, an ideal
structure for keeping a history.
I know thats rather open-ended, so really my better question
is can anyone point me to some good C resources online so I can
actually read about it...

This may sound a bit radical, but have you tried a... book? You know,
the things made of processed pulp of certain plants (usually trees),
usually rectangular in shape and glued together along the longer side
so you can leaf through them. It is sometimes called "reading".

Peter
 
C

CBFalconer

Spartan815 said:
is there another function or way of going about getting input from the
command line besides getch(). Right now, Im just doing a simple while
loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display. I know thats rather open-ended, so really my better question
is can anyone point me to some good C resources online so I can
actually read about it...

I suspect you mean the terminal, or stdin, rather than command
line. Standard routines include getc, fgetc, fgets. scanf is not
recommended for this purpose, nor is gets. However you can also
use my favorite, ggets, available on my site, download section.
R. Heathfield has another similar routine, that requires more care
and feeding IMO.
 
M

Malcolm

Spartan815 said:
is there another function or way of going about getting input from the
command line besides getch(). Right now, Im just doing a simple
while loop (terminated by a new line character) to get input, but I also
want to be able to save up to 10 entries so that a history can be
display. I know thats rather open-ended, so really my better
question is can anyone point me to some good C resources online so
I can actually read about it...
getc() is the best function for reading from stdin. gets() is unsafe,
scanf() is difficult to use, fgets() has subtle problems on overflow.

So write this function

char *getline(void)

calls malloc() and getc() to return a line from stdin.

Now all you need is an array of character points

char *history[10];
int current = 0; /* this is not strictly needed but may make things easier
to understand */

Intitialise all elements of history to NULL. Then for the first ten lines
read in the lines, incrementing current. When current gets to ten, the array
is full. Therefore free the top entry (history[0]) and call memmove() to
move all the pointers up one place. Then add your newly read line to the
bottom.
 
P

Paul Hsieh

(e-mail address removed) says...
getc() is the best function for reading from stdin. gets() is unsafe,
scanf() is difficult to use, fgets() has subtle problems on overflow.

I'll second all that. But scanf has the additional problem of necessarily
parsing your input in ways that don't allow you to definitively recover the raw
input.

I have a webpage explaining all of this, with sample code, here:

http://www.pobox.com/~qed/userInput.html
So write this function

char *getline(void)

calls malloc() and getc() to return a line from stdin.

Now all you need is an array of character points

char *history[10];
int current = 0; /* this is not strictly needed but may make things easier
to understand */

Intitialise all elements of history to NULL. Then for the first ten lines
read in the lines, incrementing current. When current gets to ten, the array
is full. Therefore free the top entry (history[0]) and call memmove() to
move all the pointers up one place. Then add your newly read line to the
bottom.

That's one way, but why not just retain current as a modulo 10 value rather
than shifting your whole history? For example, using the code I indicated on
the webpage above, we can write simply:

char * history[10] = {NULL, ..., NULL};
int current = 0;

while (...) {
free (history[current % 10U]); /* free(NULL) is ok */
getstralloc (&(history[current % 10U]));
current++;

...

/* Dump the history */
for (i=current-10; i < current; i++) {
if (history[i % 10U]) {
printf ("%d) %s\n", i, history[i % 10U]);
}
}
}

There are ways to dramatically reduce the cost of the "%" operations, but I
will leave that as an exercise to the reader.
 
S

Spartan815

"This may sound a bit radical, but have you tried a... book? You know,
the things made of processed pulp of certain plants (usually trees),
usually rectangular in shape and glued together along the longer side
so you can leaf through them. It is sometimes called "reading"."

-- Wow, way to be a total jerk about it. It was a simple question,
you chould have decided not to post a response at all (and in this
case it probably would have been better). Not that I should even
dignify what you said with a response but here we go...

1) I dont have a car to get to the library
2) I dont have money to go throwing around and buying books beyond the
required text books for classes
3) the text for the class is a book on operating systems -- with
little or no examples of code

and for the above reasons, I was looking for an online resource so I
could do that "reading" thing you talked about from home. You know,
here on my computer, they have those things called webpages that often
include helpful information on a wide range of topics.
 
P

Peter Pichler

Please keep the attributions for the benefit of others.
-- Wow, way to be a total jerk about it.

Indeed. I've just had an unhappy love affair, so I don't see why anybody
else should have a good time ;-)
It was a simple question,

Hmm, let's see:
"is there another function or way of going about getting input from the
command line besides getch()."

First of all, it makes no sense. There is no command line and no getch()
in standard C. You got two independent answers nevertheless, all in good
intentions.
you chould have decided not to post a response at all (and in this
case it probably would have been better).

As you wish. I will not bother the next time. But you should get used to
some level of sarcasm on this newsgroup, if you want to learn something.
1) I dont have a car to get to the library

A bicycle or even your own feet would do.
2) I dont have money to go throwing around and buying books beyond the
required text books for classes

"Throwing around?" Now this must be the most arrogant response I've seen
in years. Knowledge is money, have you heard that? And knowledge /costs/
money too. If you are not willing to invest in your own education, then
do not be surprised if you don't achieve much later.
3) the text for the class is a book on operating systems -- with
little or no examples of code

Then get a second book. The FAQ has some recommendations. It does not
hurt to have books on several topics.
and for the above reasons, I was looking for an online resource so I
could do that "reading" thing you talked about from home. You know,
here on my computer, they have those things called webpages that often
include helpful information on a wide range of topics.

That's all very nice. Howevere, a book is better on many accounts. First,
it is much more readily available. Second, you could make notes in it.
Then there is the thing about reading in bed - if you have a laptop with
wireless internet connection, then you surely have enough money to afford
a book too. And even a laptop would not be much help for reading on the
bus on the way to work or school.

I write all this with the intention to help you. If you want to take
offence, that is your choice. Message ends.

Peter
 
M

Malcolm

Paul Hsieh said:
That's one way, but why not just retain current as a modulo 10 value >
rather than shifting your whole history?As it is we only have ten pointers to move, in the context of user input.
Even an extremely slow computer will do this in under just-noticeable time,
and it keeps things simple.
However generally a circular buffer is a faster way of storing a fixed size
list. If we had many thousands of entries the OP might like to consider
doing this.
 
J

Joona I Palaste

-- Wow, way to be a total jerk about it. It was a simple question,
you chould have decided not to post a response at all (and in this
case it probably would have been better).

*PLONK*
 

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
474,140
Messages
2,570,810
Members
47,357
Latest member
sitele8746

Latest Threads

Top