Thread: Beginner giving question about cin another try :-)

  1. #1
    johan
    Guest

    Question Beginner giving question about cin another try :-)

    Hi!

    I would like to know if there is any way in C++ to scan the keyboard without having to press the Enter key?

    c=cin.get; // doesn't work
    c=cin.get(); // waits for the Enter key

    Thanks
    Johan

  2. #2
    Unregistered
    Guest
    cin.getch( ) doesnt wait for the enter key. Or so I've been told...

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    387
    why not use getch()?

  4. #4
    johan
    Guest
    getch() halts the program until a key is pressed.
    kbhit() is excellent but it doesn't tell you WHICH key was pressed and I haven't figured out how to reset kbhit() from 1 (which was the value when it was pressed) back to 0.

    I really need an answer to this so if there is anyone who can help me I would be thankful.

    /Johan

  5. #5
    Registered User Tazar's Avatar
    Join Date
    Aug 2001
    Posts
    13
    Just use it like this (I'm at school right now so I have not compiled this but I think it will work fine)
    I hope this helps you guys out a bit ^_^

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <dos.h>                                    //Not really sure if you
                                                                    //Need to include dos.h
    
    int main()
    {
     printf("Looping press q to quit.\n");        //Message.
     bool running=true;                                  //Make running true 
     int n;                                                       //make counter var.
     char k;                                                    //if the prog does not 
                                                                    //run make this int.
     while(running)                                         //Loop while running.
     {
      if(!kbhit())                                               //Wait for user to hit kb.
      {
       k=getch();                                             //Get user input.
      switch(k)                                                //Look at input.
       {
         case "q":                                             //User wants to quit.
          running=false;                                   //Quit.
         break;                                                 //Done with switch.
      }
     }
    n++;                                                        //add to counter.
    printf("Looped %d times\n",n);               //Show counter.
    }
     return 0;
    }
    Sorry if there are any errors I don't have a compiler to test on at school right now. ^_^
    Tazar The Demon
    -------------------------------------
    Please check my site out at: http://www.lrs.8m.com thanks =)
    -------------------------------------
    Note:
    I will help newbies and any other person that I can just ask I'll try to help you out =)

    My MSN Messager name is: [email protected] see if I'm online maybe we can talk. =)

  6. #6
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    few problems I think I've fixed:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #define TRUE 1
    #define FALSE 0
    
    int main()
    {
     int running=TRUE;                                  //Make running true
     int n = 0;                                                       //make counter var.
     char k;                                                    //if the prog does not
     printf("Looping, press q to quit.\n");        //Message.
     while(running)                                         //Loop while running.
     {
      if(kbhit())                                               //Wait for user to hit kb.
      {
       k=getch();                                             //Get user input.
       switch(k)                                                //Look at input.
       {
         case 'q':                                             //User wants to quit.
          running=FALSE;                                   //Quit.
         break;
       }                                              //Done with switch.
      }
      n++;                                                        //add to counter.
      printf("Looped %d times\n",n);               //Show counter.
     }
     return 0;
    }
    Edit - works now
    Last edited by Brian; 02-27-2002 at 12:29 PM.

  7. #7
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Excellent post

  8. #8
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    Continuing the question...

    I have a question now...

    By combining kbhit( ) and getch( ) while switching what the user entered, how would you catch the UP or LEFT arrow being hit? I did a test program to see what # and character they outputted and each arrow key (UP, LEFT, RIGHT, & DOWN) outputted 224 and the character for the greek alpha. Then it locks up the program.

    Here is the code I used to test it out (pretty much same as above):
    Code:
    #include <iostream>
    #include <cstdlib>
    #include <conio.h>
    
    using namespace std;
    
    void goodbye(void) { cout << "Goodbye!\n"; }
    
    int main()
    {
       int running = 1;
       int c;
    
       while( running )
       {
          if( !kbhit() )        // If the keyboard hasn't been hit..
          {
             c = getch();	// Snatch the character from user
    
             cout << (int)c << " " << (char)c << endl;
    
             if( c == 'q' )
                running = 0;
          }
       }
    
       atexit(goodbye);
       return 0;
    }
    Any ideas?

    Thanks

  9. #9
    Registered User Tazar's Avatar
    Join Date
    Aug 2001
    Posts
    13
    The problem is that the arrow keys and all F keys are extended and return 0 then the number for the key

    I changed your code to work and it works fine now

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    
    void goodbye(void) { cout << "Goodbye!\n"; }
    
    int main()
    {
    	int running = 1;
    	int c;
    
    	while( running )
    	{
    		if( !kbhit() )        // If the keyboard hasn't been hit..
    		{
    			c = getch();	// Snatch the character from user
    			if(c==0)c = getch();	// Snatch the character from user
    
    			cout << (int)c << " " << (char)c << endl;
    
    			if( c == 'q' )
    				running = 0;
    		}
    	}
    
    	atexit(goodbye);
    	return 0;
    }
    Hope it helps ^_^
    Tazar The Demon
    -------------------------------------
    Please check my site out at: http://www.lrs.8m.com thanks =)
    -------------------------------------
    Note:
    I will help newbies and any other person that I can just ask I'll try to help you out =)

    My MSN Messager name is: [email protected] see if I'm online maybe we can talk. =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another beginner question
    By jcafaro10 in forum C++ Programming
    Replies: 40
    Last Post: 07-31-2007, 04:06 PM
  2. simple cin question
    By verbity in forum C++ Programming
    Replies: 25
    Last Post: 05-09-2007, 03:02 PM
  3. Help Please, Beginner Question, rewrite loop
    By office888 in forum C Programming
    Replies: 4
    Last Post: 12-11-2006, 10:07 AM
  4. Replies: 14
    Last Post: 12-06-2006, 04:58 PM
  5. Beginner on Win32 apps, lame question.
    By Templario in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 08:39 PM