making a VERY simple menu

T

tricard

Good evening,

I was thinking about making a very simple DOS based (console) menu for
a project that i am working on, but I have no idea where to start for
something like this. Does anyone have any resources they could point me
to or shed some light on what it is that you do exactly. Like I said, I
don't want a real complicated menu, just a title and about three
options. I will add more stuff once I know what it is that I am doing.

thanks in advance

Tim
 
U

Ulrich Eckhardt

I was thinking about making a very simple DOS based (console) menu for
a project that i am working on, but I have no idea where to start for
something like this. Does anyone have any resources they could point me
to or shed some light on what it is that you do exactly. Like I said, I
don't want a real complicated menu, just a title and about three
options. I will add more stuff once I know what it is that I am doing.

Print options using printf(). Input the user's choice using scanf(). Don't
forget to check for errors in the input part. Repeat until you have a
valid choice.

Just try it, it's really simple.

Uli
 
M

Michael Mair

Good evening,

I was thinking about making a very simple DOS based (console) menu for
a project that i am working on, but I have no idea where to start for
something like this. Does anyone have any resources they could point me
to or shed some light on what it is that you do exactly. Like I said, I
don't want a real complicated menu, just a title and about three
options. I will add more stuff once I know what it is that I am doing.

Depends. If you want fancy stuff using arrow keys etc.,
then you are wrong here as we discuss only standard C.
A newsgroup discussing DOS will probably be able to point
you in the right direction.
If you stick with standard C, you can do something along
the lines
do {
/* Output options */
fflush(stdout);
/* Read in the reply, do not use *scanf() see clc faq */
/* variant a) safe user choice */
} while (!validchoice);
/* a) dispatch according to choice*/
or
do {
/* Output options */
fflush(stdout);
/* Read in the reply, do not use *scanf() see clc faq */
/* variant b) dispatch according to non-exit choice */
} while (!exitmenu);

Cheers
Michael
 
V

Vladimir S. Oka

Ulrich said:
Print options using printf(). Input the user's choice using scanf().
Don't forget to check for errors in the input part. Repeat until you
have a valid choice.

I'd suggest to use printf()/fgets()/sscanf() instead, just to be on the
safe side (NOT gets()), and /do/ check for invalid input.
Just try it, it's really simple.

I concur.

Cheers

Vladimir
 
T

Tim Ricard

Thanks for the help. Okay, so just one question then, just so I know if
this is a standard C question or not. What if I want some items to be
static on the screen (like at the bottom have "press F3 for help"
etc.)? Is this a standard C question or more specific to a DOS C group?

Tim
 
F

Flash Gordon

Tim said:
Thanks for the help. Okay, so just one question then, just so I know if
this is a standard C question or not. What if I want some items to be
static on the screen (like at the bottom have "press F3 for help"
etc.)? Is this a standard C question or more specific to a DOS C group?

You can't do it in standard C not can you portably detect the F3 key, so
you will have to go to a DOS group for this.

By the way, asking here if something is possible in standard C is
perfectly reasonable even when the answer turns out to be no.
 
O

osmium

Tim Ricard said:
Thanks for the help. Okay, so just one question then, just so I know if
this is a standard C question or not. What if I want some items to be
static on the screen (like at the bottom have "press F3 for help"
etc.)? Is this a standard C question or more specific to a DOS C group?

No, function keys and their mere existence are outside the scope of standard
C.
 
V

Vladimir S. Oka

Please quote what you reply to...

Tim said:
Thanks for the help. Okay, so just one question then, just so I know if
this is a standard C question or not. What if I want some items to be
static on the screen (like at the bottom have "press F3 for help"
etc.)? Is this a standard C question or more specific to a DOS C group?

Tim

Standard C does not know anytrhing about the "screen". ;-)

What I'd suggest, if you wanted 100% standard C and portability, is to
simulate celaring the screen by outputing /many/ blank lines when
re-drawing the menu. Scrolling may just send the old menu off the
screen. You'll never know how many is enough for any given machine, but
at least the old menu will be far away from the new.

Otherwise, ask about your specific platform in appropriate group. You
mention DOS, so comp.os.msdos.programmer may be the right place.

Cheers

Vladimir
 
C

CBFalconer

