getch problem

Aug 18, 2008 at 10:38pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <conio.h>
using namespace std;

bool redo();

int main()
{
    top:
    cout << "Hello, world!\n";
    redo();
    if(redo())
    goto top;
}

bool redo()
{
    cout << "Do again? [y/n] (n)\n";
    char ch;
    if( !_kbhit() ){
    ch = getch();
    if(ch == 121)
    return true;
    else
    return false;}
}


Interestingly, here is the output:
Hello, world!
Do again? [y/n] (n)
(user types n)
Do again? [y/n] (n)
*user types n*
*exit*


This is not expected. Any ideas?
Aug 18, 2008 at 11:11pm
It is because <conio.h> is ancient, and it is clashing with the DOS terminal emulator's handling of such requests -- The windows console does that when switching back and forth between line-buffered and unbuffered input.

Alas.
Aug 19, 2008 at 12:44am
Well, any known ways to do unbuffered input with a different header file?
Aug 19, 2008 at 1:12am
I don't see the problem... the output you got is what is expected..
You call the function twice, both times the user entered "n", so both times it returned false.
Aug 19, 2008 at 1:12am
Use Curses.

PDCurses (DOS, Win32, etc)
http://pdcurses.sourceforge.net/

NCurses (POSIX)
http://www.gnu.org/software/ncurses/

The MinGW suite has a precompiled pdcurses package you can simply download and unpack.

You should remember this too
http://www.cplusplus.com/forum/beginner/1988/page2.html#msg10636
Remember, mixing line-buffered and unbuffered input in a single program confuses users.

Hope this helps.
Aug 20, 2008 at 12:18am
Oh, mahlerfive.
Stupid me.
Topic archived. No new replies allowed.