I am making a command line program that finds factors of a number that the user enters. At the moment the user could enter something like this: '768r7fg38gf8320rf' which returns an error. I would like to restrict the user so they can only enter numbers (0 - 9). How would I do this?

Without knowing precise details of your environment, it's very hard to say how you would do that.

But it's far simpler just to let the user enter what they like, then validate it.

I am using a Microsoft OS and it only needs to work on Microsoft. I don't need to disable the keyboard globably, just for my application. So when the user presses a key there can be something in my app that checks if it was a number key. Except, I don't know how to do this.

Just do something like this :

string num = "";
cin >> num;
while( !isNumber( num ) ){
   cout << "Only numbers allowed, Try again : " ;
   cin >> num;
}

isNumber is your own function that uses the isdigits() function from cctype.

Edited 6 Years Ago by firstPerson: n/a

To avoid using non-stnadard functions in conio.h, you can use win32 api console functions. Here is how to filter the keys you don't want. This is not the entire program you need, but will get you going so that you can do your own thing with it.

#include <windows.h>
#include <iostream>
#include <vector>

int getkey()
{
    bool rvalue = false;
    const int MaxEvents = 16;
    HANDLE hStdin =  GetStdHandle(STD_INPUT_HANDLE);
    DWORD dwNumRead = 0;
    DWORD dwNumEvents = 0;
    INPUT_RECORD buf[MaxEvents];
    if( GetNumberOfConsoleInputEvents(hStdin, &dwNumEvents) )
    {
            if( dwNumEvents > 0)
            {
                memset(buf, 0, sizeof(buf));
                // Get the number of events available at the console
                if( ReadConsoleInput(hStdin,buf,MaxEvents, &dwNumRead) )
                {
                    // process each event
                    for(DWORD i = 0; i < dwNumRead; i++)
                    {
                        // if keyboard event
                        if( buf[i].EventType == KEY_EVENT)
                        {
                            KEY_EVENT_RECORD* pKey = (KEY_EVENT_RECORD*)&buf[i].Event.KeyEvent;
                            // if key down event
                            if( pKey->bKeyDown == TRUE)
                            {
                                // test the key type.
                                if( isdigit(pKey->uChar.AsciiChar) )
                                {
                                    return pKey->uChar.AsciiChar;
                                }
                                else if( pKey->wVirtualKeyCode == VK_RETURN)
                                    return VK_RETURN;
                            }
                        }
                    }
                }
            }
    }
    return 0;
}

int main()
{
    int key = 0;
    while( key != VK_RETURN)
    {
        while( (key = getkey()) == 0)
            Sleep(100);
        if( key != VK_RETURN)
            std::cout << (char)key << ' ';
    }
    std::cout << '\n';
}

Edited 6 Years Ago by Ancient Dragon: n/a

This article has been dead for over six months. Start a new discussion instead.