Tim said:
Thanks for the help. Okay, so just one question then, just so I know if
this is a standard C question or not. What if I want some items to be
static on the screen (like at the bottom have "press F3 for help"
etc.)? Is this a standard C question or more specific to a DOS C group?

It's not. There is no mention of 'screen' in the C standard.

Please include adequate context. See below for how on the broken
google interface to usenet.

--
"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/>
 
K

Keith Thompson

Vladimir S. Oka said:
Standard C does not know anytrhing about the "screen". ;-)

What I'd suggest, if you wanted 100% standard C and portability, is to
simulate celaring the screen by outputing /many/ blank lines when
re-drawing the menu. Scrolling may just send the old menu off the
screen. You'll never know how many is enough for any given machine, but
at least the old menu will be far away from the new.

Sure, but I think this is one of the cases where 100% standard C just
isn't worth the effort. If you really need to clear the screen, I'd
say you're better off finding out how to do it on each system you want
to support and writing a clear_screen function with however many
#ifdefs you need. (You can even resort to printing a bunch of blank
lines if all the #ifdefs fail.) The result is going to look a *lot*
better.

Then again, it's not likely that clearing the screen is going to be
the only system-specific think you want to do. If you want to be able
to move the cursor to an arbitrary position on the screen, for
example, you'll need to use some more system-specific code, which is
likely to become a fairly large part of your program.
 
B

Brian Dude

Tim Ricard said:
Thanks for the help. Okay, so just one question then, just so I know if
this is a standard C question or not. What if I want some items to be
static on the screen (like at the bottom have "press F3 for help"
etc.)? Is this a standard C question or more specific to a DOS C group?

Tim

I've often had good luck at:

comp.os.msdos.programmer

That's where I go when I have questions on DOS console applications,
function key access, etc.

Good luck!

Brian Dude
 
E

Emmanuel Delahaye

(e-mail address removed) a écrit :
I was thinking about making a very simple DOS based (console) menu for
a project that i am working on, but I have no idea where to start for
something like this. Does anyone have any resources they could point me
to or shed some light on what it is that you do exactly. Like I said, I
don't want a real complicated menu, just a title and about three
options. I will add more stuff once I know what it is that I am doing.

It's quite a simple algorithm:

DO
printout the options
entry an option
test the entry
execute the corresponding action
UNTIL end

do your best and post the code if you are stuck.
 
K

Keith Thompson

Keith Thompson said:
Then again, it's not likely that clearing the screen is going to be
the only system-specific think you want to do. If you want to be able
to move the cursor to an arbitrary position on the screen, for
example, you'll need to use some more system-specific code, which is
likely to become a fairly large part of your program.

And if clearing the screen *is* the only system-specific thing you
want to do, pleaes reconsider whether you really need to do that.
Clearing my screen for a full-screen text editor is ok; erasing what
might be important information just so your program's prompt can be at
the top of a blank screen is rude.
 
J

Joe Wright

Keith said:
And if clearing the screen *is* the only system-specific thing you
want to do, pleaes reconsider whether you really need to do that.
Clearing my screen for a full-screen text editor is ok; erasing what
might be important information just so your program's prompt can be at
the top of a blank screen is rude.

After a couple of conio.h episodes several years ago, I gave up on CLI
stuff altogether. Now I think to take it up again 'portably' using
curses. Theoretically the same simple menu could run on Unix, Sun and
DOS as well. Although curses is not Standard it is ubiquitous. Is it
portable?
 
R

Robert Gamble

Good evening,

I was thinking about making a very simple DOS based (console) menu for
a project that i am working on, but I have no idea where to start for
something like this. Does anyone have any resources they could point me
to or shed some light on what it is that you do exactly. Like I said, I
don't want a real complicated menu, just a title and about three
options. I will add more stuff once I know what it is that I am doing.

Here is a simple example of a menu in Standard C:

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

int main (void) {
int long c = 0;
char buf[80];
puts("Select an option");
puts("1 First option");
puts("2 Second option");
puts("3 Third option");
while (fgets(buf, 100, stdin) != NULL) {
c = strtol(buf, NULL, 10);
switch (c) {
case 1: case 2: case 3:
printf("You selected option %ld\n", c);
break;
default:
puts("Invalid option");
break;
}
}
return 0;
}

