Avoids buffer overflow with cin.width - C++ File Stream

C++ examples for File Stream:cin

Description

Avoids buffer overflow with cin.width

Demo Code

#include <iostream>
#include <iomanip>                  //for setw
using namespace std;
int main()// w  ww  .  j a va  2 s .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;
}

Result


Related Tutorials