Andrew Robert <> scribbled the following:
> Hi Everyone,
> I am trying to develop a simple menu using switch/case statements and I
> want to be able to have the user "Press any key to continue".
> It seems that the main problem is with the initial scanf choice part of
> the menu (see code below).
> I first looked at scanf and gets but they delete the terminal \n and
> that would mess up pressing the carraige return key.
> The function fgets seems to do what I need but man pages and on-line
> references to fgets seem to indicate it should be used strictily for files
Those man pages and on-line references are wrong. fgets, despite its
name, works on streams, not files. You can pass any input stream to it.
Try passing stdin.
> The idea of a "Press any key to continue" should be pretty common but
> the logic seems to be elusive.
> Does anyone have any ideas about this?
> Any help you can provide would be greatly appreciated.
> Thanks
> /*
(snip)
> */
> #include <stdio.h>
> #include <stdlib.h>
> int main()
> {
> int choice;
> char string[1];
> system("clear");
This command "clear" is non-portable. (Pretty much any command passed
to system() is.) Why do you want to clear the screen in the first
place?
> while (1)
> {
> system("clear");
> printf("\n\t\tSTUDENT REGISTRATION/COURSE GRADE\n");
> printf("\t\t DATABASE MANAGEMENT SYSTEM\n\n");
> printf("\t\t1. Register a new student\n");
> printf("\t\t2. Delete a student\n");
> printf("\t\t3. Enter grades for a student\n");
> printf("\t\t4. Display student grade report\n");
> printf("\t\t5. Display class grade averages\n");
> printf("\t\t6. Exit\n\n\n");
> printf("\t\t\tChoice: ");
> scanf("%d",&choice);
> fflush(stdin);
Undefined behaviour. This might do anything from segfaulting your
program to calling up George W Bush and telling him you're hiding
those elusive WMDs.
> switch (choice)
> {
> case 1: system("clear");
> printf ("\n\n\t\tOption Selected 1.\n");
> printf ("\n\n\t\tPress any key to continue");
> scanf("%s",string);
It's a bad idea to scanf() into a 1-character array this way. If
the user types more than one character, you overflow the buffer,
causing undefined behaviour. Try the "%c" specifier for scanf()
or switch to fgets(). fgets() does work on stdin.
> break;
(snip)
> case 6: printf ("Program Over.\n");
> exit (0);
> default: system("clear");
> printf ("\n\n\t\t Invalid choice.\n");
> printf ("\n\n\t\t Press any key to continue");
> scanf("%s",string);
> system("clear");
> break;
> } /* end switch */
> } /* end while */
> return 0;
> }
It's true that when you type for example '1' in response to your
prompt, you have to press Return, and such your program receives
"1\n". You need some "draining" logic to get rid of the '\n'. This
is very simple, the comp.lang.c FAQ contains some sample code.
Generally, once you get your character, you want to discard any
subsequent characters up to and including the first '\n'. Then process
the rest as normal.
--
/-- Joona Palaste () ------------- Finland --------\
\--
http://www.helsinki.fi/~palaste --------------------- rules! --------/
"To doo bee doo bee doo."
- Frank Sinatra