473,651 Members | 2,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2458
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.goo glegroups.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(nume ric_limits<stre amsize>::max(), '\n');

did solve the problem.

Thanks

Jul 23 '05 #5

"Vladimir Lushnikov" <vl************ ****@gmail.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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(nume ric_limits<stre amsize>::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.goo glegroups.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(nu meric_limits<st reamsize>::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
10951
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 std::getline(input,buf); GCC 3.2 leaves a trailing '\r' in buf. Is this standard conformant? My impression was that getline
4
5736
by: sam | last post by:
Hi, How can I use getline ignore the commandline arguments? Thanks Sam
2
3450
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 char* cstr; std::cout << "House/Appartment # "; std::cin.getline(cstr, CHAR_MAX); //<<==seg fault
2
1969
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 word of unicode file is "0xfffe" ,if use getline ,it may 0x00ff 0x00fe in my buffer do you have any idea,except use c lib like fgetws
2
6006
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 = fopen(filename, "r")) == NULL)
7
2445
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* before it continues past the getline. I tried changing the getline to std::getline(std::cin, randomStuff, '\n');
1
4044
by: Kevin Eller | last post by:
I haven't used std::getline for a long time, if someone can help me
8
3533
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 debug , and 8 sec while optimization on. It is for Visual Studio 7.1 and its std library. While vim opens it in a fraction of sec. So, is it that getline is reading the file line by line, instead reading a chunk at a time in its internal buffer?...
6
6755
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 goes all wrong. Is there some known differences with file handling on OS X? My code is quite long, but one of the defect parts looks like this (sorry about the indentation - I'm new to posting code on a newsgroup):
0
8802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8579
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7297
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2699
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1909
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1587
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.