I will probably kill you for using goto.
FWIW, I've been tinkering with this. I haven't done this in a while and it is interesting to me.
Code:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#define ARRAYSIZE(me) (sizeof(me) / sizeof(me[0]))
/** SELECT_END is the last highlighted choice in selections. */
#define SELECT_END 4
void clear (void);
void menu (void);
void blit (unsigned int select);
int main()
{
menu();
return 0;
}
void menu()
{
int select = 0;
int x;
blit(select);
while (x = getch())
{
if (x == 'w')
{
select -= 2;
if (select < 0)
select = 0;
blit (select);
}
else if (x == 's')
{
select += 2;
if (select > SELECT_END)
select = SELECT_END;
blit (select);
}
}
}
void clear()
{
system("cls"); /* better code here */
}
void blit (unsigned int select)
{
const char *selections[] =
{
" >> NEW GAME <<",
" NEW GAME ",
" >> HELP <<",
" HELP ",
" >> QUIT <<",
" QUIT ",
};
unsigned int i;
clear();
for (i = 0; i < ARRAYSIZE(selections); i += 2)
{
if (i == select)
printf ("%s\n", selections[i]);
else
printf ("%s\n", selections[i + 1]);
}
printf ("Enter choices. Close window to quit.\n");
}
Compile that and play with it. To know what selection was made, you can modify this to return a specific value to more important code. Each selection has a particular spot in the array, so it should not be hard to know which one. Instead of enter, use it with the space key first because that has a standard value. Save special keys for a later version.
Using special keys like arrows and enter are more difficult because of the unbuffered input. The computer does not have a chance to turn enter into '\n' for you. You could make it easy by rolling your own function to do input. First, you need to figure out what the value for the first byte is that indicates a special key. You read that, and test for it. If it is a special key, then you have to grab the distinguishing part with another call, and return the distinguishing byte. No two control characters will be represented by the same sequence, so if you use the distinguishing byte the program should do the right thing. If you aren't detecting a special key, then just return what you read first.
Code:
int getkey() {
int bytes[2];
if ((bytes[0] = getch()) == 0x1a) { /* !! my guess for what the special character is; trust but verify */
bytes[1] = getch();
return bytes[1];
} else {
return bytes[0];
}
}
Something like that. You'll have to find out what the special character is... maybe, put this aside for now and make another little test program. Try inputting some random stuff and printing out the corresponding numbers to discover it.
Finally there is something in jim's earlier advice, conio is really old and was probably used when computers were not so widespread. You'd be lucky if all these magic values are the same accross windows versions even. It shouldn't take long for conio.h to show it's age. But in my opinion unbuffered input requires the same skills, no matter the API, so this is good practice.