By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Stroustrup chapter 3 - 3.6

P: n/a
i have this code. it works but its behaviour is strange:
------------------------------------------------------------
#include <iostream>
#include <string>

int main() {
std::string s1;
std::string s2;

std::cout << "Please enter your name: ";
std::cin >s1;
std::cout << "hello " << s1 << "\n";

std::cout << "now using GETLINE(): ";

getline(std::cin, s2);
std::cout << "hi babe " << s2 << " ;-) \n";
}

OUTPUT (with one word as input):

[arnuld@localhost cpp]$ ./a.out

Please enter your name: arnuld
hello arnuld
now using GETLINE(): hi babe ;-)

OUTPUT (with 2 words as input):

Please enter your name: arnuld fraser
hello arnuld
now using GETLINE(): hi babe fraser ;-)
[arnuld@localhost cpp]$

-----------------------------------------------------------------
i expected:

1.) if i enter 1 word then it will print it /hello word/ & it will ask
me for another when it will hit /getline/ but it doesn't.

2.) with 2 words i just expect /hello 1st word/ & then after hitting
/getline/ it will ask for another input.

but behaviour is not like this. can somebody explain?

i am using "g++ version 4.1.1" on "BLAG Linux 50002".

thanks
-- arnuld
http://arnuld.blogspot.com

Nov 5 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
arnuld wrote:
i have this code. it works but its behaviour is strange:
------------------------------------------------------------
#include <iostream>
#include <string>

int main() {
std::string s1;
std::string s2;

std::cout << "Please enter your name: ";
std::cin >s1;

The above statement reads the FIRST word from 'cin'.
If more than one word is pending on 'cin', the the call
to getline() below will read the remaining word(s).
If 'arnuld fraser' is entered in reply to the '...name:'
prompt above, then 's1' will contain 'arnuld' and
'fraser' will still be pending on 'cin' - and will
be read by getline() below.

std::cout << "hello " << s1 << "\n";

std::cout << "now using GETLINE(): ";

getline(std::cin, s2);
std::cout << "hi babe " << s2 << " ;-) \n";
}

OUTPUT (with one word as input):

[arnuld@localhost cpp]$ ./a.out

Please enter your name: arnuld
hello arnuld
now using GETLINE(): hi babe ;-)

OUTPUT (with 2 words as input):

Please enter your name: arnuld fraser
hello arnuld
now using GETLINE(): hi babe fraser ;-)
[arnuld@localhost cpp]$

-----------------------------------------------------------------
i expected:

1.) if i enter 1 word then it will print it /hello word/ & it will ask
me for another when it will hit /getline/ but it doesn't.

2.) with 2 words i just expect /hello 1st word/ & then after hitting
/getline/ it will ask for another input.

but behaviour is not like this. can somebody explain?

i am using "g++ version 4.1.1" on "BLAG Linux 50002".

thanks
-- arnuld
http://arnuld.blogspot.com
Nov 5 '06 #2

P: n/a
The above statement reads the FIRST word from 'cin'.
If more than one word is pending on 'cin', the the call
to getline() below will read the remaining word(s).
If 'arnuld fraser' is entered in reply to the '...name:'
prompt above, then 's1' will contain 'arnuld' and
'fraser' will still be pending on 'cin' - and will
be read by getline() below.
ok, i got it but why it does not call /getline/ when i input one word

-- arnuld
http://arnuld.blogspot.com

Nov 5 '06 #3

P: n/a
arnuld wrote:
>The above statement reads the FIRST word from 'cin'.
If more than one word is pending on 'cin', the the call
to getline() below will read the remaining word(s).
If 'arnuld fraser' is entered in reply to the '...name:'
prompt above, then 's1' will contain 'arnuld' and
'fraser' will still be pending on 'cin' - and will
be read by getline() below.

ok, i got it but why it does not call /getline/ when i input one word

-- arnuld
http://arnuld.blogspot.com
It does call getline() when only one word was entered in
reply to the '...name:' prompt, but all that is left for
getline() to read is the newline that followed the single
word (i.e. getline() reads an empty line). For example,
if you entered 'arnuld', then the input buffer for 'cin'
contains "arnuld\n". the statement:

std::cin >s1;

will read "arnuld" from the input and place it into 's1'.
Now the input buffer for 'cin' contains only "\n".
getline() reads up thru the next newline ("\n"), so
getline() reads the "\n" from the 'cin' input buffer as
a blank line.
Nov 5 '06 #4

P: n/a
Larry Smith wrote:
It does call getline() when only one word was entered in
reply to the '...name:' prompt, but all that is left for
getline() to read is the newline that followed the single
word (i.e. getline() reads an empty line). For example,
if you entered 'arnuld', then the input buffer for 'cin'
contains "arnuld\n". the statement:

std::cin >s1;

will read "arnuld" from the input and place it into 's1'.
Now the input buffer for 'cin' contains only "\n".
getline() reads up thru the next newline ("\n"), so
getline() reads the "\n" from the 'cin' input buffer as
a blank line.
i did not get that when i read it first. then i tried what you told me
on the Emacs /eshell/ with one more /getline/ & i saw it asking for
input. that way i got it "larry\n" gives us

1.) larry
2.) \n

& hence 3rd /getline/ does not find anything & it asks for input.

hey thanks Larry. now i understood it.

-- arnuld
http://arnuld.blogspot.com

Nov 5 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.