C++ cin width Avoids buffer overflow
#include <iostream> #include <iomanip> //for setw using namespace std; int main()// w w w. j a va 2 s . com { const int MAX = 20; //max characters in string char str[MAX]; //string variable str cout << "\nEnter a string: "; cin >> setw(MAX) >> str; //put string in str, // no more than MAX chars cout << "You entered: " << str << endl; return 0; }