cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : getch problem :
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programm...
Articles
Lounge
Jobs

-

post  getch problem

QWERTYman (255)
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?
|
Duoas (1595)
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.
|
QWERTYman (255)
Well, any known ways to do unbuffered input with a different header file?
|
mahlerfive (117)
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.
|
Duoas (1595)
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.
|
QWERTYman (255)
Oh, mahlerfive.
Stupid me.
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2009 - All rights reserved - v2.2
Spotted an error? contact us