473,395 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

std::getline weird problem

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

Jul 23 '05 #1
7 2449
Vladimir Lushnikov wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #2

"Vladimir Lushnikov" <vl****************@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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.
Jul 23 '05 #3
Vladimir Lushnikov wrote:
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/...4f10305a600bcc

Regards,
Vyacheslav

Jul 23 '05 #4
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

Jul 23 '05 #5

"Vladimir Lushnikov" <vl****************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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


Have a look at:

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

Look at the section entitled "Fix to <string>".
Jul 23 '05 #6
Dave wrote:
"Vladimir Lushnikov" <vl****************@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
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

Have a look at:

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

Look at the section entitled "Fix to <string>".


That patch is always a good idea, but it's not related to the original
question.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #7
I'm using the Visual Studio.NET 2005 Beta 2 :)

Thanks for your help everyone.

Jul 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Frank Schmitt | last post by:
Hi! I've just been bitten by the famous Windows newline issue, i.e. tried to read a textfile written by a windows application on an unix platform. When reading a line with ...
4
by: sam | last post by:
Hi, How can I use getline ignore the commandline arguments? Thanks Sam
2
by: jalkadir | last post by:
I am trying to get character string from the user, to do that I use getline(char_type*, streamsize), but I get a segmentation fault??!! Can anyone give me a hand, what am I doing wrong? --snip...
2
by: aGAric | last post by:
i use std::getling to read a line in unicoude file to buffer,like: WCHAR buf wifstream in; std::getling(buf,in,100); but i can't get the right result,it seems can only read by 1 byte;e.g first...
2
by: Bit Byter | last post by:
I have a method in a class implemented like this: int foo (const char* filename, bool flg) { FILE *fp; char *buffer, *tokstr, *tmp; size_t size, toksize; unsigned int i; if ((fp =...
7
by: Chris | last post by:
Running into a problem on Windows. This code std::string randomStuff; std::getline(std::cin, randomStuff); works on unix, but on windows, it requires the user to hit the enter key *twice*...
1
by: Kevin Eller | last post by:
I haven't used std::getline for a long time, if someone can help me
8
by: toton | last post by:
Hi, I am reading some large text files and parsing it. typical file size I am using is 3 MB. It takes around 20 sec just to use std::getline (I need to treat newlines properly ) for whole file in...
6
by: JML | last post by:
Hi, I have some code which parses a text file and creates objects based on what is in the text file. The code works just fine on Windows, but when I compile it using XCode on OS X the parsing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.