Last Edited By Krjb Donovan
Last Updated: Mar 11, 2014 07:52 PM GMT


QUESTION: Hi. I'm using Visual Studio 2010 and I have the following C++ code segment:

  1. include<StdAfx.h>
  2. include<conio.h>
  3. include<iostream>
  4. include<string.h>

using namespace std; int ora, min, sec, r; char a; void main(){ int d1=10; while(d1>0){ wcout<<" "<<d1<<" \n\t"; d1=d1-1;} wcout<<" Apasa RA4 \n\t"; a=_getch(); r=strnicmp(a,"a",1); if(r==0){ ....

I get following error message: "test.cpp(16): error C2664: 'strnicmp' : cannot convert parameter 1 from 'char' to 'const char *'

         Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast"

Any idea how I could solve this problem? Or if you know a better way to achieve the C# "OnKeyPress" function, please tell me. Thank you in advance. Bye.

ANSWER: Hello Rudolph.

Your first parameter to strnicmp is the variable a which is a character. The function needs a pointer to a character. You could pass in the address of the variable like this.

strnicmp(&a, "a", 1);

If you are comparing just one character, and want to ignore case, you could do this too:

r = (tolower(a) == 'a')

The lower case value of the variable a is compared against the character 'a', and r gets 1 if the two values are equal, and 0 otherwise. If you choose that method, your statement if (r == 0) becomes if (r == 1)

The _getch function will pause the program until the user enters a keystroke. Is that what you want ?

---------- FOLLOW-UP ----------

QUESTION: Thank you for your response. As for your question: In that part of the program, yes, I want it to pause execution. But in another part/subprogram, I want it to be a timer (it shows the time starting from 0:0:0, which is updated every second, until the user presses a key (b), at which time the program will jump to a subprogram to set the time). I already have the code for the timer and set_timer functions, but I haven't figured out how to combine the _getch() and timer (in other words, not make the program wait until the user presses the key). If you have any idea how to do this, please tell me. Again, thank you. Bye.


Hello Rudolph.

I get this question quite often. The simplest way to check for keyboard input without blocking is to use the kbhit functions.

Here is a sample with a timer expiry set to 60 seconds that reads a line of text.

  1. include <time.h>
  2. include <string>
  3. include <iostream>
  4. include <conio.h>
  5. include <Windows.h>

void kbhitExample() {

       // expire in 60 seconds.
       time_t expiry = time(NULL) + 60;
       string input;
           if (time(NULL) > expiry)
               cout << "Expired\n";
           if (_kbhit())
               char ch = (char)_getch();
               _putch(ch); // echo character back to the screen
               if (ch == '\n' || ch == '\r') break;
               else input.insert(input.end(), 1, ch);
       cout << "String is <" << input << ">\n";



©2021 eLuminary LLC. All rights reserved.