1

I've tried googling an answer for my problem, but I cannot seem to find one.

Here's my very simple test code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main(void) {
    char ch;

    printf("Enter character: ");
    ch = getch();
    printf("%c", ch);

    return 0;
}

When I try to run this in Eclipse, I can't even get the first printf line to show up, and performing any keypresses does nothing.

I've also tried doing fflush(stdout) and fflush(stdin), but the program does not as I want it to. If I try this on Visual Studio, it works perfectly.

Does anyone have an idea why? Thanks.

Cilyan
  • 7,883
  • 1
  • 29
  • 37
  • Do you manage to compile using CDT? Which compiler do you use? Also note that conio.h is not part of C standard library, so compiling with anything else than MS' compiler will probably fail. – Cilyan Mar 28 '15 at 03:40
  • I managed to compile using CDT. My compiler is mingw. I initially did not have conio.h, didn't work. Threw in conio.h because online sources say getch() is part of that library, but it's not running like I want to in the eclipse console. Just now, I debugged it using windows console, and it's running like how I want it.. just won't run properly in eclipse console.. – William Huang Mar 28 '15 at 03:51
  • You should use `getchar` instead. I doubt throwing `conio.h` like this will do any good. https://stackoverflow.com/questions/9180001/what-is-the-difference-between-getch-and-getchar – Cilyan Mar 28 '15 at 03:56
  • the function getch() is specific to windows. I.E. not portable. use getchar() instead. the get rid of that non-portable header file: conio.h – user3629249 Mar 28 '15 at 04:33
  • per the C standard, fflush(stdin) is undefined. Therefore, it 'may' work in some implementations but not in others. If you really want to empty stdin, use getchar() in a loop until is returns EOF – user3629249 Mar 28 '15 at 04:41
  • I'm not using getchar() is because getchar() echos my input, and it requires me to hit enter. What I'm basically trying to do is perform some kind of password simulation.. whenever a user inputs a letter (for example, a), it will show * instead of a. I've been searching online on how to turn on/off echo with explanations now.. – William Huang Mar 28 '15 at 18:00

3 Answers3

1
output, for instance to the console/terminal, is buffered.   
it will not actually be output until either: 
1) a newline is output. 
2) fflush(stdout) is called.  
3) a read from stdin is performed 

using getchar() will cause the stdout output buffer
to be flushed to the console/terminal.

the final printf() is not showing for this same reason.
suggest changing the format string from "%c" to "%c\n"
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • OP's question is problem is not on printf line, it is on line getch(), getch has no return when we run this program in eclipse. As far as I know, eclipse has problem use getch in its console – shijq73 Jan 09 '18 at 18:30
1

The Eclipse console window appears not support keyboard input.

For a work-around, you can configure your debug session to launch an external terminal window.

On Eclipse-Oxygen, you can do this from the Debug Configuration dialog. In the Debugger tab, find the checkbox that specifies

Use external console for inferior (open a new console window for input/output)

This problem also occurs when using cin fro C++. See this question: c++ debug mode in eclipse causes program to not wait for cin

Jay Elston
  • 1,978
  • 1
  • 19
  • 38
-1

Try adding these lines in front to open the console:

FILE * a = fopen("CON","w");
freopen("CON","w",stdout);
freopen("CON","r",stdin);
fclose(a);

Good luck!

acenturyandabit
  • 1,188
  • 10
  • 24