getchar() question

Discussion in 'C Programming' started by Bill Cunningham, Jan 7, 2011.

  1. 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
     
    Bill Cunningham, Jan 7, 2011
    #1
    1. Advertising

  2. Bill Cunningham

    osmium Guest

    "Bill Cunningham" wrote:

    > 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.
     
    osmium, Jan 7, 2011
    #2
    1. Advertising

  3. Bill Cunningham

    Lew Pitcher Guest

    On January 7, 2011 12:59, in comp.lang.c, wrote:

    > "Bill Cunningham" wrote:
    >
    >> 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.


    How would the curses getch() function help him?

    --
    Lew Pitcher
    Master Codewright & JOAT-in-training | Registered Linux User #112576
    Me: http://pitcher.digitalfreehold.ca/ | Just Linux: http://justlinux.ca/
    ---------- Slackware - Because I know what I'm doing. ------
     
    Lew Pitcher, Jan 7, 2011
    #3
  4. On Jan 7, 7:36 pm, "Bill Cunningham" <> wrote:
    >     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().
     
    Malcolm McLean, Jan 7, 2011
    #4
  5. Bill Cunningham

    Seebs Guest

    On 2011-01-07, Malcolm McLean <> wrote:
    > On Jan 7, 7:36?pm, "Bill Cunningham" <> wrote:
    >> ? ? 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.


    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
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Jan 7, 2011
    #5
  6. Bill Cunningham

    osmium Guest

    "Lew Pitcher" wrote:

    > On January 7, 2011 12:59, in comp.lang.c, wrote:
    >
    >> "Bill Cunningham" wrote:
    >>
    >>> 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.

    >
    > 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.
    ---
    Sorry Bill, look for kbhit on Google Groups. And note that curses is Unix
    speak. The DOS/Windows equivalent is conio.h.
     
    osmium, Jan 7, 2011
    #6
  7. Bill Cunningham

    Ian Collins Guest

    On 01/ 8/11 07:03 AM, Richard wrote:
    > "Bill Cunningham"<> writes:
    >
    >> 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().

    >
    > 9/10!


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

    --
    Ian Collins
     
    Ian Collins, Jan 7, 2011
    #7
  8. On Jan 7, 2:42 pm, Ian Collins <> wrote:
    > On 01/ 8/11 07:03 AM, Richard wrote:
    >
    > > "Bill Cunningham"<>  writes:

    >
    > >>      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().

    >
    > > 9/10!

    >
    > 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
     
    David Resnick, Jan 7, 2011
    #8
  9. Bill Cunningham

    Seebs Guest

    On 2011-01-07, osmium <> wrote:
    > 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
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Jan 7, 2011
    #9
  10. "Bill Cunningham" <> writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 7, 2011
    #10
  11. Bill Cunningham

    jacob navia Guest

    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
     
    jacob navia, Jan 7, 2011
    #11
  12. Bill Cunningham

    Tim Rentsch Guest

    Richard <> writes:

    > "Bill Cunningham" <> writes:
    >
    >> 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
    >>

    >
    >
    > 9/10!


    I agree. Oh wait... you did mean this as 9/(10!), right?
     
    Tim Rentsch, Jan 7, 2011
    #12
  13. 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");
    }
    }
     
    Bill Cunningham, Jan 7, 2011
    #13
  14. Lew Pitcher wrote:
    >> Search Google Groups for getch.

    >
    > How would the curses getch() function help him?


    Yes I am aware of getch() in ncurses or curses. I'm sorry I forgot to
    post my code.

    B
     
    Bill Cunningham, Jan 7, 2011
    #14
  15. Keith Thompson wrote:

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


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

    B
     
    Bill Cunningham, Jan 7, 2011
    #15
  16. On Jan 7, 10:40 pm, jacob navia <> wrote:
    >
    > 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.
     
    Malcolm McLean, Jan 9, 2011
    #16
  17. Malcolm McLean <> writes:
    > On Jan 7, 10:40 pm, jacob navia <> wrote:
    >>
    >> 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.


    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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jan 9, 2011
    #17
  18. On Jan 9, 11:48 am, Keith Thompson <> wrote:
    > Malcolm McLean <> writes:
    > > On Jan 7, 10:40 pm, jacob navia <> wrote:

    >
    > >> 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.

    >
    > 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.
     
    Malcolm McLean, Jan 9, 2011
    #18
  19. Bill Cunningham

    tm Guest

    On 7 Jan., 23:54, "Bill Cunningham" <> wrote:
    > Keith Thompson wrote:
    > > <http://www.c-faq.com/>, question 19.1.

    >
    >     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.
     
    tm, Jan 9, 2011
    #19
  20. Bill Cunningham

    Seebs Guest

    On 2011-01-09, Malcolm McLean <> wrote:
    > 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
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Jan 9, 2011
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cam

    Use of getchar

    Cam, May 30, 2004, in forum: C++
    Replies:
    3
    Views:
    526
    John Carson
    May 30, 2004
  2. Kristo

    getchar vs. cin

    Kristo, Mar 23, 2005, in forum: C++
    Replies:
    1
    Views:
    2,975
    Thomas Matthews
    Mar 24, 2005
  3. Replies:
    21
    Views:
    1,212
    Richard Herring
    Apr 20, 2005
  4. Ben Fitzgerald

    Re: getchar returns int, assign to array of char?

    Ben Fitzgerald, Jun 26, 2003, in forum: C Programming
    Replies:
    9
    Views:
    955
    Ben Fitzgerald
    Jun 27, 2003
  5. neras
    Replies:
    25
    Views:
    923
    santosh
    Mar 17, 2006
Loading...

Share This Page