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
Is there some way to clear the buffer after using cin >> ?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; }
That seems to be what is causing my problem.



LinkBack URL
About LinkBacks


