Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Stroustrup chapter 3 - 3.6

Reply
Thread Tools

Stroustrup chapter 3 - 3.6

 
 
arnuld
Guest
Posts: n/a
 
      11-05-2006
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

 
Reply With Quote
 
 
 
 
Larry Smith
Guest
Posts: n/a
 
      11-05-2006
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
>

 
Reply With Quote
 
 
 
 
arnuld
Guest
Posts: n/a
 
      11-05-2006
> 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

 
Reply With Quote
 
Larry Smith
Guest
Posts: n/a
 
      11-05-2006
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.
 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      11-05-2006
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stroustrup Chapter 4, Exercise 5 arnuld C++ 4 03-05-2007 04:05 PM
Stroustrup Chapter 4, Exercise 3 - displaying the sizes of char, int etc. arnuld C++ 0 03-05-2007 03:18 PM
Stroustrup Chapter 4, Exercise 2 arnuld C++ 0 03-05-2007 02:34 PM
Stroustrup: chapter 4 arnuld C++ 13 03-05-2007 07:41 AM
Stroustrup "desk calculator" chapter 6 arnuld C++ 14 11-11-2006 04:14 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57