C++ char array read from console and avoid buffer overflow

Description

C++ char array read from console and avoid buffer overflow

#include <iostream>
#include <iomanip>                  //for setw
using namespace std;
int main()// w w w.  ja v a2s  .co  m
{
   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;
}



PreviousNext

Related