getchar() question

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

tm said:
There are several problems in this area:
- Reading characters without waiting for RETURN
- Reading characters without echoing them on the screen
- Determine if a key has been pressed
- Write to a specific screen position
- Positioning the cursor

Years ago I wrote a portable library to address this
problems. This library interfaces to different functions
on Windows and Linux. I even found some errors in curses,
so I based this library on terminfo instead of curses.

Long ago I stopped supporting this library in its form
as simple C library. Instead I improved it and now it is
part of the Seed7 runtime library. As such it is tailored
towards Seed7 (e.g.: It uses Seed7 strings instead of C
strings). With this library Seed7 supports all the
keyboard and screen related things in a portable way.
A program which uses the keypressed function is here:
http://seed7.sourceforge.net/examples/txtclock.htm

Every experienced programmer is able to convert my
keyboard/screen library, so that it is usable from C.
But for you this is probably byond your capabilities.

I have seen you struggle with many simple things in C.
I guess that C is not the right programming language for
you. My advice is: Use a different programming language.

I don't intend to convert you to Seed7. You can choose
whatever you want, but you really should move away from
C to a "higher level" language.

I appreciate your concern and interest. I have not ruled this idea out
either. I have never heard of seed7 until you mentioned it. I have looked at
java too though not too seriously. I have come to understand the std
functions to a good degree but the fundamental basics of C still escape me.
I don't know if I am not looking at good code and still reading very basic
books or what. I also have a lot of trouble remembering what I've just read
so that handicaps me. Keith has pointed me to a FAQ question and that
question does answer somewhat my problem but I do know how to remove a
character just typed by using ungetchar().
Also my life doesn't revolve around C or programming so it takes me a
little more time to do things. Try taking 4mg of klonpin a day too and you
might have trouble staying in conversations.

Bill
 
O

osmium

Bill Cunningham said:
Oops. What a day. Better to post the code than not to.
Sorry.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
if (argc != 1) {
fputs("usage error\n", stderr);
exit(1);
}
int index, a;
if ((a = getchar()) > 256)
exit(0);
else {
puts("enter");
}
}

I can't tell where you are at the moment in this program. Your original
question has been answered with more than one answer; use getche(FAQ answer)
use kbhit (my answer plus one other). The FAQ seems to me to be written for
someone who already knows the answer. (Where in God's name did this word
"echo" come from?)

Nevertheless.

Your program seems designed to detect the presence or absence of EOF by some
less than obvious means. Is that true?

Some questions for you.
What OS and compiler do you use?
Is "index" used in the above program?
Do you know that the range of char is typically 0..255 and not 1..256?
Do you realize you have not told the compiler to display the word "enter"?
Do you realize that your program has three return points but only specifies
a return value for two of them?
I don't see any easy way for the programmer to detect whether the program
works or not.
You program is very unfriendly, it gives the user no guidance at all as to
what to do.
 
B

Bill Cunningham

osmium said:
I can't tell where you are at the moment in this program.

At the beginning. There's quite a bit more to add to the idea I had.

Your
original question has been answered with more than one answer; use
getche(FAQ answer) use kbhit (my answer plus one other). The FAQ
seems to me to be written for someone who already knows the answer.
(Where in God's name did this word "echo" come from?)

Nevertheless.

Your program seems designed to detect the presence or absence of EOF
by some less than obvious means. Is that true?

Yes! Yes!
Some questions for you.
What OS and compiler do you use?
Is "index" used in the above program?

Not yet. And I forgot to clip it out. And in the above program, no.
Do you know that the range of char is typically 0..255 and not 1..256?
No

Do you realize you have not told the compiler to display the word
"enter"? Do you realize that your program has three return points but
only specifies a return value for two of them?
I don't see any easy way for the programmer to detect whether the
program works or not.
You program is very unfriendly, it gives the user no guidance at all
as to what to do.

gcc-3.4.6 on linux. The original idea was to have two loops. one
beginning at 5000 and counting down to 1. One beginning at 1 and counting to
5000. Though that wouldn't take my machine many clock cycles to process. So
I would need a bigger number. At the instant a key was pressed the loops
would terminate their values added together to obtain a number. A "key"
generated.

People say they have trouble reading my code but I find it easy to read.
However on the other hand other's code is hard for me to read even when
heavily commented. Since most people write their code in shorthand I am left
in the dust. What's wrong with for ? Does one have to use ? and : for
example.

Bill
 
O

osmium

Bill Cunningham said:
At the beginning. There's quite a bit more to add to the idea I had.

Your

Yes! Yes!


Not yet. And I forgot to clip it out. And in the above program, no.


gcc-3.4.6 on linux. The original idea was to have two loops. one
beginning at 5000 and counting down to 1. One beginning at 1 and counting
to 5000. Though that wouldn't take my machine many clock cycles to
process. So I would need a bigger number. At the instant a key was pressed
the loops would terminate their values added together to obtain a number.
A "key" generated.

It sounds like you want to generate a random number which you will use
subsequently as a key.
This following works for me on Windows with DevC but it does not have your
detector for a bad command line or for EOF. kbhit() is for Windows, Linux
might have some equivalent or else getch() or getche() in curses or ncurses.
You can build kbhit from either of the getch functions if you can't find an
equivalent to kbhit.. If you have problems in that area, post to a Linux
group or look for existing threads already posted, this is a very common
problem.
----------------
#include <stdio.h>
#include <conio.h>

int main()
{
unsigned long int key;
while(1)
{
key++;
if(kbhit())
break;
}
// If you want an EOF detector, put it here
printf("The new key is: %li \n", key);
// Remove the next two lines if not using DevC.
getchar();
getchar();
}
------------------
When the value of key overflows the maximum value possible for a long int,
it will simply start over at zero. Note that key is not initialized on
purpose, that way the initial value is just another part of the
unpredictable value of key, and you *want* unpredictable
People say they have trouble reading my code but I find it easy to
read. However on the other hand other's code is hard for me to read even
when heavily commented. Since most people write their code in shorthand I
am left in the dust. What's wrong with for ? Does one have to use ? and :
for example.

No, you don't need to use that form. Note that on p 51, K&R say that the
ternary operator "provides an *alternate* way". to do things. It is simply
an alternate that is more concise than the primitive way; the last thing you
want is for someone to be extra concise.
 
B

Bill Cunningham

osmium wrote:

[snip]
..
It is simply an alternate that is more concise than the primitive
way; the last thing you want is for someone to be extra concise.

Everything I run into seems to be extra concise and written in
shorthand. I trouble following a lot of it.

Bill
 

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

Similar Threads


Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top