473,327 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Proper way to wait for menu command and return?


Disclaimer: I'm new to C.

I'm building a simple text based program and I'm not sure I'm
designing it correctly. What I have is simple and it works but it just
seems weird. Is there a better way? I do still have some debugging
code in the subroutine to print out the key pressed.

Basically menuinput() waits for the key press, and passes it to the
switch below. What I'm confused about is, what is the best way to get
back to the menu input subroutine? At the end of the routine that was
called, should I just call menuinput() again?

Thanks,
/*Raj*/

--------------------

Here is the menu input subroutine:

<code>

int menuinput() {
unsigned char menukey;
fflush (stdout);
menukey = getc();

gotoxy (12,18);
printf("key pressed: %c", menukey);
sleep(3);
clearxy(12,18, 16);

switch(menukey)
{
case 'a':
drstatus();
break;
case 'b':
resetdr();
break;
default:
putsxy(1, 20, "invalid menu option");
sleep(3);
clearxy(1, 20, 20);
menuinput();
break;
}
}

</code>
Mar 4 '06 #1
9 2239
You can use a while loop and put the switch in it.

Mar 4 '06 #2
On 4 Mar 2006 12:38:14 -0800, "broeisi" <br*******@gmail.com> wrote:
You can use a while loop and put the switch in it.

That is also an idea. What about the way back to the menu input
subroutine? Would you call it directly as I have done here or is there
some better way to return?

