key input definition

This is a discussion on key input definition within the C++ Programming forums, part of the General Programming Boards category; I appreciate all the help you guys have been giving me previously. I wondering how the code works for setting ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    12

    key input definition

    I appreciate all the help you guys have been giving me previously. I wondering how the code works for setting the Enter key to continue through the loop on a program and if it is possible to allow any other key to exit out of the program?

    cin.get()!EOF?

    Code:
    cout<<"To remain in the program, keep pressing ‘Enter’"<<endl;
    	cout<<"Enter any other character to leave:"<<endl;
    
    	int randnum; 
     
    	while(cin.get()!=EOF)
    	{
                   num.RandInt(0,6); 
                   randnum=num.RandInt(0,6);                                                                   
                   cout<<"handles "<<handles[randnum]<<endl; 
    	
    	Bird.getKind();
    	Bird.getmoves();
    	Bird.getCall();
    	
    	}
    This allows me to keep pressing enter to stay in the loop, but I cannot quit the program without pressing ctrl + c.

    Thanks again for all the help.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <iostream>
     
     int main(void)
     {
       std::cout << "To remain in the program, keep pressing [Enter]" <<std::endl 
                 << "Enter any other character to leave:" <<std::endl;
     
       while (std::cin.get() == '\n')
       {
         std::cout << "Still going..." << std::endl;
       }
     
       return(0);
     }
     
     /*
      Output:
     To remain in the program, keep pressing [Enter]
     Enter any other character to leave:
     
     Still going...
     
     Still going...
     
     Still going...
     x
     
     */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    12
    Thanks a lot. I didn't think that the Enter key press was needed with the other keys, but it works fine. Thanks for the reply.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Quote Originally Posted by hkmixxa
    Thanks a lot. I didn't think that the Enter key press was needed with the other keys, but it works fine. Thanks for the reply.
    Then you're after this I guess.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. User Input - Key 'Bouncing' = Bad
    By Tonto in forum Windows Programming
    Replies: 1
    Last Post: 12-09-2006, 08:37 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Key input?
    By Kavity in forum Linux Programming
    Replies: 0
    Last Post: 12-20-2002, 10:01 AM
  5. single key input
    By SPiRiToFCaT in forum C++ Programming
    Replies: 14
    Last Post: 11-15-2002, 01:23 PM

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