I've prepared this little routine for you...
be aware that instead of is used in combination with .
This enables a little more robust program behavior as user input is limited to the first character on a line and no dangling input remaining, which may cause undefined behavior. No guarantees though.
Code:
void Getchar_Test( void )
{
const char s[3][3][16] = {
{ { "Road" }, { "Road" }, {"Forest"} } ,
{ { "Shed" }, { "House" }, {"Road"} } ,
{ { "Road" }, { "Jeep" }, {"Corpse!"} } ,
},
s_blocked[] = "Your way is blocked...",
c_exit = 'q';
const int ox = 1, oy = 1;
int x = 0, y = 0,
c, moves;
printf("Possible commands...\n\t\t(n)orth, (w)est, (s)outh, (e)ast, (i)nventory, (q)uit.\n\n");
printf( "(n, w, s, e, i or %c)? ", c_exit );
c = getchar();
c = toupper( c );
for ( moves = 0; toupper(c_exit) != c; moves++ )
{
switch( c )
{
case 'N':
if (-1 < y) { y--; } else { printf("\t%s\n", s_blocked); }
break;
case 'W':
if (-1 < x) { x--; } else { printf("\t%s\n", s_blocked); }
break;
case 'S':
if (1 > y) { y++; } else { printf("\t%s\n", s_blocked); }
break;
case 'E':
if (1 > x) { x++; } else { printf("\t%s\n", s_blocked); }
break;
case 'I':
printf( "\tYou carry... %s.\n", "nothing" );
break;
default:
printf("Unknown command...\n");
}
printf("\n[Move %d]\n\tYou are at... %s\n\n", moves + 1, s[oy+y][ox+x] );
printf( "(n, w, s, e, i or %c)? ", c_exit );
fflush( stdin ); /* remove newline ...and other possible characters not needed */
c = getchar();
c = toupper( c );
}
printf("\n\n\nAfter %4i move(s)... your adventure is over.\n", moves );
}