Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

switch,case and getch()

0 views
Skip to first unread message

Garry Charles

unread,
Feb 6, 1998, 3:00:00 AM2/6/98
to

hi proggers,

can anyone help me with this ...


in c++ version3.......i want to be able to run a program so that
it sets up an initial screen(i've done this bit!) and then wait for a
keypresses to 'open text windows'. i've written all the window calling
routines but the bit that stumps me is the bit for scanning for
keypresses!

how do you put a getch() function inside a switch..case routine ???
--
Garry Charles

Daniel Lawrence

unread,
Feb 6, 1998, 3:00:00 AM2/6/98
to Garry Charles

Ny c++ version 3 I presume you mean version 3 of your compiler. Unless
we know which compiler this isn't much help. However it's probably not
necessary for this question.

You want something along the lines of

char keypressed;
const char exitkey = 'q';
do
{
switch (keypressed = tolower(getch()))
{
case 'a': // action for a
break;
case 'z': // action for z
break;
default: // action for invalid option
}
} while (keypressed != exitkey);

--
Daniel Lawrence, Computer Science Student, University of York
E-mail: Dan...@rcp.co.uk or drl...@york.ac.uk

Will Rose

unread,
Feb 6, 1998, 3:00:00 AM2/6/98
to

Garry Charles (ga...@elton.demon.co.uk) wrote:
: hi proggers,

: can anyone help me with this ...


: in c++ version3.......i want to be able to run a program so that
: it sets up an initial screen(i've done this bit!) and then wait for a
: keypresses to 'open text windows'. i've written all the window calling
: routines but the bit that stumps me is the bit for scanning for
: keypresses!

: how do you put a getch() function inside a switch..case routine ???

Well, if it's C++ should you be using getc()? Try asking in a C++
programming group. Scanning for keypresses is system-specific, so
you'll need to look at what your specific compiler/OS does.


Will
c...@crash.cts.com


Stephan Wilms

unread,
Feb 6, 1998, 3:00:00 AM2/6/98
to

Daniel Lawrence wrote:
>
> Ny c++ version 3 I presume you mean version 3 of your compiler. Unless
> we know which compiler this isn't much help. However it's probably not
> necessary for this question.
>
> You want something along the lines of
>
> char keypressed;
> const char exitkey = 'q';
> do
> {
> switch (keypressed = tolower(getch()))

Now I *know* that C does not have a "getch()" function, and up to today
I thought that C++ didn't have one either. Has C++ been changed behind
my back ?

Stephan
(initiator of the campaign against grumpiness in c.l.c)

Jon Watson

unread,
Feb 6, 1998, 3:00:00 AM2/6/98
to

Heya..

> how do you put a getch() function inside a switch..case routine ???

If I understand you correctly, you want to trap the keypress and evaluate
it.
If that is the case, you have to assign the getch() to something:

variable = getch();

Then you an evaluate variable.

This works in C++ 4.5. I can't really comment on earlier version, but you
might want to try it.

Hope that helps

Jon


Daniel Lawrence

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to Stepha...@cwa.de

I have been using getch() with borland c++ for years. It's defined in
conio.h. I have never found a compiler without it.

Daniel

Rainer Temme

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

Daniel Lawrence wrote:
>
> ...
>
> I have been using getch() with borland c++ for years. It's defined in
> conio.h. I have never found a compiler without it.

But that doesn't mean that getch()/conio.h is part of any standard, does
it?!

Regards Rainer

Daniel Lawrence

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to Rainer Temme

No it doesn't but as there is no similar command within the standard
(correct me if I'm wrong) you have to resort to non-standard functions.
This function may not be standard but it is (as much as any other
non-standard extension) portable.

Daniel

PS. getch() is defined in curses.h on UNIX systems.

Richard Stamp

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

In article <34E030A1...@rcp.co.uk>,

Daniel Lawrence <Dan...@rcp.co.uk> wrote:
>
>I have been using getch() with borland c++ for years. It's defined in
>conio.h. I have never found a compiler without it.

You haven't looked very hard. GCC is a popular compiler available in
several platforms. The following transcript is from a machine running
Linux, one of the most popular UNIX implementations.

rgs20@charon:~/scratch$ cat >test.c
#include <conio.h>

int main (void)
{
int c = getch();
return 0;
}

rgs20@charon:~/scratch$ gcc test.c
test.c:1: conio.h: No such file or directory

rgs20@charon:~/scratch$ g++ test.c
test.c:1: conio.h: No such file or directory

Oops! No conio.h. Maybe if I declare getch() implicitly...

rgs20@charon:~/scratch$ cat >test.c
int main (void)
{
int c = getch();
return 0;
}

rgs20@charon:~/scratch$ gcc test.c
/tmp/cca136501.o: In function `main':
/tmp/cca136501.o(.text+0x7): undefined reference to `getch'

rgs20@charon:~/scratch$ g++ test.c
test.c: In function `int main()':
test.c:3: warning: implicit declaration of function `int getch(...)'
/tmp/cca136551.o: In function `main':
/tmp/cca136551.o(.text+0x7): undefined reference to `getch'

Oh dear. No getch() either.

>Daniel Lawrence, Computer Science Student, University of York

Good Grief! I'd have expected a computer science student to be aware
of the existence of UNIX.

Cheers,
Richard
--
Richard Stamp
Churchill College, Cambridge

Will Rose

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

Daniel Lawrence (Dan...@rcp.co.uk) wrote:
[...]

: I have been using getch() with borland c++ for years. It's defined in


: conio.h. I have never found a compiler without it.

Then you have found very few compilers. I, OTOH, haven't found many
compilers _with_ it; of the four or five compilers I use regularly,
one has this call available, and another has something similar. And
regardless of our different experiences, it isn't part of standard C.


Will
c...@crash.cts.com


Lawrence Kirby

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

In article <34E030A1...@rcp.co.uk>
Dan...@rcp.co.uk "Daniel Lawrence" writes:

...

>I have been using getch() with borland c++ for years.

A compiler does not define the C language.

>It's defined in
>conio.h.

None of the compilers I use here support conio.h.

>I have never found a compiler without it.

All that means is that you have used an extremely limited range of
compilers.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------


Will Rose

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

Lawrence Kirby

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

In article <34E05845...@rcp.co.uk>
Dan...@rcp.co.uk "Daniel Lawrence" writes:

...

>PS. getch() is defined in curses.h on UNIX systems.

And it acts differently to the getch() function supplied with Borland
compilers for DOS.

Kaz Kylheku

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

In article <34E05845...@rcp.co.uk>,

Daniel Lawrence <Dan...@rcp.co.uk> wrote:
>No it doesn't but as there is no similar command within the standard
>(correct me if I'm wrong) you have to resort to non-standard functions.
>This function may not be standard but it is (as much as any other
>non-standard extension) portable.

Extensions are not very portable, that's why they are called extensions.

Some extensions are more portable than others. For example, extensions defined
by major standards such as IEEE POSIX.1, have a wide acceptance among many
platforms, so programs which use their extensions are still fairly portable.

The extensions have to be used properly; you have to include the correct
header files, use the proper feature-test macros, etc.

>Daniel


>
>PS. getch() is defined in curses.h on UNIX systems.

This is thanks to pure historic accident. It is a different function; before
you can use getch(), you must initialize curses with initscr(), and you may
have to disable echo with noecho(), and go into non-canonical mode with
cbreak(). It is not the same function as the MS-DOS getch() from conio.h even
if the name happens to be the same.

I just tried using the curses getch() without a call to initscr(),
and my program *core dumped*.

What's that about getch() being portable?

The ISO 9899:1990 document, also known as the C Standard, states that
if you call a function which is not in the standard library or
which is not defined in one of the translation units of your program,
the behavior is undefined. The getch function does not appear in
the standard library. Undefined behavior means that anything
may happen; you may get a link-time error, or your program may
crash, as happened in my case.

Realize that you are wrong, and stop being a wise-ass---at least hold off
until you graduate.

firewind

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

On Tue, 10 Feb 1998, Daniel Lawrence wrote:

> I have been using getch() with borland c++ for years. It's defined in
> conio.h. I have never found a compiler without it.

'It works on my compiler' is pretty meaningless. getch() and <conio.h> are
-not- standard. As a matter of fact, I've only used -one- compiler that
had it.

--
Attempting to write in a hybrid which can be compiled by either a C compiler
or a C++ compiler produces a compromise language which combines the drawbacks
of both with the advantages of neither.
-- John Winters <jo...@polo.demon.co.uk> in comp.lang.c


firewind

unread,
Feb 10, 1998, 3:00:00 AM2/10/98
to

On Tue, 10 Feb 1998, Daniel Lawrence wrote:

> No it doesn't but as there is no similar command [to getch()] within the

> standard (correct me if I'm wrong)

First, that's function, not command. Second, you -are- wrong. Ever heard
of getc(), fgetc(), or getchar()?

> This function may not be standard but it is (as much as any other
> non-standard extension) portable.

That is to say, it isn't.

Ulric Eriksson

unread,
Feb 11, 1998, 3:00:00 AM2/11/98
to

In article <34E05845...@rcp.co.uk>,
Daniel Lawrence <Dan...@rcp.co.uk> wrote:
>
>No it doesn't but as there is no similar command within the standard
>(correct me if I'm wrong) you have to resort to non-standard functions.
>This function may not be standard but it is (as much as any other
>non-standard extension) portable.

What an odd view on what is portable.

>
>Daniel
>
>PS. getch() is defined in curses.h on UNIX systems.

evelin:~/tmp$ cat getch.c
#include <curses.h>

int main (void)
{
printf("Press any key");
getch();
return 0;
}
evelin:~/tmp$ cc -Wall -ansi -pedantic getch.c -lcurses
evelin:~/tmp$ a.out
Segmentation fault (core dumped)


Ulric
--
Style is always a matter of style.

Giorgos Keramidas

unread,
Apr 16, 1998, 3:00:00 AM4/16/98
to

Lawrence Kirby wrote:
>
> In article <34E030A1...@rcp.co.uk>

> Dan...@rcp.co.uk "Daniel Lawrence" writes:
> ...
>
> >I have been using getch() with borland c++ for years.
>
> A compiler does not define the C language.
>
> >It's defined in
> >conio.h.
>
> None of the compilers I use here support conio.h.

>
> >I have never found a compiler without it.
>
> All that means is that you have used an extremely limited range of
> compilers.

Nah... he just thinks that the whole world is DOS+Borland.
He'll grow up.

0 new messages