Is there any way to clear the keyboard buffer?

This is a discussion on Is there any way to clear the keyboard buffer? within the C++ Programming forums, part of the General Programming Boards category; I have a program using a vector of chars. For some reason this does not let me enter the first ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    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, 01:04 AM
  3. Buffer problem
    By Narcose in forum Game Programming
    Replies: 3
    Last Post: 03-24-2006, 08:29 AM
  4. Having Buffer Problems With Overlapped I/O --
    By Sargera in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 03:46 PM
  5. Console Screen Buffer
    By GaPe in forum Windows Programming
    Replies: 0
    Last Post: 02-06-2003, 04:15 AM

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