Thread: Keyboard input: Array of characters

  1. #1
    Registered User
    Join Date
    Jan 2003

    Question Keyboard input: Array of characters

    I want to get some input from the user, put it in an array, and then display what they are typing on the screen. I'm using TextOut to do part of this, but am unable to figure out a way to update the output as the user fills the array.

    When the user hits the enter key, I want to display what was entered, add one to it, and display the new number.

    I would be grateful for any help anyone can give me.

    //message handler (window procedure)
    long CALLBACK WindowProc(HWND hwnd,UINT message,
                             WPARAM wParam,LPARAM lParam)
    	HDC hdc;
    	hdc = GetDC(hwnd);
    	char * entstr = "Enter an Integer";
    	TextOut(hdc, 280, 150, entstr, strlen(entstr));
    	char * plus = "+ 1 =";
    	char USERstr[11];
    	USERstr[0] = 0;
    	char strptr[11];
    	strptr[0] = 0;
    	int count = 280;
        case WM_ACTIVATEAPP: ActiveApp=wParam; break;
        case WM_CREATE: break; 
        case WM_KEYDOWN: //keyboard hit
         if(wParam >= 48 && wParam <= 57 && wParam != VK_RETURN) 
    	USERstr[0] = wParam;
    	TextOut(hdc, count, 250 ,USERstr, 1);
    	strcat(strptr, USERstr);
    	count = count + 10;
         if(wParam == VK_RETURN)
    	TextOut(hdc, 350, 400, strptr, strlen(strptr));
    	TextOut(hdc, 390, 400, plus, strlen(plus));
          if(wParam == VK_ESCAPE)DestroyWindow(hwnd);break;
        case WM_DESTROY: //end of game
          if(lpDirectDrawObject!=NULL){ //if DD object exists
            if(lpPrimary!=NULL) //if primary surface exists
              lpPrimary->Release(); //release primary surface
            lpDirectDrawObject->Release(); //release DD object
          ShowCursor(TRUE); //show the mouse cursor
          PostQuitMessage(0); //and exit
        default: //default window procedure
          return DefWindowProc(hwnd,message,wParam,lParam);
      } //switch(message)
      return 0L;
    } //WindowProc

  2. #2
    Registered User
    Join Date
    Mar 2002
    Is there a reason you are trying to work with Windows API instead of console code? IMO it makes it much more difficult to learn C++ when trying to use the API.

    Reading around the API doodah's you appear to be trying to use a char array as a string but you haven't NULL terminated it.

    In this line:

    USERstr[0] = wParam;

    assuming the value in wParam can be interpretted as an ASCII char then you are assigning a single char to the first element of USERstr by doing this. But, since there is no terminating NULL char at then end of USERstr at this point, it is not a string, but just a char array with a char at element 0. Therefore, if TextOut() needs a string as the third parameter (which I suspect it does because I haven't seen an API function that takes just single char values yet, though my experience with Window API is far from extensive):

    TextOut(hdc, count, 250 ,USERstr, 1);

    then this call will fail. And I know this will fail, too:

    strcat(strptr, USERstr);

    for the same reason. To fix this you need to add a NULL char at the end of USERstr before you try to use it--like this:

    USERstr[0] = wParam;
    USERstr[1] = '\0'; //or whatever symbol you like to use for NULL. This assumes that wParam is a valid ASCII char value.

    Also, I don't know whether you can concatenate onto the end of an empty string or not. If not, then you will need to use strcpy() the first time you want to "concatenate" USERstr onto strptr.

  3. #3
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Phildelphia, PA
    What I don't understand is why you want to bother with using char arrays anyway. I'd suggest using the string class in the standard library. Or if you really feel the need to get your hands dirty, write your own string class. Then, once you've perfected that one class, you can use it every other program you write. Ah, the wonders of code reuse.

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  4. #4
    Registered User
    Join Date
    Jan 2003
    I'm taking a class, and trying to learn how to use the API. Up until a few weeks ago, I had only worked with console code (with some success).

    Thanks for that info.

    I'm unfamiliar with the string class.

    I'm having difficulty stepping through my array. I don't know if I need an iteration inside the switch statement or not.

    I'd like to step through the array and fill up as many buckets (up to ten) as I want with numbers (0-9). Convert that string to an integer, add one to the number and then print it out on the screen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. 2 dimension array of characters
    By braddy in forum C Programming
    Replies: 9
    Last Post: 03-15-2006, 12:49 PM
  4. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM