A
Andrew Robert
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
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
/*
program: stu_menu.c
Function: Display student registration menu to screen, retrieve/validate
choice and pass control to sub-program. Code includes standard
elements to comply as c++ code.
Modification History
Date Programmer Modification
11/06/03 AAR Initial creation
Added statement to initialize choice variable
Added scanf/flushf statement to get user choice
Added switch case handles to determine user
choice
Added mode to clear screen and warn of bad
choice
11/06/03 AAR Test compiled on Fedora Linux CORE 9.1 using
gcc 2.96 compiler
11/07/03 AAR Ported to Alpha OpenVMS v7.3-1 and compiled
using Compaq C v6.5-001. Code successfully
tested
11/07/03 AAR Ported to Solaris v5.8 and compiled
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int choice;
char string[1];
system("clear");
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);
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);
break;
case 2: system("clear");
printf ("\n\n\t\tOption Selected 2.\n");
printf ("\n\n\t\tPress any key to continue");
scanf("%s",string);
break;
case 3: system("clear");
printf ("\n\n\t\tOption Selected 3.\n");
printf ("\n\n\t\tPress any key to continue");
scanf("%s",string);
break;
case 4: system("clear");
printf ("\n\n\t\tOption Selected 4.\n");
printf ("\n\n\t\tPress any key to continue");
scanf("%s",string);
break;
case 5: system("clear");
printf ("\n\n\t\tOption Selected 5.\n");
printf ("\n\n\t\tPress any key to continue");
scanf("%s",string);
break;
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;
}
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
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
/*
program: stu_menu.c
Function: Display student registration menu to screen, retrieve/validate
choice and pass control to sub-program. Code includes standard
elements to comply as c++ code.
Modification History
Date Programmer Modification
11/06/03 AAR Initial creation
Added statement to initialize choice variable
Added scanf/flushf statement to get user choice
Added switch case handles to determine user
choice
Added mode to clear screen and warn of bad
choice
11/06/03 AAR Test compiled on Fedora Linux CORE 9.1 using
gcc 2.96 compiler
11/07/03 AAR Ported to Alpha OpenVMS v7.3-1 and compiled
using Compaq C v6.5-001. Code successfully
tested
11/07/03 AAR Ported to Solaris v5.8 and compiled
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int choice;
char string[1];
system("clear");
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);
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);
break;
case 2: system("clear");
printf ("\n\n\t\tOption Selected 2.\n");
printf ("\n\n\t\tPress any key to continue");
scanf("%s",string);
break;
case 3: system("clear");
printf ("\n\n\t\tOption Selected 3.\n");
printf ("\n\n\t\tPress any key to continue");
scanf("%s",string);
break;
case 4: system("clear");
printf ("\n\n\t\tOption Selected 4.\n");
printf ("\n\n\t\tPress any key to continue");
scanf("%s",string);
break;
case 5: system("clear");
printf ("\n\n\t\tOption Selected 5.\n");
printf ("\n\n\t\tPress any key to continue");
scanf("%s",string);
break;
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;
}