getchar() question

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have this little code that complies fine and I guess it does what it's
written to do. Trouble is I want the system to exit by pressing any key not
just enter. Would this have something to do with what the int a has stored
and what getchar() will return? Then I want to move on with my idea. Please
excuse the system specific numbers passed to exit().

Bill
 
O

osmium

Bill Cunningham said:
I have this little code that complies fine and I guess it does what
it's written to do. Trouble is I want the system to exit by pressing any
key not just enter. Would this have something to do with what the int a
has stored and what getchar() will return? Then I want to move on with my
idea. Please excuse the system specific numbers passed to exit().

Search Google Groups for getch.
 
M

Malcolm McLean

    I have this little code that complies fine and I guess it does what it's
written to do. Trouble is I want the system to exit by pressing any key not
just enter. Would this have something to do with what the int a has stored
and what getchar() will return? Then I want to move on with my idea. Please
excuse the system specific numbers passed to exit().
You need to use a non-standard library function. It's years since I
did any command window programming with such a library, but usually
the function is called kbhit().
 
S

Seebs

You need to use a non-standard library function.

Quite right.
It's years since I
did any command window programming with such a library, but usually
the function is called kbhit().

"Usually"?

Wouldn't that imply that, out of the more than a dozen such ways of doing
this, more than one had named the function that? I've only seen one target
that had a "kbhit()".

I also can't understand why anyone still responds to "Bill Cunningham". He's
a troll who posts elaborately designed stuff which it is impossible for
someone to have gotten wrong that way; in order to make these mistakes, you'd
need to know enough not to make them. He's been pretending to be stuck
at the "first week of C programming experience" for several years, and
frankly, I think around the time he announced that he was planning on writing
a new database system faster and more robust than Oracle, but couldn't figure
out how to store strings in a file, people shoulda caught on.

-s
 
O

osmium

Lew Pitcher said:
How would the curses getch() function help him?

Well, darn, I guess it wouldn't. It is the first thing that comes into my
mind when this general subject is mentioned. It has been a while since I
have done any of this stuff. I guess he wants kbhit as someone else has
already mentioned.
 
D

David Resnick

9?  I'd give it a 6 a best.

I'd have to agree 9 is too high. So is 6. Compared to some of the
past gems he has written, this is quite low on the list. The ones
that show C code are surely much more worthy of a high rank, as are
the ones that mention IPC or databases or grand projects.

-David
 
S

Seebs

Well, darn, I guess it wouldn't. It is the first thing that comes into my
mind when this general subject is mentioned. It has been a while since I
have done any of this stuff. I guess he wants kbhit as someone else has
already mentioned.

Uh, maybe.
Sorry Bill, look for kbhit on Google Groups. And note that curses is Unix
speak. The DOS/Windows equivalent is conio.h.

Yeah, uhm.

It's entirely possible that the OP isn't primarily looking for a DOS interface
that was obsolete ten years ago. It's also entirely possible that the OP
isn't looking for curses.

Lew's point was that your answer was simultaneously off-topic and most likely
wrong. You appear to be sort of having trouble with this.

The real answer to that question, if it were asked by a non-troll, would
probably be to start by jumping back and asking why you think you want to
do that. The chances that anything from <conio.h> would be the right
choice, or even make an appearance in a top-ten list of plausible right
choices, are basically nil. The answer to any question posted by "Bill
Cunningham" is to stop feeding the trolls.

-s
 
K

Keith Thompson

Bill Cunningham said:
I have this little code that complies fine and I guess it does what it's
written to do. Trouble is I want the system to exit by pressing any key not
just enter. Would this have something to do with what the int a has stored
and what getchar() will return? Then I want to move on with my idea. Please
excuse the system specific numbers passed to exit().

In other words:

How can I read a single character from the keyboard without waiting for
the RETURN key? How can I stop characters from being echoed on the
screen as they're typed?

<http://www.c-faq.com/>, question 19.1.
 
J

jacob navia

Le 07/01/11 18:36, Bill Cunningham a écrit :
I have this little code that complies fine and I guess it does what it's
written to do. Trouble is I want the system to exit by pressing any key not
just enter. Would this have something to do with what the int a has stored
and what getchar() will return? Then I want to move on with my idea. Please
excuse the system specific numbers passed to exit().

Bill
Yeah, a very difficult problem Mr Cunningham. I do not see any other
solution than to buy immediately a PREMIUM version of lcc-win32.

In there you will find a wonderful NEW and IMPROVED clone of BorlandC
"gotoxy" functions and among those oldies but goodies you will find
also kbhit();

This mysterious function will wait (patiently of course) till you
hit the keyboard (any key will do).

You should buy the special version at
www.q-software-solutions.de at a special price of ONLY 250 US$.

Just enter your Visa card number and all your problems will
disappear. Together with the 250 US$ of course.

Your sincerely

An lcc-win user
 
B

Bill Cunningham

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");
}
}
 
M

Malcolm McLean

In there you will find a wonderful NEW and IMPROVED clone of BorlandC
"gotoxy" functions and among those oldies but goodies you will find
also kbhit();

This mysterious function will wait (patiently of course) till you
hit the keyboard (any key will do).
My memory is coming back to me.

kbhit() was what you used for video games. It returned 1 if a key was
currently depressed, 0 if it wasn't.

getch() was what Bill needs. It waited until a key was pressed and
released, then returned its character code.
 
K

Keith Thompson

Malcolm McLean said:
My memory is coming back to me.

kbhit() was what you used for video games. It returned 1 if a key was
currently depressed, 0 if it wasn't.

getch() was what Bill needs. It waited until a key was pressed and
released, then returned its character code.

I seriously doubt that getch() will do Bill any good, even if you
decide which of several incompatible functions of that name
you're talking about.
 
M

Malcolm McLean

I seriously doubt that getch() will do Bill any good, even if you
decide which of several incompatible functions of that name
you're talking about.
I'd like to write a command console game again. The problem is that
it's so hard these days to get the right resources. Everything is so
wrapped up in windowing systems and layers of abstraction that nothing
just works.
 
T

tm

    Ok. I thought I might be missing something and should mention to the
group for support. Thanks

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.


Greetings Thomas Mertes

--
Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
 
S

Seebs

I'd like to write a command console game again. The problem is that
it's so hard these days to get the right resources. Everything is so
wrapped up in windowing systems and layers of abstraction that nothing
just works.

For stuff that's just text moving around the screen and keyboard input,
there's plenty of stuff that just works. There's some very nice
tools out there which work at the right level of abstraction and don't
require you to build all the frameworks up yourself.

-s
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top