Using getch() function

This is a discussion on Using getch() function within the C++ Programming forums, part of the General Programming Boards category; I need to get input without having to press enter. Something like: cout<<"Enter a Number"<<endl; --Something goes here-- switch(number){ case ...

  1. #1
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47

    Using getch() function

    I need to get input without having to press enter.
    Something like:


    cout<<"Enter a Number"<<endl;

    --Something goes here--

    switch(number){
    case 1:
    cout<<"This is number 1"<<endl;
    break;
    case 2:
    cout<<"This is number 2"<<endl;
    break;
    }

    Thanks

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    --Something goes here--
    number = getch();

    --Or if you want the number echoed as it's typed--
    number = getche();

    --To use, include <conio.h>

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Actually, to use the switch() statement:
    Code:
    cout<<"Enter a Number"<<endl; 
    
    //--Something goes here-- 
    int number;
    number = getch();
    
    switch(number){ 
    case '1': 
    cout<<"This is number 1"<<endl; 
    break; 
    case '2': 
    cout<<"This is number 2"<<endl; 
    break; 
    }

  4. #4
    Registered User BigSter's Avatar
    Join Date
    Nov 2001
    Posts
    47
    Hey Thanks

    This works:

    number = getche();

  5. #5
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    so...

    if you had a picture on the screen that you wanted the user to move around, you could use something like that to do it. like put it in a loop. then just change the position and re-draw acording to what they put in. something like that, eh? for msvc++ 6 do you still have to use the _getch(); ? or _getche(); ?

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    If you continue to mix old C style I/O and C++'s stream I/O classes you will eventually run into problems which will be difficult to find. Use one, or the other, not both.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    So Adrian, are you saying do this?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
       int number;
       printf("Enter a Number\n"); 
       number = getche();
    
       switch(number){ 
       case '1': 
          printf("This is number 1\n"); 
          break; 
       case '2': 
          printf("This is number 2\n"); 
          break; 
       }
    
       return 0;
    }
    Last edited by swoopy; 11-29-2001 at 02:51 PM.

  8. #8
    Unregistered
    Guest
    I keep getting very strange results when I mix getch() within couts. It keeps cutting off the previous couts. Does anyone know why?

  9. #9
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Using cout wil place output in a buffer that is normally flushed automatically when cin is used to obtain input. getch() isn't buffered and cout's buffer will not be flushed when it's used to get input. If you're using both you should use flush or endl before using getch(). -

    cout << some_output << flush;
    ch=getch();
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM

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