all groups > dotnet academic > november 2003 >
You're in the

dotnet academic

group:

C++ cin.getline()


C++ cin.getline() Robert
11/19/2003 8:19:06 PM
dotnet academic:
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,

Re: C++ cin.getline() Peter van der Goes
11/20/2003 8:17:29 AM

[quoted text, click to view]

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]

Re: C++ cin.getline() Robert
11/20/2003 12:17:15 PM
Thanks for your suggestions Peter. When I tried the
first option using C-style character arrays, the program
would not allow spaces between words (ie "Joe Public").
I tried a variation of the first and second options:

cout << "Please enter your name: ";
getline(cin,myName);
cin.ignore(100,'\n');

The only problem with this is I have to press "enter"
twice before the input is accepted. Is there a way to
eliminate the need for twice pressing "enter"?

Thanks again,

Robert

[quoted text, click to view]
Re: C++ cin.getline() Peter van der Goes
11/21/2003 7:31:23 AM

[quoted text, click to view]
The "enter twice" appears to be caused by a known bug in the STL.
Read more here:

http://www.dinkumware.com/vc_fixes.html

This info is quite old, and I'm surprised it hasn't been fixed.
I'll ask elsewhere and get back to you.

--
Peter - [MVP - .NET Academic]

Re: C++ cin.getline() Peter van der Goes
11/22/2003 7:20:32 AM

[quoted text, click to view]
So far, all I have from the private newsgroup where I asked is the same info
I've already provided. It appears this bug is still alive and that the patch
at the link I provided previously is the solution. I'm keeping threads open
in two other places in the hope that something else will surface.

--
Peter - [MVP - .NET Academic]

Re: C++ cin.getline() Peter van der Goes
11/22/2003 2:35:56 PM

[quoted text, click to view]
OK, it finally occurred to me that the cin.ignore() is causing the problem.
I also got a corrective update in the private newsgroup that the referenced
patch does *not* need to be applied in .NET.
Try this:

string newName;

cout <<"Please enter your name? \n";


getline(cin, newName);

cout << endl << newName << endl;

cout << "Please number of scores? \n";

cin >> numScores;

cout << numScores << endl;

This works fine for me here, with no need to press Enter twice.


--
Peter - [MVP - .NET Academic]

AddThis Social Bookmark Button