Stroustrup chapter 3 - 3.6

Discussion in 'C++' started by arnuld, Nov 5, 2006.

  1. arnuld

    arnuld Guest

    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
     
    arnuld, Nov 5, 2006
    #1
    1. Advertising

  2. arnuld

    Larry Smith Guest

    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
    >
     
    Larry Smith, Nov 5, 2006
    #2
    1. Advertising

  3. arnuld

    arnuld Guest

    > 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
     
    arnuld, Nov 5, 2006
    #3
  4. arnuld

    Larry Smith Guest

    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.
     
    Larry Smith, Nov 5, 2006
    #4
  5. arnuld

    arnuld Guest

    > 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
     
    arnuld, Nov 5, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. arnuld
    Replies:
    14
    Views:
    1,055
    Alf P. Steinbach
    Nov 11, 2006
  2. arnuld

    Stroustrup: chapter 4

    arnuld, Mar 4, 2007, in forum: C++
    Replies:
    13
    Views:
    683
    benben
    Mar 5, 2007
  3. arnuld
    Replies:
    0
    Views:
    446
    arnuld
    Mar 5, 2007
  4. arnuld
    Replies:
    0
    Views:
    530
    arnuld
    Mar 5, 2007
  5. arnuld
    Replies:
    4
    Views:
    555
    mlimber
    Mar 5, 2007
Loading...

Share This Page