conio.c and getch() conflict?

This is a discussion on conio.c and getch() conflict? within the C Programming forums, part of the General Programming Boards category; Hi, I use DevC++ 4. When i don't include conio.c when compiling a program, getch() gets the character at the ...

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    conio.c and getch() conflict?

    Hi,

    I use DevC++ 4. When i don't include conio.c when compiling a program, getch() gets the character at the instant i press a particular key. But if i include conio.c, then getch() only works if i press enter after pressing a particular key. Why's that? How can i fix this.

    THanks

    -dAniEL.

  2. #2
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225
    I don't know about Dev, but under Borland's conio.h, getch() works with no buffering -- a character is accepted everytime you press it. Maybe Dev's getch() conio.c sets everything to line buffering -- everything is accepeted only after the carriage return/line feed character is read (from the Enter key).

    I think there's no conflict in that
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    But i want to get the character code at the instant the key is pressed, not after pressing enter. And this works without conio.c being included, which i can't figure out why.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Is this what you mean?
    Code:
    #include <conio.h>
    #include <stdio.h>
    
    
    int main(void) {
    	int letter = 0;
    	while(letter != -1) {
    		if(kbhit()) {
    			letter = getch();
    			putc(letter, stdout);
      		}
    	}
    }

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600

    Dev C++

    Not sure. In my conio.h, there is this line:

    #ifndef _CONIO_H_
    #define _CONIO_H_
    /*snip*/
    #define getch getchar

    My conio.c has no reference to getch().

    However, there is a second file: conio_mingw.h. Here is a snippet:

    #ifndef _CONIO_H_
    #define _CONIO_H_
    /*snip*/
    char* _cgets (char* szBuffer);
    int _cprintf (const char* szFormat, ...);
    int _cputs (const char* szString);
    int _cscanf (char* szFormat, ...);

    int _getch (); //<-this is unbuffered!
    int _getche ();
    int _kbhit ();
    int _putch (int cPut);
    int _ungetch (int cUnget);


    #ifndef _NO_OLDNAMES

    int getch (); //<-this is buffered!

    int getche ();
    int kbhit ();
    int putch (int cPut);
    int ungetch (int cUnget);

    #endif /* Not _NO_OLDNAMES */


    So I am assuming that when the compiler sees your using conio.c, it uses the first conio.h. Otherwise it uses conio_mingw.


    But I'm just guessing there
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    hey Sebastiani thnx. I got it working now. What i did was open conio.h and delete the line

    Code:
    #define getch getchar
    and open conio.c and delete the lines

    Code:
    int getche() {
      int ch;
      ch = getch ();
      printf ("%c\n", ch);
      return ch;
    }
    Sik.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Pls repair my basketball program
    By death_messiah12 in forum C++ Programming
    Replies: 10
    Last Post: 12-11-2006, 04:15 AM
  3. Clearing input buffer after using getch()
    By milkydoo in forum C++ Programming
    Replies: 3
    Last Post: 07-21-2003, 11:04 PM
  4. Problems with getch()
    By GrNxxDaY in forum C++ Programming
    Replies: 14
    Last Post: 08-12-2002, 02:11 AM
  5. Using getch()
    By pr0ficient in forum Linux Programming
    Replies: 2
    Last Post: 07-26-2002, 05:59 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21