The only case not explicitly handled is when a user inputs n characters
at a time where n is greater than 79, in this case the loop will run
n/79+1 times. If this is not what you would like you can drain the
input buffer after each call to fgets (there is no standard function
specifically for this purpose) or use some scanf magic. Also note that
if someone enters "2h", this will behave as if the user entered "2".

If you want to do anything fancy like cursor placement, unbuffered
non-blocking i/o, colors, etc. you will need to step outside Standard
C. In that case you may want to check out ncurses (which is off-topic
here so if you have questions about it ask elsewhere).

Robert Gamble
 
R

Robert Gamble

Robert said:
Good evening,

I was thinking about making a very simple DOS based (console) menu for
a project that i am working on, but I have no idea where to start for
something like this. Does anyone have any resources they could point me
to or shed some light on what it is that you do exactly. Like I said, I
don't want a real complicated menu, just a title and about three
options. I will add more stuff once I know what it is that I am doing.

Here is a simple example of a menu in Standard C:

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

int main (void) {
int long c = 0;
char buf[80];
puts("Select an option");
puts("1 First option");
puts("2 Second option");
puts("3 Third option");
while (fgets(buf, 100, stdin) != NULL) {

that line should be:

while (fgets(buf, 80, stdin) != NULL) {

you always notice these things right *after* you hit submit...

Robert Gamble
 
K

Keith Thompson

Joe Wright said:
After a couple of conio.h episodes several years ago, I gave up on CLI
stuff altogether. Now I think to take it up again 'portably' using
curses. Theoretically the same simple menu could run on Unix, Sun and
DOS as well. Although curses is not Standard it is ubiquitous. Is it
portable?

"Is it portable?" is not a yes/no question.

There are multiple systems on which some version of curses is
available. There are undoubtedly systems on which it's not available.
I probably know less than you do about which systems fall into each
category (I wasn't aware of a curses implementation for DOS), and the
question isn't particularly topical anyway.

The real question, I suppose, is whether it's portable enough for your
purposes, i.e., whether curses is supported on all the systems you'd
want your software to run on. I haven't a clue what the answer is.
 
M

Mark B

Robert Gamble said:
Robert said:
Good evening,

I was thinking about making a very simple DOS based (console) menu for
a project that i am working on, but I have no idea where to start for
something like this. Does anyone have any resources they could point me
to or shed some light on what it is that you do exactly. Like I said, I
don't want a real complicated menu, just a title and about three
options. I will add more stuff once I know what it is that I am doing.

Here is a simple example of a menu in Standard C:

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

int main (void) {
int long c = 0;
char buf[80];
puts("Select an option");
puts("1 First option");
puts("2 Second option");
puts("3 Third option");
while (fgets(buf, 100, stdin) != NULL) {

that line should be:

while (fgets(buf, 80, stdin) != NULL) {

No, I'd personally prefer:
while(fgets(buf, sizeof buf, stdin) != NULL) {

I think you've made a pretty good case as to why ;-)
you always notice these things right *after* you hit submit...
I know the feeling...
 
R

Robert Gamble

Mark said:
Robert Gamble said:
Robert said:
(e-mail address removed) wrote:
Good evening,

I was thinking about making a very simple DOS based (console) menu for
a project that i am working on, but I have no idea where to start for
something like this. Does anyone have any resources they could point me
to or shed some light on what it is that you do exactly. Like I said, I
don't want a real complicated menu, just a title and about three
options. I will add more stuff once I know what it is that I am doing.

Here is a simple example of a menu in Standard C:

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

int main (void) {
int long c = 0;
char buf[80];
puts("Select an option");
puts("1 First option");
puts("2 Second option");
puts("3 Third option");
while (fgets(buf, 100, stdin) != NULL) {

that line should be:

while (fgets(buf, 80, stdin) != NULL) {

No, I'd personally prefer:
while(fgets(buf, sizeof buf, stdin) != NULL) {

I think you've made a pretty good case as to why ;-)

In production code I might use a macro or variable for the size of the
buffer. The only reason I wouldn't go with your suggestion is because
it fails if buf ever becomes a pointer to dynamically allocated memory,
but that's just me.

Robert Gamble
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top