Simple program using conio.h not working

Main program:

char key = '0';
while(key != 'x')
{
cout << key;
key = _getchar();
}
Whenever I run this code, key comes up as some question mark inside of a square. Why?
I think you mean _getch() or getchar() (without underscore). Also, I don't think it's a good idea to use conio.h, it is deprecated. Your code works fine for me ( with _getch() ), but if it still shows squares as you said, this should be because you output unicode to a ASCII stream, so try this instead:

1
2
3
4
5
6
7
8

wchar_t key = '0';
while(key != 'x')
{
    std::wcout << key;
    key = _getch();
}


If you want to use keyboard commands, better use WINAPI (i.e Windows.h ).

Last edited on
Topic archived. No new replies allowed.