Thread: Getting input without the user having to press Enter...

  1. #1
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Post Getting input without the user having to press Enter...

    How do I get some input without the user having to press enter?
    (I read the FAQ on that, but I didn't quite understand it).

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    what are the input?

    what are the inputs?? int? char? or string?

  3. #3
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361

    Re: Getting input without the user having to press Enter...

    Originally posted by Ranedhel
    How do I get some input without the user having to press enter?
    (I read the FAQ on that, but I didn't quite understand it).
    What don't you understand?

  4. #4
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Re:

    The input is a 'int'.
    -Elven Forge Software-
    (Lead Designer)

    http://www.geocities.com/elvenforge

    **infected by frenchfry164**
    I am a signature virus. Please add me to your signature so that I may multiply

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Code:
    http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385
    A compiler specific implementation using getch()
    
    #include <stdio.h> /* for printf() */ 
    #include <conio.h> /* for getch()  */
    
    int main(void)
    {
       printf("Press a key to continue...");
       getch();
       return 0;
    }
    assUming you don't have a problem using <conio.h> this answer should suffice. I don't know what you didn't "quite understand" on the faq page, but perhaps you can tell us that and we can explain it to you.
    Code:
    bool Console::keyHit() {
      HANDLE stdIn = GetStdHandle(STD_INPUT_HANDLE);
    
      DWORD saveMode;
      GetConsoleMode(stdIn, &saveMode);
      SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
    
      bool ret = false;
    
      if (WaitForSingleObject(stdIn, 1) == WAIT_OBJECT_0)
        ret = true;
    
      SetConsoleMode(stdIn, saveMode);
    
      return ret;
    }
    
    bool Console::getChar(TCHAR &ch) {
      bool ret = false;
    
      if (!isValid())
        return ret;
    
      HANDLE stdIn = GetStdHandle(STD_INPUT_HANDLE);
    
      DWORD saveMode;
      GetConsoleMode(stdIn, &saveMode);
      SetConsoleMode(stdIn, ENABLE_PROCESSED_INPUT);
    
      if (WaitForSingleObject(stdIn, INFINITE) == WAIT_OBJECT_0) {
        DWORD num;
        ReadConsole(stdIn, &ch, 1, &num, NULL);
    
        if (num == 1)
          ret = true;
      }
    
      SetConsoleMode(stdIn, saveMode);
    
      return ret;
    }
    
    TCHAR getChar() { TCHAR ch = 0; getChar(ch); return ch; }
    If you don't mind using <windows.h> you can use oen of these functions (from a Console class I wrote once upon a time). The first one just returns true if a key has been pressed. The second one waits for a keypress and sets 'ch' to its value. The third also waits for a key to be pressed but returns the value (requiring no arguments to the function).

    Hey admins
    Perhaps these solutions could reside as alternate solutions on that faq page... Just a thought.
    Last edited by LuckY; 07-15-2003 at 10:51 PM.

  6. #6
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Re:

    I sorta like to understand every bit of what I put into my game, so I'd have to learn about windows programming before I can use your code(but still, thanks!).

    Here is what I don't understand about the example in the FAQ:

    while ((ch = getch()) != EOF && ch != '0')

    How does this work, and more importantly; can I make more than one possible input(besides 0).
    -Elven Forge Software-
    (Lead Designer)

    http://www.geocities.com/elvenforge

    **infected by frenchfry164**
    I am a signature virus. Please add me to your signature so that I may multiply

  7. #7
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Lightbulb Re:

    Nevermind, I understand how it works(duh!).

    Here is my code, could you show me how to edit it?
    (I know there are alot of unnecessary lines, but I haven't got around to refining it yet.)

    Code:
    #include <iostream.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int quit;
    int input;
    int x = 1;
    int y = 1;
    int recentx;
    int recenty;
    
    int main()
    {
    while (quit == 0)
    {
    char map[5][5];
    if (x < 1)
    {
    x = x + 1;
    }
    if (x > 5)
    {
    x = x - 1;
    }
    if (y < 1)
    {
    y = y + 1;
    }
    if (y > 5)
    {
    y = y - 1;
    }
    if (map[x][y] == (char)35)
    {
    x = recentx;
    y = recenty;
    }
    map[1][1] = (char)46;
    map[1][2] = (char)46;
    map[1][3] = (char)46;
    map[1][4] = (char)46;
    map[1][5] = (char)46;
    map[1][6] = (char)46;
    map[1][7] = (char)46;
    map[1][8] = (char)46;
    map[1][9] = (char)46;
    map[1][10] = (char)46;
    map[2][1] = (char)46;
    map[2][2] = (char)46;
    map[2][3] = (char)46;
    map[2][4] = (char)46;
    map[2][5] = (char)46;
    map[2][6] = (char)46;
    map[2][7] = (char)46;
    map[2][8] = (char)46;
    map[2][9] = (char)46;
    map[2][10] = (char)46;
    map[3][1] = (char)46;
    map[3][2] = (char)46;
    map[3][3] = (char)46;
    map[3][4] = (char)46;
    map[3][5] = (char)46;
    map[3][6] = (char)46;
    map[3][7] = (char)46;
    map[3][8] = (char)46;
    map[3][9] = (char)46;
    map[3][10] = (char)46;
    map[4][1] = (char)46;
    map[4][2] = (char)46;
    map[4][3] = (char)46;
    map[4][4] = (char)46;
    map[4][5] = (char)46;
    map[4][6] = (char)46;
    map[4][7] = (char)46;
    map[4][8] = (char)46;
    map[4][9] = (char)46;
    map[4][10] = (char)46;
    map[5][1] = (char)46;
    map[5][2] = (char)46;
    map[5][3] = (char)46;
    map[5][4] = (char)46;
    map[5][5] = (char)46;
    map[5][6] = (char)46;
    map[5][7] = (char)46;
    map[5][8] = (char)46;
    map[5][9] = (char)46;
    map[5][10] = (char)46;
    map[6][1] = (char)46;
    map[6][2] = (char)46;
    map[6][3] = (char)46;
    map[6][4] = (char)46;
    map[6][5] = (char)46;
    map[6][6] = (char)46;
    map[6][7] = (char)46;
    map[6][8] = (char)46;
    map[6][9] = (char)46;
    map[6][10] = (char)46;
    map[7][1] = (char)46;
    map[7][2] = (char)46;
    map[7][3] = (char)46;
    map[7][4] = (char)46;
    map[7][5] = (char)46;
    map[7][6] = (char)46;
    map[7][7] = (char)46;
    map[7][8] = (char)46;
    map[7][9] = (char)46;
    map[7][10] = (char)46;
    map[8][1] = (char)46;
    map[8][2] = (char)46;
    map[8][3] = (char)46;
    map[8][4] = (char)46;
    map[8][5] = (char)46;
    map[8][6] = (char)46;
    map[8][7] = (char)46;
    map[8][8] = (char)46;
    map[8][9] = (char)46;
    map[8][10] = (char)46;
    map[9][1] = (char)46;
    map[9][2] = (char)46;
    map[9][3] = (char)46;
    map[9][4] = (char)46;
    map[9][5] = (char)46;
    map[9][6] = (char)46;
    map[9][7] = (char)46;
    map[9][8] = (char)46;
    map[9][9] = (char)46;
    map[9][10] = (char)46;
    map[10][1] = (char)46;
    map[10][2] = (char)46;
    map[10][3] = (char)46;
    map[10][4] = (char)46;
    map[10][5] = (char)46;
    map[10][6] = (char)46;
    map[10][7] = (char)46;
    map[10][8] = (char)46;
    map[10][9] = (char)46;
    map[10][10] = (char)46;
    map[x][y] = (char)64;
    system("cls");
    cout << map[1][1] << map[1][2] << map[1][3] << map[1][4] << map[1][5] << map[1][6] << map[1][7] << map[1][8] << map[1][9] << map[1][10] << endl;
    cout << map[2][1] << map[2][2] << map[2][3] << map[2][4] << map[2][5] << map[2][6] << map[2][7] << map[2][8] << map[2][9] << map[2][10] << endl;
    cout << map[3][1] << map[3][2] << map[3][3] << map[3][4] << map[3][5] << map[3][6] << map[3][7] << map[3][8] << map[3][9] << map[3][10] << endl;
    cout << map[4][1] << map[4][2] << map[4][3] << map[4][4] << map[4][5] << map[4][6] << map[4][7] << map[4][8] << map[4][9] << map[4][10] << endl;
    cout << map[5][1] << map[5][2] << map[5][3] << map[5][4] << map[5][5] << map[5][6] << map[5][7] << map[5][8] << map[5][9] << map[5][10] << endl;
    cout << map[6][1] << map[6][2] << map[6][3] << map[6][4] << map[6][5] << map[6][6] << map[6][7] << map[6][8] << map[6][9] << map[6][10] << endl;
    cout << map[7][1] << map[7][2] << map[7][3] << map[7][4] << map[7][5] << map[7][6] << map[7][7] << map[7][8] << map[7][9] << map[7][10] << endl;
    cout << map[8][1] << map[8][2] << map[8][3] << map[8][4] << map[8][5] << map[8][6] << map[8][7] << map[8][8] << map[8][9] << map[8][10] << endl;
    cout << map[9][1] << map[9][2] << map[9][3] << map[9][4] << map[9][5] << map[9][6] << map[9][7] << map[9][8] << map[9][9] << map[9][10] << endl;
    cout << map[10][1] << map[10][2] << map[10][3] << map[10][4] << map[10][5] << map[10][6] << map[10][7] << map[10][8] << map[10][9] << map[10][10] << endl;
    cin >> input;  
    switch (input)
    {
    case 0:
    quit = 1;
    break;
    case 2:
    recenty = y;
    recentx = x;
    x = x + 1;
    break;
    case 4:
    recenty = y;
    recentx = x;
    y = y - 1;
    break;
    case 6:
    recenty = y;
    recentx = x;
    y = y + 1;
    break;
    case 8:
    recenty = y;
    recentx = x;
    x = x - 1;
    break;
    default:
    quit = 0;
    break;
    }
    }
    exit(0);
    }
    Last edited by Ranedhel; 07-16-2003 at 12:40 PM.
    -Elven Forge Software-
    (Lead Designer)

    http://www.geocities.com/elvenforge

    **infected by frenchfry164**
    I am a signature virus. Please add me to your signature so that I may multiply

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Dude: code tags please
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Hey admins
    Perhaps these solutions could reside as alternate solutions on that faq page... Just a thought.<<
    Done.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Ranedhel, you should really consider learning file i/o in C++. Your code is horrible.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #11
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Learn about using loops:
    Code:
    for (int x=0;x<arrayLenX;x++)
      for (int y=0;y<arrayLenY;y++)
          array[x][y]=theValueIWant;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  12. #12
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Re:

    If you would have read what I said, I haven't gotten around to refining it yet.

    Sorry 'bout the code tags though.
    -Elven Forge Software-
    (Lead Designer)

    http://www.geocities.com/elvenforge

    **infected by frenchfry164**
    I am a signature virus. Please add me to your signature so that I may multiply

  13. #13
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    Re: Re:

    Originally posted by Ranedhel
    If you would have read what I said, I haven't gotten around to refining it yet.
    Not that I'm trying to ream you or anything, but lets call a spade a spade. No one would set every single element manually in a two-dimensional array because they were going to "refine it later." That's like writing a novel with a pencil because you wanted to type it later or perhaps blowing into a tire because you want to use a pump later. Just be honest and say that you didn't know you could do it with a nested loop... It's okay, really. No one will poke fun at you. They may even respect you more for being able to admit it.. You never know.

  14. #14
    Registered User Ranedhel's Avatar
    Join Date
    Jun 2003
    Posts
    34

    Re:

    LoL, actually I did know I could do it with a loop, but I wasn't sure how(still not, when I try to display it it does every thing one dimensional).

    Code:
    for (int a = 0; a <= 10; a++)
     {
     for (int b = 0; b <= 10; b++)
      {
       cout << array[a][b] << endl;
      }
     }
    BTW: Your similes aren't entirely accurate.
    Last edited by Ranedhel; 07-16-2003 at 01:15 PM.
    -Elven Forge Software-
    (Lead Designer)

    http://www.geocities.com/elvenforge

    **infected by frenchfry164**
    I am a signature virus. Please add me to your signature so that I may multiply

  15. #15
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856

    Re: Re:

    Originally posted by Ranedhel
    BTW: Your similes aren't entirely accurate.
    Well okay next time I say something ridiculous you come up with something better. I thought they worked just fine... What's inaccurate about them?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still problems with ceasar shift
    By trevordunstan in forum C Programming
    Replies: 2
    Last Post: 09-14-2008, 01:49 AM
  2. make user press enter / wierd code thingy
    By CorvusVita in forum C++ Programming
    Replies: 10
    Last Post: 04-05-2006, 09:55 AM
  3. What Would You Use To Read User Input?
    By djwicks in forum C Programming
    Replies: 11
    Last Post: 04-05-2005, 03:32 PM
  4. Einstine calculator Mark 2
    By Mach_ie in forum C Programming
    Replies: 1
    Last Post: 08-31-2003, 01:54 PM
  5. Einstine calculator
    By Mach_ie in forum C Programming
    Replies: 2
    Last Post: 08-30-2003, 09:37 AM