By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,734 Members | 832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,734 IT Pros & Developers. It's quick & easy.

Menu looping

P: n/a
sk
Hi everyone, I'm kind of new to C programming, so I thought that a
newsgroup would be the best place to ask a question about this.

I'm trying to write a small program that displays a menu and has the
user input a value(int). I'm only using getchar and printf to make
things simple.

I have the menu printing part down pretty well, but I'm getting an error
where the entire menu will print n+1 number of times when you input a
value with n-digits. I asked around a little, and some others said that
it was because getchar is buffered, and will just print for every char
that is passed. I'm stuck on how to make it so that the menu will only
print once, then ask the use for input.

This is how it is set up right now:

prints menu
getchar
while (getchar variable is not EOF)
{
prints menu
(conditionals for menu...)
getchar
}

Can anyone give me any ideas on how to fix this? Sorry if this is a
stupid question. Thanks in advance!
Feb 28 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
"sk" <sk******@purdue.edu> wrote in message
news:7a5Nf.794387$x96.655350@attbi_s72...
Hi everyone, I'm kind of new to C programming, so I thought that a
newsgroup would be the best place to ask a question about this.

I'm trying to write a small program that displays a menu and has the
user input a value(int). I'm only using getchar and printf to make
things simple.

I have the menu printing part down pretty well, but I'm getting an error
where the entire menu will print n+1 number of times when you input a
value with n-digits. I asked around a little, and some others said that
it was because getchar is buffered, and will just print for every char
that is passed. I'm stuck on how to make it so that the menu will only
print once, then ask the use for input.

This is how it is set up right now:

prints menu
getchar
while (getchar variable is not EOF)
{
prints menu
(conditionals for menu...)
getchar
}

Can anyone give me any ideas on how to fix this? Sorry if this is a
stupid question. Thanks in advance!


You may consider using a do-while loop since it may suit your needs better.
As far as the implementation is concerned, you can try reading the first
character of the user input and discard the rest of the input line. The end
of a line is signaled with getchar() returning '\n'. In any case you should
always look out for EOF as you already have. Do not forget to store the
getchar() return in an int. Feel free to post your code here in your
attempts towards the desired behaviour.
Mar 1 '06 #2

P: n/a
sk
stathis gotsis wrote:
"sk" <sk******@purdue.edu> wrote in message
news:7a5Nf.794387$x96.655350@attbi_s72...
Hi everyone, I'm kind of new to C programming, so I thought that a
newsgroup would be the best place to ask a question about this.

I'm trying to write a small program that displays a menu and has the
user input a value(int). I'm only using getchar and printf to make
things simple.

I have the menu printing part down pretty well, but I'm getting an error
where the entire menu will print n+1 number of times when you input a
value with n-digits. I asked around a little, and some others said that
it was because getchar is buffered, and will just print for every char
that is passed. I'm stuck on how to make it so that the menu will only
print once, then ask the use for input.

This is how it is set up right now:

prints menu
getchar
while (getchar variable is not EOF)
{
prints menu
(conditionals for menu...)
getchar
}

Can anyone give me any ideas on how to fix this? Sorry if this is a
stupid question. Thanks in advance!


You may consider using a do-while loop since it may suit your needs better.
As far as the implementation is concerned, you can try reading the first
character of the user input and discard the rest of the input line. The end
of a line is signaled with getchar() returning '\n'. In any case you should
always look out for EOF as you already have. Do not forget to store the
getchar() return in an int. Feel free to post your code here in your
attempts towards the desired behaviour.


Thank you for your quick reply!

I tried to use the do while, but it's still printing n+1 times...
I'm trying to make something like a stripped down calculator where it
will print a menu, ask for input, print out data, then loops over again.
It will stop when the user pushes ctrl-d or inputs a '0';

Just another quick question. What is the difference between storing the
getchar() return to an int instead of a char?
Mar 1 '06 #3

P: n/a
sk wrote:

You may consider using a do-while loop since it may suit your needs
better.
As far as the implementation is concerned, you can try reading the first
character of the user input and discard the rest of the input line.
The end
of a line is signaled with getchar() returning '\n'. In any case you
should
always look out for EOF as you already have. Do not forget to store the
getchar() return in an int. Feel free to post your code here in your
attempts towards the desired behaviour.


Thank you for your quick reply!

I tried to use the do while, but it's still printing n+1 times...
I'm trying to make something like a stripped down calculator where it
will print a menu, ask for input, print out data, then loops over again.
It will stop when the user pushes ctrl-d or inputs a '0';

Just another quick question. What is the difference between storing the
getchar() return to an int instead of a char?


Did you remove the first prints menu before the loop?

--
Ian Collins.
Mar 1 '06 #4

P: n/a
sk
Ian Collins wrote:
sk wrote:

You may consider using a do-while loop since it may suit your needs
better.
As far as the implementation is concerned, you can try reading the first
character of the user input and discard the rest of the input line.
The end
of a line is signaled with getchar() returning '\n'. In any case you
should
always look out for EOF as you already have. Do not forget to store the
getchar() return in an int. Feel free to post your code here in your
attempts towards the desired behaviour.


Thank you for your quick reply!

I tried to use the do while, but it's still printing n+1 times...
I'm trying to make something like a stripped down calculator where it
will print a menu, ask for input, print out data, then loops over
again. It will stop when the user pushes ctrl-d or inputs a '0';

Just another quick question. What is the difference between storing
the getchar() return to an int instead of a char?


Did you remove the first prints menu before the loop?


Yes, I have. I've also tried many combinations of while loops/print
statements, and it still prints too many times. :(
Mar 1 '06 #5

P: n/a
sk wrote:
Hi everyone, I'm kind of new to C programming, so I thought that a
newsgroup would be the best place to ask a question about this.

I'm trying to write a small program that displays a menu and has the
user input a value(int). I'm only using getchar and printf to make
things simple.

I have the menu printing part down pretty well, but I'm getting an error
where the entire menu will print n+1 number of times when you input a
value with n-digits. I asked around a little, and some others said that
it was because getchar is buffered, and will just print for every char
that is passed. I'm stuck on how to make it so that the menu will only
print once, then ask the use for input.

This is how it is set up right now:

prints menu
getchar
while (getchar variable is not EOF) getchar takes char by char buffering chars ...
and you want a multiple char entry...
Don't you want to use fgets, sscanf ? then test on strlen (buf) == 0 to exit {
prints menu
(conditionals for menu...)
getchar
}

Can anyone give me any ideas on how to fix this? Sorry if this is a
stupid question. Thanks in advance!

Mar 1 '06 #6

P: n/a

"sk" <sk******@purdue.edu> schrieb im Newsbeitrag
news:C47Nf.794542$x96.597967@attbi_s72...

read this part again...
As far as the implementation is concerned, you can try reading the
first
character of the user input and discard the rest of the input line. The
end
of a line is signaled with getchar() returning '\n'. In any case you
should
always look out for EOF as you already have. Do not forget to store the
getchar() return in an int. Feel free to post your code here in your
attempts towards the desired behaviour.
getchar() will buffer your input until you press enter.
then the next calls will return each character, including a '\n' for enter.

some loop {
menu();

c = getchar();

/* do something */

/* forget the rest of the line */
while (c != '\n' && c != EOF)
c = getchar();
}

Just another quick question. What is the difference between storing the
getchar() return to an int instead of a char?

EOF (end of file/no more input) is no regular character and if you put the
result in a char... then there will be a character and a eof that you cant
distinguish.

Did you remove the first prints menu before the loop?


Yes, I have. I've also tried many combinations of while loops/print
statements, and it still prints too many times. :(


or look what xavier wrote...

f~
Mar 1 '06 #7

P: n/a
sk wrote:
What is the difference between storing the
getchar() return to an int instead of a char?


EOF is not a char.

if ((char)getchar() == EOF) /* is *never* true */

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
Mar 1 '06 #8

P: n/a
Pedro Graca <he****@dodgeit.com> writes:
sk wrote:
What is the difference between storing the
getchar() return to an int instead of a char?


EOF is not a char.

if ((char)getchar() == EOF) /* is *never* true */


Not quite. EOF is guaranteed to have a negative value of type int
(it's typically -1). If plain char happens to be signed, the
condition can be true *either* if getchar() returns EOF (and EOF is
within the range of signed char) *or* if getchar() returns a valid
character whose value happens matches the value of EOF. On many
systems, the latter will occur if the input character is character
255, which is a y with an umlaut ('') in Latin-1 (a common extension
of ASCII).

getchar() returns either the value EOF, or the next character from
stdin converted to unsigned char and then converted to int. Storing
the value in an int rather than a char lets you distinguish between
character 255 and -1.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 1 '06 #9

P: n/a
sk wrote:
.... snip ...
I have the menu printing part down pretty well, but I'm getting an
error where the entire menu will print n+1 number of times when
you input a value with n-digits. I asked around a little, and some
others said that it was because getchar is buffered, and will just
print for every char that is passed. I'm stuck on how to make it
so that the menu will only print once, then ask the use for input.

This is how it is set up right now:
.... snip non-compilable code ...
Can anyone give me any ideas on how to fix this? Sorry if this is
a stupid question. Thanks in advance!


Show us compilable code, and tell us what problems you have with
it. At any rate, the following is an expansion of something I
explained in another thread a few days ago. It compiles, but has
not been tested.

#include <stdio.h>
#include <ctype.h>
#include <string.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.
Returns response initial char or EOF.
*/
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 (untested) */

You could call this with something like:

ch = menuget("Enter your desire\n"
" A or a for all\n"
" b for botheration\n"
" x for quit : ",
"Aabx";

Notice the merged strings for the prompt.

--
"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 1 '06 #10

P: n/a
sk wrote:

I have the menu printing part down pretty well, but I'm getting an error
where the entire menu will print n+1 number of times when you input a
value with n-digits.

prints menu
getchar
while (getchar variable is not EOF)
{
prints menu
(conditionals for menu...)
getchar
}


getchar() gets a char. If you enter 10 chars then it your
loop will obviously execute 10 times, once for each char.

It sounds like what you want to do is discard all input up
until the '\n' (enter key), each time you have successfully
read a char. (Another option would be to use a platform-
specific function that returns immediately upon keypress).

Also, if you make the getchar() part of the loop condition
then you don't have to duplicate it.

For example:

int ch;
while ( EOF != (ch = getchar()) )
{
/* consume extra input */
if ( ch != '\n' )
while ( getchar() != '\n' );

do_stuff();
}

Mar 2 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.