This is one way to do what you want, but note that it uses the non-standard header file, conio.h, to provide unbuffered input.
Other options include printing up the whole menu except the one line that shows them the choice. In case of error, a while loop can just reprint the last line and scanf(), again. So your bad entry would look like this:
Your Choice [1-4]: 5 <enter>
Your Choice [1-4]: 2 <enter>
etc.
So only that one line would be reprinted, for each bad entry, not the whole menu.
You could also just print out 40 newlines, which would blank the screen out, and just reprint the whole menu (seems odd, but it works).
Anyway, here's how you make people who strictly love the C standard, faint in their tracks:
Code:
#include <stdio.h>
#include <conio.h>
int main(void) {
int gar;
int choice = 0;
int menu(void);
choice = menu();
printf("\n Your Choice Was %d ", choice);
printf("\n\n\t Program Complete - Press Enter When Ready \n");
gar = getchar(); gar++;
return;
}
int menu(void) {
int i, gar;
int number = 0;
char a1 = 178;
char a2 = 177;
char a3 = 176;
printf("\n\t\t %c%c%c Welcome to the Main Menu %c%c%c\n\n\n", a1,a2,a3, a3,a2,a1);
printf("\t Please Choose One of the Following Choices: \n\n");
printf("\t 1) Play a Game \n");
printf("\t 2) View The Highest Scores \n");
printf("\t 3) View All Scores \n");
printf("\t 4) Explore Game Options and Characters \n");
printf("\n Your Choice [1-4]: ");
do {
number = getche() - 48;
//scanf("%d", &number);
getch();
if(number < 0) number = 0; //an error occurred
if(number < 1 || number > 4)
printf("%c%c%c", '\b', ' ', '\b');
}while(number > 4 || number < 1);
return number;
}
There are other low-level options including using conio.h to locate your current screen position, and just back it up one row, then print a line of 79 char's, and then return to the start of that line again, and print your "Your Choice [1-4]: line, again.
Another option is working directly with your video memory to poke in the right values - probably a pain in Windows, though.
Another option is to use the Windows API which provides a function for moving the cursor on the monitor.
Using getchar() won't help here. You're dealing with buffered input, and as soon as the <enter> key is hit, the monitor will move to the next line.