0

I've written a small C program where I wanted to display the numeric ASCII value that corresponds to certain key presses.

My code follows. The problem is, after running the program, it accepts input, but doesn't do anything else. It doesn't even reach the first printf statement. I can't figure out what the issue is - is there a problem with mixing the getchar() function with the printf() function in the same program?

#include <stdio.h> 

int main() {

    const int numKeys = 256;

    int keys[numKeys];

    int i;
    for (i = 0; i < numKeys; i++) {

        keys[i] = 0;
    }

    printf("\n Start pressing some keys!\n\n");

    int c;
    while ((c = getchar()) != EOF) {

        printf(" CAPTURED: %d\n", c);

        keys[c]++;
    }

    printf("\n\n ** RESULTS ** \n\n");

    for (i = 0; i < numKeys; i++) {

        if (keys[i] != 0) {

            printf(" Key with value %d was called %d times.", i, keys[i]);
        }
    }
}

I should clarify that I have a Windows XP Pro machine, with Cygwin installed. I use Cygwin for my development space, so I wonder if there is something different when running this type of program in that environment.

dvanaria
  • 6,593
  • 22
  • 62
  • 82
  • I don't think there is a problem with mixing them, as getch effects stdin, while printf effects stdout. I think the problem might be elsewhere? – jacob Mar 20 '13 at 05:38
  • Are you pressing enter? – Xymostech Mar 20 '13 at 05:40
  • it doesnt reach `printf("result")` or `printf(captured)`?.if its the former then its right in doing so because how will it break the `while loop` if you dont get `EOF`. `getchar` waits for `\r\n` or`Enter` to be pressed. use getche. but even then you will have the first issue – Koushik Shetty Mar 20 '13 at 05:46
  • 1
    Your program works fine for me, but then again, I'm running Ubuntu Linux. ctrl+d gives me an EOF, I would imagine Windows has something similar. For interesting reading, look at http://c-faq.com/osdep/cbreak.html to discover ways to read in a character from stdin without having to press enter. – NickO Mar 20 '13 at 06:07

3 Answers3

0

I found the problem. I think you want to use

while ((c = getchar()) != EOF && c != '\n')

Instead if you want to have it print the results after the person hits enter/return.

jacob
  • 4,656
  • 1
  • 23
  • 32
0

problem 1 : getting to printf(" CAPTURED: %d\n", c); without having to press the Enter key solution : is by using getche() in while loop.

problem 2 : getting to 'printf("\n\n ** RESULTS ** \n\n");' or essentially breaking while loop?

solution : you cannot. you will never get EOF as long as you read from keyboard. workaround : close stdin or use a escape character other than EOF.

EDIT : workaround2 : ->use getchar() itself. but to print those entered char u need to press Enter key. now on windows ctrl+z gives EOF but this should be the **FIRST** input on the line after you press Enter key. well this is not a good solution.

if you want a "Press key display times pressed scenario. there is just no simple way(AFAIK)"

Koushik Shetty
  • 2,146
  • 4
  • 20
  • 31
0

I believe that the first printf statement gets executed, but due to buffering is not displayed on the screen immediately. Use fflush(stdout) to send the contents of the buffer to the screen. Ie:

printf("\n Start pressing some keys!\n\n");
fflush(stdout);
bcmpinc
  • 3,202
  • 29
  • 36