Not having to hit enter after entering a character via CIN

This is a discussion on Not having to hit enter after entering a character via CIN within the C++ Programming forums, part of the General Programming Boards category; Hi all, My program is running smoothly, but there is something that annoys me about it. cin >> num; // ...

  1. #1
    Unregistered
    Guest

    Question Not having to hit enter after entering a character via CIN

    Hi all,

    My program is running smoothly, but there is something that annoys me about it.

    cin >> num; // requires the user to hit [enter] afterwards

    Is there a way to make it such that they just have to hit a single key ('y' for example) and it will accept that char value without needing the enter key?

    I have tried cin.getline(), but I don't know what I would use for arguments, as that function is generally used when a known character will be entered at the end of the string.

    I am using Bordland C++ Builder 5.0.

    Thanks

  2. #2
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    Code:
    int chars = 5; // number of charcters to read
    char word[4]; // var to store it in
    
    cin.getline(word, 5); // how to use cin.getline

  3. #3
    Unregistered
    Guest
    Thanks, I'll give er a shot

  4. #4
    Refugee face_master's Avatar
    Join Date
    Aug 2001
    Posts
    2,052
    I was just showing you how to use cin.getline. I think you still have to hit [enter] to continue.

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    char Key=0;
    while(Key != 27)
    {
       Key=0;
       if(kbhit()) Key=getch();
    
       ... //Loops until the user press ESC
    }
    However, I don't think kbhit() is a standard function...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  6. #6
    Unregistered
    Guest
    kbhit() isnt standard.
    Cant you use GetKeyState() ?
    Though i think thats for virtual keys only....

  7. #7
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Eeep that was me btw

  8. #8
    Registered User
    Join Date
    Nov 2001
    Posts
    30
    Look up the use of getche() function, which is ANSI standard.

  9. #9
    Registered User heat511's Avatar
    Join Date
    Dec 2001
    Posts
    169

    add conio.h

    it may work then, i think kbhit works for me if i add conio.h

    you can also getch without the kbhit if you want the computer to wait for a key to be pressed...

  10. #10
    Unregistered
    Guest
    Can't you use getch?

  11. #11
    Unregistered
    Guest
    Thank you! getch() worked BEAUTIFULLY

  12. #12
    Unregistered
    Guest
    Glad to help you ;D

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 07:19 PM
  2. Replies: 5
    Last Post: 05-27-2008, 05:29 PM
  3. I need help badly
    By taz_jiggy in forum C++ Programming
    Replies: 4
    Last Post: 04-23-2002, 10:36 PM
  4. entering a long character
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-06-2002, 02:10 PM
  5. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21