Best practice Cstring/int input

Pushing through Prada c++ primer plus and still confused about best way to deal with Cstring input. There seems to be many pitfalls (buffer overflow) and unexpected results (buffer extras ends up answering future query). It also seems that "asks" get saved in a "buffer" and can have consequences (like cin.ignore)?

Ultimately my question is what is the accepted best practice sample code for input Cstrings and non cstring variables that checks for type and cstring array overflow and gets rid of and extra characters or \n's so you are good for the next query. Looking at code, everyone seems to do it differently (some spread cin.get()'s everywhere). What is the bulletproof way to do it so it is not different / customized for every situation?

Sample code from a function for one of my HW problems (9-1). Works well except if I enter a cstring longer than Len. The two options I tried to fix shown, also didn't work. Also tried to use cin.get instead of cin.getline to no avail.

int setgolf(golf& g) // function to fill structure with cstringline and int
{

cout << "F Enter fullname:";
cin.getline(g.filename,Len);
// option 1 - cin.clear();
// option 1 cin.ignore (1000, '\n');

//option 2 - while (cin.get() != '\n') - use this with cin.get
//option 2 continue;


if (g.filename[0] == '\0')
return 0;

cout << "Enter handicap for " << g.filename << ":";
while (!(cin >> g.handicap))
{
cout << "Please enter an integer: ";
cin.clear();
cin.ignore (1000,'\n');

}
cin.ignore (1000, '\n');

return 1; // return successful completion

}

Some other debugging questions - can you see the buffer as you debug/run? Using MAC/Eclipse CDT

Cstrings are driving me crazy - can see why C++ invented string class...
Thanks
Topic archived. No new replies allowed.