Thread: Other than cin, how can a get a program to take input?

  1. #1
    Hidoi Ryuujin
    Join Date
    Nov 2002
    Posts
    220

    Question Other than cin, how can a get a program to take input?

    [noobism]
    I'm thinking about trying to do a menu in a program, but would rather have the input just be single key presses rather than "a <enter>, b <enter>..." I have a chart with keyboard scan codes but don't know what to use the with. I know how to use scan codes to make getch require specific keys but don't think that getch would work.
    [/noobism]
    One death is a tragedy, one million... a statistic.
    -Josef Stalin

    In case I forget, I use Bloodshed Dev C++ v.4

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    try switch statements

    Code:
    #include <iostream.h>
    
    int main()
    {
    char message;
    
    cin>>message;
    
    switch(message);
    {
    case a:
    
    cout<<"u pressed a";
    break;
    case b:
    cout<<"u pressed b";
    break;
    default:
    cout<<"not a valid key";
    break;
    }
    
    return 0;
    }
    p.s
    no error checking made!

  3. #3
    Hidoi Ryuujin
    Join Date
    Nov 2002
    Posts
    220
    but cin requires use of the enter key on the user, which is what I want to avoid. Besides, I thought switch only worked with ints.
    One death is a tragedy, one million... a statistic.
    -Josef Stalin

    In case I forget, I use Bloodshed Dev C++ v.4

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    thats not true you can use chars too. but why dont you think
    getch would work?

  5. #5
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    With a little skill and fantasy you can make your own CIN with
    getch();

  6. #6
    Hidoi Ryuujin
    Join Date
    Nov 2002
    Posts
    220
    I was unsure as to whether getch just pauses the program or if it could be used as a condition in, say, an if loop.
    One death is a tragedy, one million... a statistic.
    -Josef Stalin

    In case I forget, I use Bloodshed Dev C++ v.4

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    1,640
    getch() doesn't pauses the game, unless its buffer is empty IM NOT SURE

  8. #8
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>getch() doesn't pauses the game, unless its buffer is empty IM NOT SURE
    getch() is just a blocking read, if there's input waiting then it takes it immediately, otherwise it blocks anything else in the process and waits for input.
    *Cela*

  9. #9
    Hidoi Ryuujin
    Join Date
    Nov 2002
    Posts
    220
    I'll try using getch tonight, I'll post my code and results tomorrow.
    One death is a tragedy, one million... a statistic.
    -Josef Stalin

    In case I forget, I use Bloodshed Dev C++ v.4

  10. #10
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    getch is not a standard C++ function.

    There is no standard way to take input in the manner you want. You'd have to use getch or other functions.

    getch is okay, but the downside is you can only really use it with letter keys, numbers, etc. You can't use it to detect arrow keys or the FKeys, etc. and also, the function takes into account shift so it detects shift A as being different from A by itself, which can be good or bad depending on what you want to do. Also, if there's no input it will force your program to wait for it, which is very bad in a realtime game.

    If you are using windows, you can use ReadConsoleInput, which will let you read the input buffer in groups. It also provides info for both keydown and keyup events. You can use virtual and OEM keys to be able to detect all the keys on a standard keyboard and even mouse buttons. You can also detect the current position of the mouse in the console window after movement (but not its direction and degree of movement, unfortunately).

    You can also check to see if there are no input records in the buffer before reading so that your program doesn't wait for input when there isn't any.
    Last edited by Polymorphic OOP; 01-29-2003 at 04:39 PM.

  11. #11
    Hidoi Ryuujin
    Join Date
    Nov 2002
    Posts
    220
    Getch does work with the arrow keys, I tested.

    However, getch has another issue. Scancodes go in, but scancodes don't come out. Getch spits out codes of its own, which I didn't have time to chart last night. However, I am attaching a program that ended up as an infinite recursion to test the codes with. Just ignore the press enter, any key will work.

    It's extremely sloppy, and you don't want to see the source code.
    Last edited by deathstryke; 01-30-2003 at 08:20 AM.
    One death is a tragedy, one million... a statistic.
    -Josef Stalin

    In case I forget, I use Bloodshed Dev C++ v.4

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

  13. #13
    Hidoi Ryuujin
    Join Date
    Nov 2002
    Posts
    220
    Any idea if that solution works with conio_mingw? I use dev c++ and so don't have full use of conio.
    One death is a tragedy, one million... a statistic.
    -Josef Stalin

    In case I forget, I use Bloodshed Dev C++ v.4

  14. #14
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by deathstryke
    Any idea if that solution works with conio_mingw? I use dev c++ and so don't have full use of conio.
    Dont know, sorry.

    >>However, getch has another issue. Scancodes go in, but scancodes don't come out.
    Can you be more specific, ie provide some code and example results.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  15. #15
    Hidoi Ryuujin
    Join Date
    Nov 2002
    Posts
    220
    Code:
    #include <iostream>
    #include <conio_mingw.h>
    
    Int main()
    {
    int g = getch();
    cout<< g;
    return 0;
    }
    press <enter> (scancode 28)
    spits out 013

    the arguement in getch doesn't seem to matter when used to assign a variable, otherwise the arguement is the scancode that getch waits for. It does similar stuff with other keys as well. I didn't have time to write any down and don't feel like fooling with it at the moment (Bill might start teaching at any moment).

    Sorry for the earlier sloppyness.
    Last edited by deathstryke; 01-30-2003 at 10:45 AM.
    One death is a tragedy, one million... a statistic.
    -Josef Stalin

    In case I forget, I use Bloodshed Dev C++ v.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  4. CIN Input count question?
    By kamran in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2006, 04:06 PM
  5. Replies: 1
    Last Post: 08-11-2006, 05:44 AM