If you want to use curses, there is a library called pdcurses that works with Windows.
Otherwise, the native Windows console API functions as linked to above will do these sort of things, probably requiring a bit of wrapping them into more suitable functions for your application, e.g. you may want a function that reads a string of certain length from a specific location on the screen:
Code:
// Return 0 on success, otherwise non-zero.
int readStringXY(int x, int y, char *str, size_t maxlen, const char *allowedChars);
The last argument, allowedchars, is used to tell the function for example to only accept digits for reading an integer (and perhaps '-', '+', 'e', '.' for a float) - you can then wrap up a function like this:
Code:
// Returns 1 on invalid number, 0 on valid.
int checkValidNumber(const char *buf, int *value, int min, int max)
{
char *ptr;
*value = strtol(buf, 0, &ptr);
if (*ptr) return 1; // Last char parsed is not NUL - we didn't accept the whole string.
if (*value > max || *value < min) return 1;
return 0;
}
// Return 0 on success.
int readIntXY(int x, int y, int *value, size_t ndigits, int min, int max)
{
char buf[12];
if (ndigits == 0 || ndigits > 11) ndigits = 11;
sprintf(buf, "%d", *value);
do {
readStringXY(x, y, buf, ndigits, "0123456789");
} while(checkValidNumber(buf, value, min, max));
return 0;
}
You will probably also find yourself adding a function pointer to check the validity for special cases (social security number or such, post/zip-code, telephone numbers, etc, etc). Something like this:
Code:
typedef int (*CheckFunc)(const char *buf);
int readStringXYCheck(int x, int y, char *buf, size_t maxlen, const char *allowedChars, CheckFunc checkfunc)
{
do
{
readStringXY(x, y, buf, maxlen, allowedchars);
} while(checkfunc(buf));
return 0;
}
--
Mats