Thanks,
/*Raj*/
Mar 4 '06 #3
In article <2g********************************@4ax.com>,
RajW <ra********@c64.net> wrote:
Disclaimer: I'm new to C. Basically menuinput() waits for the key press, and passes it to the
switch below. int menuinput() {
unsigned char menukey;
fflush (stdout);
menukey = getc();

gotoxy (12,18);
gotoxy(), clearxy(), and putsxy() are not part of standard C.
printf("key pressed: %c", menukey);


I get the impression that you are expecting that key presses
will be available "immediately" (or nearly so) to the program --
that the program will be able to read off the keys as the user
enters them.

In standard C, there is no way of configuring for "immediate"
input: there is no way that one can be sure that the input will
be available to the program until the user enters the end-of-line
sequence (e.g., often a carriage return that gets translated
to newline by the line protocol layer.)

If you are using gotoxy() and kin, you are using extensions to
standard C and those extensions probably provide a mechanism to
configure single-character I/O -- but it isn't portable to
other systems that do not have the same extension.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Mar 4 '06 #4

"RajW" <ra********@c64.net> wrote in message
news:2g********************************@4ax.com...

Disclaimer: I'm new to C.

I'm building a simple text based program and I'm not sure I'm
designing it correctly. What I have is simple and it works but it just
seems weird. Is there a better way? I do still have some debugging
code in the subroutine to print out the key pressed.

Basically menuinput() waits for the key press, and passes it to the
switch below. What I'm confused about is, what is the best way to get
back to the menu input subroutine? At the end of the routine that was
called, should I just call menuinput() again?


Since this seems OS specific (DOS?), some people aren't responding...

You want to call menuinput() again but not from within menuinput() (i.e.,
delete that line). It would be best if in a loop. Menuinput probably should
return a non-zero value when you want to exit the loop:

int main(void)
{
int status;
do {
/* ... */
status=menuinput();
/* ... */
} while(!status);
}

I don't know which OS your using (looks like DOS), you might want use
non-blocking functions. A non-blocking function doesn't wait for a
response. This allows your loop to keep executing and polling other events,
like a mouse:

int main(void)
{
int status;
do {
/* ... */
status=menuinput();
status|=mouseinput();
/* ... */
} while(!status);
}

As soon as the non-blocking mouse or keyboard returns a "event," your loop
exits.
Rod Pemberton
Mar 5 '06 #5
RajW wrote:

Disclaimer: I'm new to C.

I'm building a simple text based program and I'm not sure I'm
designing it correctly. What I have is simple and it works but it just
seems weird. Is there a better way? I do still have some debugging
code in the subroutine to print out the key pressed.

Basically menuinput() waits for the key press, and passes it to the
switch below. What I'm confused about is, what is the best way to get
back to the menu input subroutine? At the end of the routine that was
called, should I just call menuinput() again?


Try this routine. It is portable, unlike the <snipped> code you
posted. Compile with -DTESTING (under gcc) for a test program.

/* -------------------- file menuget.h -------------------- */
/* public domain, by C.B. Falconer. Attribution appreciated */

#ifndef H_menuget_h
# define H_menuget_h

# ifdef __cplusplus
extern "C" {
# endif

#include <stdio.h>

/* ------------------ */

int skipblanks(FILE *f);

/* ------------------ */

int flushln(FILE *f);

/* ------------------ */

/* Print optional menu and get a single char response. Repeat
as needed until the response is in the acceptable string.
If the first char in acceptable is uppercase, use that as
a default response when a blank line is entered
*/
int menuget(const char *prompt, /* whatever prompt is needed */
const char *acceptable); /* list of acceptable ans */

# ifdef __cplusplus
}
# endif

#endif
/* ----------- file menuget.h ------------ */

/* -------------------- file menuget.c -------------------- */
/* public domain, by C.B. Falconer. Attribution appreciated */

#include <ctype.h>
#include <string.h>
#include "menuget.h"

/* ------------------ */

int skipblanks(FILE *f)
{
int ch;

while (' ' == (ch = getc(f))) continue;
return ch;
} /* skipblanks */

/* ------------------ */

int flushln(FILE *f)
{
int ch;

while (('\n' != (ch = getc(f))) && (EOF != ch)) continue;
return ch;
} /* flushln */

/* ------------------ */

/* Print optional menu and get a single char response. Repeat
as needed until the response is in the acceptable string.
If the first char in acceptable is uppercase, use that as
a default response when a blank line is entered
*/
int menuget(const char *prompt, /* whatever prompt is needed */
const char *acceptable) /* list of acceptable ans */
{
int ch;

do {
if (prompt) {
printf("%s", prompt); fflush(stdout);
}
/* Now get a reply */
ch = skipblanks(stdin); /* get first non-blank char */
if ('\n' != ch) flushln(stdin); /* set up to try again */
else if (isupper(acceptable[0]))
ch = acceptable[0]; /* 1st is default entry */
} while (!strchr(acceptable, ch) && (EOF != ch));
return ch;
} /* menuget */

#ifdef TESTING

/* ------------------ */

int main(void)
{
int ch;

do {
ch = menuget("\nA a all cases\n"
"n nonsense\n"
"x exit : ",
"Aanx");
switch (ch) {
case 'a':
case 'A': puts("All cases"); break;
case 'n': puts("nonsense"); break;
case 'x':
case EOF: /* ignore here */ break;
default: puts("Ooops"); break;
}
} while (('x' != ch) && (EOF != ch));

if (EOF == ch) puts("EOF encountered");
return 0;
} /* main */

#endif
/* ----------- file menuget.c ------------ */

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>

Mar 5 '06 #6
On Sat, 4 Mar 2006 19:27:11 -0500, "Rod Pemberton"
<do*********@sorry.bitbucket.cmm> wrote:
Since this seems OS specific (DOS?), some people aren't responding...


You'll get a laugh when I tell what I'm programming this for...

You ready?

A Commodore 64! I'm using CC65 (6502 cross compiler) to create a
program to help me troubleshoot 1541 disk drives.

Thanks,
/*Raj*/
Mar 5 '06 #7

"RajW" <ra********@c64.net> wrote in message
news:bm********************************@4ax.com...
On Sat, 4 Mar 2006 19:27:11 -0500, "Rod Pemberton"
<do*********@sorry.bitbucket.cmm> wrote:
Since this seems OS specific (DOS?), some people aren't responding...


You'll get a laugh when I tell what I'm programming this for...

You ready?

A Commodore 64! I'm using CC65 (6502 cross compiler) to create a
program to help me troubleshoot 1541 disk drives.


No I won't. I've got a stack of them in the basement, various disks, C64
Programmers Reference with schematics, ABACUS books with dissassembled C64
and 1541 roms, the Howard W. Sams schematics for the 1541, original MOS
Technologies 6502 manual etc., etc... I think they were last fired up
around 92-94...(Ironic, I was programming on VAXen then...But, of course the
PC didn't get a decent color video card until 94 either...The Amiga's had
that in 86)

Saving some games? Certain copyrighted 5 1/4 disks had a visible dot on the
bottom side, called a 'laser dot' where the disk wouldn't store data. The
track would return an error it you attempted to read it and wouldn't save
data if you attempted to write to it.

The early (brown) 1541's had a low quality metal frame which expanded too
much when heated. So some disks are only readable when the drive is cool
and others only when hot. They also had problems maintaining alignment of
track zero. IIRC, all but the last version of the brown 1541's had a trim
pot inside that could be used to adjust this but you needed the Sams
schematics and an oscilloscope. The final version of the brown drives they
changed and reduced the circuitry. The later (light tan, or was it white?)
drives worked properly. You may still be able to find the C64 Hacking zine
which was available from around 92-94. There were schematics of 1541 to PC
interface cables which were pretty simple. A number of decent emulators for
the PC popped up a few years later. You might want to check to see if the
MAME arcade emulator runs C64 games now...

The 1541 had it's own cpu, ram, and bios with bunch of commands that you
could send from the C64 to program it. Let's see, track 18 was the
directory right? For files, the first two bytes of each sector was a
pointer to the next sector in the file chain. Sort of like, a distributed
FAT table. It used a layout that was a holdover from the CBM mainframes.

At one point in time I had written a 6510 disassembler which showed how each
instruction affected each flag. I used this to allow me to write pieces of
code, each of which only modified a certain flag. Essentially, this was
extremely primitive multitasking or threading.
Rod Pemberton
Mar 5 '06 #8
On Sun, 5 Mar 2006 04:52:48 -0500, "Rod Pemberton"
<do*********@sorry.bitbucket.cmm> wrote:
No I won't. I've got a stack of them in the basement, various disks, C64
Programmers Reference with schematics, ABACUS books with dissassembled C64
and 1541 roms, the Howard W. Sams schematics for the 1541, original MOS
Technologies 6502 manual etc., etc... I think they were last fired up
around 92-94...(Ironic, I was programming on VAXen then...But, of course the
PC didn't get a decent color video card until 94 either...The Amiga's had
that in 86)

Impressive! You should come over to comp.sys.cbm ... quite a few of us
still left.

I've been repairing 1541's and other Commodore equipment for quite a
while now. I just needed a tool to help me which I had not seen
anywhere before... So I wrote it. :)

