[quoted text, click to view] "Robert" <anonymous@discussions.microsoft.com> wrote in message
news:093a01c3af1d$6fcfaf20$a301280a@phx.gbl...
> Hi:
>
> I am using C++ to create a student class. I need to
> input data from the keyboard and process it inside the
> class. The code looks like:
>
> cout << "What is your name: ";
> cin.getline(response,maxCharacters);
> myStudent.setName(response);
>
> The problem is that if I enter more than the maximum
> number of characters, later lines are not executed. How
> can I ignore any additional characters (ie maxCharacters
> + 1) so that I can process other fields?
>
> Thanks,
>
> Robert
You can "format" your input to limit the number of characters accepted, then
get rid of extra characters, as:
char myName[12];
cout <<"Please enter your name? \n";
cin >> setw(8) >> myName;
cin.ignore(100, '\n');
cout << endl << myName << endl;
or, you can research the C++ string class and it's advantages over the
C-style strings, or char arrays used here.
To use the string class:
#include <string>
string myName;
cout <<"Please enter your name? \n";
getline(cin, myName);
cout << endl << myName << endl;
Peter - [MVP - .NET Academic]