std::getline weird problem

V

Vladimir Lushnikov

Hi,

I have just started learning C++ and have been trying to do simple
string manipulation. However, when calling this function:

int helloString()
{
string name;

cout << "Please enter your name:\n";
getline(cin, name);


cout << "Length of string: " << name.length() << "\n";

return 0;
}

The compiled program gives absolutely no control for the user to type
the string in, so the length of it is always zero. helloString() is
called out of main() in a switch() statement [if that knowledge helps].

So does anyone know what the problem is, and why it won't let me type
anything in befere continuing as normal?

Thanks,
Vladimir Lushnikov
 
P

Pete Becker

Vladimir said:
The compiled program gives absolutely no control for the user to type
the string in, so the length of it is always zero. helloString() is
called out of main() in a switch() statement [if that knowledge helps].

The way to track down the cause of this sort of problem is to simplify.
Remove the switch and call the function directly; if that does the same
thing, then the problem is in the function; if it works right, the
problem is elsewhere.
 
D

Dave

Vladimir Lushnikov said:
Hi,

I have just started learning C++ and have been trying to do simple
string manipulation. However, when calling this function:

int helloString()
{
string name;

cout << "Please enter your name:\n";
getline(cin, name);


cout << "Length of string: " << name.length() << "\n";

return 0;
}

The compiled program gives absolutely no control for the user to type
the string in, so the length of it is always zero. helloString() is
called out of main() in a switch() statement [if that knowledge helps].

So does anyone know what the problem is, and why it won't let me type
anything in befere continuing as normal?

Thanks,
Vladimir Lushnikov

If by chance you're using Visual C++ 6.0, there is a known problem with
std::getline() in that compiler's Standard Library implementation. A fix is
available (ask for more info if you're on that platform). If you're on any
other platform, please disregard this reply.
 
V

Vyacheslav Kononenko

Vladimir said:
The compiled program gives absolutely no control for the user to type
the string in, so the length of it is always zero. helloString() is
called out of main() in a switch() statement [if that knowledge helps].

So does anyone know what the problem is, and why it won't let me type
anything in befere continuing as normal?

Thanks,
Vladimir Lushnikov

I suspect you do something like cin >> choice; switch( choice ) in your
code before this. If so read this
http://groups-beta.google.com/group...rriage+return+getline&rnum=3#ac4f10305a600bcc

Regards,
Vyacheslav
 
V

Vladimir Lushnikov

Yes, I am using both Visual C++ and cin >> choice;

I also tried this in Cygwin-GCC and it is the same problem. Adding

cin.ignore(numeric_limits<streamsize>::max(), '\n');

did solve the problem.

Thanks
 
V

Vladimir Lushnikov

I'm using the Visual Studio.NET 2005 Beta 2 :)

Thanks for your help everyone.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top