Thanks,
/*Raj*/
Mar 5 '06 #9
On Sat, 4 Mar 2006 19:27:11 -0500, "Rod Pemberton"
<do*********@sorry.bitbucket.cmm> wrote:
int main(void)
{
int status;
do {
/* ... */
status=menuinput();
status|=mouseinput();
/* ... */
} while(!status);
}


Good idea (and programming methodology). :) I'll rewrite that portion
of my program.

Thanks!
/*Raj*/
Mar 5 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
1
by: eltronic | last post by:
here is some code to add right click context menus. in the few apps I've edited sofar, it just works, once you run rCbinder with the root of the gui or bind B3 all present and future widgets will...
3
by: Naomi | last post by:
hello Could anyone please tell me if there is any way i can have the text "Menu Loading Please Wait..." appear while the js menu loads and then disappear after it loads ? hoping to hear some...
0
by: Noulouk | last post by:
Hi, I build an add-in for Visual Studio 2005. I use the wizzard, I fixed the bug with envdte reference and build my project immediately after the wizzard. All seems to work, the add-in is shown...
5
by: yxq | last post by:
Hi I am build vb6 Context menu extension, but how to determine which popup menu item(popupItem1 and popupItem2) was clicked? Thanks The code ' ' IContextMenu::QueryContextMenu '
2
by: gilad | last post by:
Hi, I seem to be having a problem getting a context menu to work in Explorer. The menu item installs fine, but when I click it a message box should pop up indicating the command was received and...
2
by: Dustan | last post by:
I don't know if this is because of Tkinter (ie Tk) itself or the Windows default way of handling things, but when I create a very long menu (my test is shown below), the way it displays is rather...
1
by: Gigs_ | last post by:
what is the best way to write tkinter menus? As toplevels or as frame with Menubutton? im doing like this class MyWidget(Frame): def __init__(self, master=None): """ should this master be...
6
by: roguefeebo | last post by:
Hello everyone, thanks for looking. I'll get right to the point: I'm having problems trying to figure out a hierarchy of menus in C; command-line style. Say, for example the program analyzes a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.