Stroustrup chapter 3 - 3.6

A

arnuld

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
 
L

Larry Smith

arnuld said:
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
 
A

arnuld

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
 
L

Larry Smith

arnuld said:
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.
 
A

arnuld

Larry said:
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
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top