1

Consider this block of C++ code.

    if(_kbhit()){
        //printf("Enter\n");
        c = getchar();
        int d = c;
        printf("%d", d);
        //printf("Exit \n");
    }

The output I get is

sd115d100s100

If I press s, d and then d, s. Weird thing is that when I pressed some character, the ascii of previously pressed character was displayed.

Now consider,

    if(_kbhit()){
        printf("Enter\n");
        c = getchar();
        int d = c;
        printf("%d", d);
        printf("Exit \n");
    }

The output I got this time is

aEnter
97
Exit 
dEnter
100
Exit 
dEnter
100
Exit 
aEnter
97
Exit

Now everything is correct the ascii code of correct character is being displayed after I pressed a character.

Why this anomaly ? How can I correct this anomaly ?

Borgleader
  • 15,826
  • 5
  • 46
  • 62
user8277998
  • 147
  • 6
  • Your first shown output doesn't match the code (the code prints numbers, but not characters). Show real, complete and reproducible code. – deviantfan Jul 09 '17 at 04:39
  • @deviantfan Ofcourse it does not print character. The character are automatically printed on the terminal as I enter them. Do I need to show you screenshot my screen ? – user8277998 Jul 09 '17 at 04:43
  • Whoops, it's not getch but getchar ... I must be tired. Sorry. ... Nonetheless, a complete program (with main and everything) would be nice. Maybe you have UB. ... edit: And ... isn't getchar supposed to wait for enter...? – deviantfan Jul 09 '17 at 04:44
  • @deviantfan And that is completely besides the point, The question is why ascii code is different from the character entered ? If you did not read the question properly then please do so before accusing me. – user8277998 Jul 09 '17 at 04:46
  • Ok, if you think UB is besides the point, well ... btw., this wasn't an accusation. Undefined behaviour can very well be the answer. – deviantfan Jul 09 '17 at 04:48
  • @deviantfan The whole program is long. – user8277998 Jul 09 '17 at 04:48
  • 1
    That's why it's helpful to make a small test program with just this code, to look if it behaves similar. If not, the problem is probably somewhere else in the large program. ... (usually because UB) – deviantfan Jul 09 '17 at 04:50
  • @deviantfan Well I already kind of did it because rest of this program is sitting idle and this block is the only one to run. I commented other things out. – user8277998 Jul 09 '17 at 04:52
  • 1
    I tried (your code in a while(1) - loop, nothing else, and it worked... so... to make it clear, some if-blocks where the condition won't be fulfilled at runtime (ie. they are not executed) are plenty to invoke UB. Please try a clean small sample, it doesn't even take a minute. – deviantfan Jul 09 '17 at 05:00
  • @deviantfan Yes I tried still same result, but adding \n after the printf does the work. – user8277998 Jul 09 '17 at 05:11
  • @deviantfan I used std::cout << std::flush and it works now. – user8277998 Jul 09 '17 at 05:15

1 Answers1

2

As mentioned in the comments, it's hard to say for sure without seeing the entire function, but I suspect the additional printf statements are flushing the output buffer for you, since they contain newline characters. Change

printf("%d", d);

to

printf("%d\n", d);

and see if that doesn't have the result you want.

Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31
  • While that's a good idea, something is strange then ... if the output buffer isn't full enough after the first s, but after the next d, it should print both numbers together, not just the first one. – deviantfan Jul 09 '17 at 05:06
  • See this question: [Why does printf not flush after the call unless a newline is in the format string?](https://stackoverflow.com/questions/1716296/why-does-printf-not-flush-after-the-call-unless-a-newline-is-in-the-format-strin). – Mark Benningfield Jul 09 '17 at 05:28
  • @deviantfan Consider this: `getchar()` echoes the input as its typed already, and thanks to `_kbhit()`, it won't wait for the Enter key to be pressed because `stdin` already has a character. Apparently echoing input to the screen is done by writing to stdout rather than writing to the console directly, resulting in flushing of stdout on each character entered, else the character pressed wouldn't get printed until a `\n` was sent on stdout. –  Jul 09 '17 at 07:04