Thread: Words waiting in input buffer

  1. #1
    the person
    Guest

    Question Words waiting in input buffer

    How do you tell if there is a word waiting in the input buffer? I want the user to be able to enter more than one word, but I want to store them in seperate character arrays. I tried using kbhit(), it hasn't worked. The words keep getting pulled out on following cin calls, instead of inside my looop. I'm using Borland C++ Builder 5, and the cin and cout stream objects. Thanks for any help. Here's the code that I've tried:

    void GetCommand( char *s)
    {
    int i=0;
    cout<<s;
    cin>>command;
    while ( kbhit() )
    {
    cin>>word[i];
    i++;
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    156
    Here's one way to do it. You'll need to replace _getche( ), with the borland equivalent. The function gets a character and echoes the character to stdout.

    Code:
    #include "stdafx.h"
    #include<iostream> 
    using namespace std;
    #include <conio.h>
    
    #define NUMBER_OF_WORDS 20
    char szWord[NUMBER_OF_WORDS][81] = {0};
    
    enum status
    {
    	eol,
    	more
    };
    
    status GetCommand( char * pszWords );
    
    int main(void) 
    { 
    
    	for( int i = 0; 1; i++ )
    	{
    		if( eol == GetCommand( &szWord[i][0] ) )
    			break;
    		
    	}
    	cout << "done " << endl;
    
    	return 1;
    }
    
    status GetCommand( char * pszWords )
    {
    	if( NULL == pszWords )
    		return eol;
    
    	status st = more;
    	char ch;
    
    	for( int i = 0; '\r' != ch &&  ' ' != ch; i++ )
    	{
    		ch = _getche( );
    
    		switch( ch )
    		{
    			case ' ': 
    				break;
    			case '\r': 
    				cout << endl;
    				st = eol;
    				break;
    			default :
    				*( pszWords + i ) = ch;
    				break;
    		}
    	}
    
    	return st;
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    char word1[80];
    char word2[80];

    cout << "enter two words separated by a space " << endl;
    cout << "hit enter key after typing in the words" << endl;
    cin >> word1 >> word2;

    cout << word1 << word2;

    If user enters only 1 word then the second >> will fail. If the user enters three words instead of two before hitting the enter key the third word will remain in the input buffer and may cause problems later if not removed by the ignore() method.

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. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 09:12 PM
  3. text input buffer clearing
    By red_Marvin in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2003, 03:17 PM
  4. getline problem
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 10-06-2001, 09:28 AM