Thread: Is there any way to clear the keyboard buffer?

  1. #1
    Shadow12345
    Guest

    Is there any way to clear the keyboard buffer?

    I have a program using a vector of chars. For some reason this does not let me enter the first character.


    code:--------------------------------------------------------------------------------
    #include <iostream>
    #include <vector>
    #include <string.h>

    using namespace std;

    typedef vector<char*> ChaVec;
    ChaVec CharacterVector;

    int main() {
    int NumOfChars;

    cout << "How many characters do you want created? " << endl;

    cin >> NumOfChars;

    for(int i = 0; i < NumOfChars; i++) {
    CharacterVector.push_back(new char[50]);
    cout << "Please enter what you want string number " << i << " to be" << endl;
    cin.getline(CharacterVector[i], 50);
    cout << "CharacterVector["<<i<<"] is " << CharacterVector[i] << endl;
    }

    return 0;
    }
    --------------------------------------------------------------------------------

    On the other hand this does work
    Code:
    #include <iostream>
    #include <vector>
    #include <string.h>
    
    using namespace std;
    
    typedef vector<char*> ChaVec;
    ChaVec CharacterVector;
    
    int main() {
    	int NumOfChars;
    
    	cout << "Five characters will be created for you, stupid ****ing ****bag " << endl;
    	
    	
    	CharacterVector.push_back(new char[50]);
    	for(int i = 0; i < 5; i++) {
    		CharacterVector.push_back(new char[50]);
    		cout << "Please enter what you want string number " << i << " to be" << endl;
    		cin.getline(CharacterVector[i], 50);
    		cout << "CharacterVector["<<i<<"] is " << CharacterVector[i] << endl;
    	}
    
    	return 0;
    }
    Is there some way to clear the buffer after using cin >> ?
    That seems to be what is causing my problem.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    I think its cin.ignore
    But you'll need to look up the exact usage

    > For some reason this does not let me enter the first character
    The first getline just uses the \n left behind by

    cin >> NumOfChars;

  3. #3
    Shadow12345
    Guest
    thanks cin.ignore() works for me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a buffer line by line, while using system read
    By Hammad Saleem in forum C Programming
    Replies: 9
    Last Post: 05-27-2008, 05:41 AM
  2. writing a file from buffer
    By mariano_donati in forum C Programming
    Replies: 8
    Last Post: 02-25-2008, 02:04 AM
  3. Buffer problem
    By Narcose in forum Game Programming
    Replies: 3
    Last Post: 03-24-2006, 09:29 AM
  4. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 04:46 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 05:15 AM