basically- the solution would be to not get input directly into a char string, but rather handle it through a C++ string, and then you can test to make sure the char array is long enough to hold it:
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
const int maxlength=15;
char * cstr;
string str;
getline(cin,str);
if(str.length() > maxlength)
{
cout << "error, input string exceeds specified maximum of " << maxlength << ". length is " << str.length() << endl;
}
else
{
cstr = new char [maxlength];
memset(cstr,0,maxlength);
strcpy (cstr, str.c_str());
cout << "successful:" << cstr << endl;
}
int pausevalue;
cin >> pausevalue;
return 0;
}
here we simply read a line from cin into a string. then, if that string is shorter then the given maxlength constant, we reject it and give an error message, otherwise, we use strcpy to copy from the char array that c_str() returns and copy it to the target char array.