473,383 Members | 1,788 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,383 software developers and data experts.

is it string's getline or just me?

Hi,

I've a simplified program snippet in which getline behaves in a way, which
at least I find odd:

// File: getline_example.cpp
#include <iostream>
#include <string>
using namespace std;

int main()
{
int one;
if (cin >> one)
{
cout << "one: " << one << endl;

string two;
if (getline(cin,two))
{
cout << "two: " << two << endl;

string three;
if (getline(cin,three))
{
cout << "three: " << three << endl;
}
else
{
cout << "third error." << endl;
}
}
else
{
cout << "second error." << endl;
}
}
else
{
cout << "first error." << endl;
}
}

Using g++ 3.4.2 with the following input results in the following:
$ g++ getline_example.cpp
$ ./a.exe
42
one: 42
two:

I.e. I start program and it is waiting for input.
I enter 42 and hits return.
As expected the program outputs what I just inputed.
However, the program outputs "two:" as well!?!?!?

This is very odd to me since I have only entered one data input (42). I
was expecting that the program was awaiting a second set of indata but no
- it just goes on and instead it is waiting for a third set of data!?!?

I have no idea what or why the statement getline(cin,two) did! But I do
know that it did not wait for my second set of indata :-(

Is it me or is it g++ who gets this wrong. (I do not think that g++ should
get this trivial case wrong, so...)

Thanks in advance!

PS. Do not advise me to add advanced error-checking and resetting a state
flag or the like because that should really not be nescessarry. (State is
checked in the if statements.)
Jul 22 '05 #1
3 1575
algorithm wrote:
This is very odd to me since I have only entered one data input (42). I
was expecting that the program was awaiting a second set of indata but no
- it just goes on and instead it is waiting for a third set of data!?!?
I have no idea what or why the statement getline(cin,two) did! But I do
know that it did not wait for my second set of indata :-(


getline reads to the next end of line. If you have an ureaded end of line in
the stream (and you have it, because the operator >> stops reading after
the last digit) the result will be an empty string.

If you expect data separated in lines, will be better to always read with
getline, and decode the content of each line separately, using a
istringstream, for example.

--
Salu2
Jul 22 '05 #2
Julián Albo wrote:
algorithm wrote:

This is very odd to me since I have only entered one data input (42). I
was expecting that the program was awaiting a second set of indata but no
- it just goes on and instead it is waiting for a third set of data!?!?
I have no idea what or why the statement getline(cin,two) did! But I do
know that it did not wait for my second set of indata :-(

getline reads to the next end of line. If you have an ureaded end of line in
the stream (and you have it, because the operator >> stops reading after
the last digit) the result will be an empty string.

If you expect data separated in lines, will be better to always read with
getline, and decode the content of each line separately, using a
istringstream, for example.


More specifically, after you do cin >> one, and have read the number 42,
there is still a newline pending on cin, so that cin >> two will read to
the end of line (the very next character), and set the variable "two" to
an empty string (since there was no other data before the newline).
Jul 22 '05 #3
> after you do cin >> one, and have read the number 42,
there is still a newline pending on cin, so that
cin >> two will read to the end of line


ah - i didn't think of that...
thanks!
Jul 22 '05 #4

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

Similar topics

8
by: Developwebsites | last post by:
I want to enter sentences with blanks between words, names, cities, new york, pam anderson, etc. as in: Input"enter your name";name$ print"your name is: ";name$ C++ seems to make it rather...
6
by: Allan Bruce | last post by:
I have a string like: "FL:1234ABCD:3:FileName With Spaces.txt\n" and I want to read the values separated by ':' into variables. I tried to use sscanf like this: sscanf("FL:%s:%d:%s\n",...
2
by: Roberto Dias | last post by:
Hi all, What to do for searching for more than one string occurrence in the same string (this last are line of a text). I have used getline(), to get the text lines by means of WHILE loop and...
18
by: JKop | last post by:
Can some-one please point me to a nice site that gives an exhaustive list of all the memberfunctions, membervariables, operators, etc. of the std::string class, along with an informative...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
33
by: Jordan Tiona | last post by:
How can I make one of these? I'm trying to get my program to store a string into a variable, but it only stores one line. -- "No eye has seen, no ear has heard, no mind can conceive what God...
14
by: cpisz | last post by:
I want to do some find and erase operations that i find in the string class, upon the entire text contents of a file. I made a function that will take a string (designed to hold the entire file...
2
by: John Brawley | last post by:
Please what is the functional C++ difference between a string of characters typed manually and included inside the main() function of a program... vis: string str = "123,4.56,7.8"; , ....and...
3
by: James Kanze | last post by:
On Sep 12, 7:25 am, Ruben <ru...@www2.mrbrklyn.comwrote: You can specify a delimitor when calling getline, but I'd tend to consider that a bit abuse. I'd just parse the string directly,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.