6

I'm trying to create a function that will printf a certain string if the user presses any button on the keyboard EXCEPT for capital P, if the user presses P then it will break the loop.

However I don't think I'm using _kbhit and _getch properly. I use the number 80 because that is the ASCII symbol for 80....sorry for any confusion

void activateAlarm(int channelID) {

    int key = 0;

    while(temperatureChannel[channelID].currentTemperature > temperatureChannel[channelID].highLimit
        ||temperatureChannel[channelID].currentTemperature < temperatureChannel[channelID].lowLimit) {

        beep(350,100);

        if (_kbhit()) {
            key = _getch();
            if(key == 'P');
                break;
        }    
    }
}
Casper Beyer
  • 2,203
  • 2
  • 22
  • 35
Pardon_me
  • 715
  • 2
  • 8
  • 20

1 Answers1

14

No need to explain, the code talks better :

#include <conio.h>

// ...

printf("please press P key to pause \n ");

int key = 0;

while(1)
{
    if (_kbhit())
    {
      key =_getch();

      if (key == 'P')
        break;
    }    
}
Nic
  • 6,211
  • 10
  • 46
  • 69
masoud
  • 55,379
  • 16
  • 141
  • 208
  • 3
    The #4 edit should _not_ have been approved as it introduced several bugs to the code: (1) It was correct to declare `key` as an `int`, since that's the actual return type of `_getch()`. (2) The leading underscores on `_kbhit()` and `_getch()` should not have been deleted; the Visual C++ libraries don't define most POSIX-style additions using the POSIX names because, technically, those violate the ISO C++ standard. – Adrian McCarthy Feb 10 '17 at 17:45
  • @AdrianMcCarthy `kbhit` and `getch` are not functions defined by POSIX. Defining these functions without underscores also doesn't violate the C/C++/POSIX standards. The actual reason we have to include the underscore is because the functions without underscore once existed as a left-over from a failed attempt by Microsoft to conform to the POSIX standard. Their attempt was miserable, has been deprecated, and as of Windows 8, has been completely removed. The underscore exists to prevent old executables using the broken POSIX functions from compiling. – yyny May 02 '19 at 14:11
  • Microsoft originally only created a minimal POSIX compatability layer for contractual reasons. Nowadays, there are much better alternatives that should be used instead. The new functions with underscores, although not conforming to POSIX, probably are closer to what you would expect them to do than the original broken POSIX functions. If you're using MinGW, you've already been using them, even if you didn't include the underscores. – yyny May 02 '19 at 14:13
  • @YoYoYonnY: `getch` is indeed defined by Posix: http://pubs.opengroup.org/onlinepubs/007908799/xcurses/getch.html – Adrian McCarthy May 02 '19 at 17:26
  • @YoYoYonnY: You're right that `kbhit` is not from Posix--it came from DOS. Microsoft has long held that adding functions to the standard C++ library is a violation of the C++ standard [probably a strict reading of S17.1 P2 [library.general]]. Their workaround is to rename those non-standard functions with a leading underscore because those are "reserved to the implementation for use as a name in the global namespace." [S17.6.4.3.2 [global.names]] – Adrian McCarthy May 02 '19 at 17:39