[n00b] Variable with space is split

Discussion in 'C++' started by Charles, Jan 2, 2007.

  1. Charles

    Charles Guest

    This is very strange. In the following program, If I type a one-word
    name, it works fine. Now, if I type a two-word name, the program splits
    the variable into two:

    #include <iostream>
    #include <string>
    int main()
    {
    std::cout << "What is your name? ";
    std::string name;
    std::cin >> name;
    std::cout << "Hello, " << name
    << std::endl << "And what is yours? ";
    std::cin >> name;
    std::cout << "Hello, " << name
    << "; nice to meet you too!" << std::endl;
    return 0;
    }

    Do you know why?
    Thanks.
     
    Charles, Jan 2, 2007
    #1
    1. Advertising

  2. Charles

    John Carson Guest

    "Charles" <> wrote in message
    news:
    > This is very strange. In the following program, If I type a one-word
    > name, it works fine. Now, if I type a two-word name, the program
    > splits the variable into two:
    >
    > #include <iostream>
    > #include <string>
    > int main()
    > {
    > std::cout << "What is your name? ";
    > std::string name;
    > std::cin >> name;
    > std::cout << "Hello, " << name
    > << std::endl << "And what is yours? ";
    > std::cin >> name;
    > std::cout << "Hello, " << name
    > << "; nice to meet you too!" << std::endl;
    > return 0;
    > }
    >
    > Do you know why?
    > Thanks.



    See the description of the >> operator in details on p.15.

    --
    John Carson
     
    John Carson, Jan 2, 2007
    #2
    1. Advertising

  3. Charles

    Guest

    Charles wrote:
    > This is very strange. In the following program, If I type a one-word
    > name, it works fine. Now, if I type a two-word name, the program splits
    > the variable into two:
    >
    > #include <iostream>
    > #include <string>
    > int main()
    > {
    > std::cout << "What is your name? ";
    > std::string name;
    > std::cin >> name;
    > std::cout << "Hello, " << name
    > << std::endl << "And what is yours? ";
    > std::cin >> name;
    > std::cout << "Hello, " << name
    > << "; nice to meet you too!" << std::endl;
    > return 0;
    > }
    >
    > Do you know why?
    > Thanks.


    When you read a string from cin via operator>>, the delimiter is
    whitespace. Each operator>> will
    stop after a whitespace, while the next operator>> will ignore
    whitespace and continue parsing the
    input.

    so, if I type;

    tolga ceylan<return>

    then the first operator>> gets "tolga"

    and stops at whitespace. The next operator>> will skip whitespace and
    get "ceylan" and stop at <return>

    Hope this helps,

    Tolga Ceylan
     
    , Jan 2, 2007
    #3
  4. wrote:
    > Charles wrote:
    > > This is very strange. In the following program, If I type a one-word
    > > name, it works fine. Now, if I type a two-word name, the program splits
    > > the variable into two:
    > >
    > > #include <iostream>
    > > #include <string>
    > > int main()
    > > {
    > > std::cout << "What is your name? ";
    > > std::string name;
    > > std::cin >> name;
    > > std::cout << "Hello, " << name
    > > << std::endl << "And what is yours? ";
    > > std::cin >> name;
    > > std::cout << "Hello, " << name
    > > << "; nice to meet you too!" << std::endl;
    > > return 0;
    > > }
    > >
    > > Do you know why?
    > > Thanks.

    >
    > When you read a string from cin via operator>>, the delimiter is
    > whitespace. Each operator>> will
    > stop after a whitespace, while the next operator>> will ignore
    > whitespace and continue parsing the
    > input.
    >
    > so, if I type;
    >
    > tolga ceylan<return>
    >
    > then the first operator>> gets "tolga"
    >
    > and stops at whitespace. The next operator>> will skip whitespace and
    > get "ceylan" and stop at <return>
    >
    > Hope this helps,
    >
    > Tolga Ceylan


    What exactly happened and why did she leave Tulsa for Melbourne?
     
    Mathematician, Jan 2, 2007
    #4
  5. Charles

    Charles Guest

    escreveu:
    > When you read a string from cin via operator>>, the delimiter is
    > whitespace. Each operator>> will
    > stop after a whitespace, while the next operator>> will ignore
    > whitespace and continue parsing the
    > input.
    >
    > so, if I type;
    >
    > tolga ceylan<return>
    >
    > then the first operator>> gets "tolga"
    >
    > and stops at whitespace. The next operator>> will skip whitespace and
    > get "ceylan" and stop at <return>
    >
    > Hope this helps,


    Thanks, ah ok, I didn't grasp that...Interesting. Now I know!
     
    Charles, Jan 2, 2007
    #5
  6. Charles

    Deniel Rose' Guest

    Mathematician wrote:

    > What exactly happened and why did she leave Tulsa for Melbourne?


    This is a C++ book not a place for human relationship questions. It's
    private you should stop!


    ---*---*---*----*--------*********---------------***************

    Dear ladies and gentlemen, please forgive mathematician for his curios
    question, really sorry about that..

    But if you know the reasons, I think others might also want to hear a
    litle
     
    Deniel Rose', Jan 2, 2007
    #6
  7. Charles

    Zero Lee Guest

    wrote:
    > Charles wrote:
    > > This is very strange. In the following program, If I type a one-word
    > > name, it works fine. Now, if I type a two-word name, the program splits
    > > the variable into two:
    > >
    > > #include <iostream>
    > > #include <string>
    > > int main()
    > > {
    > > std::cout << "What is your name? ";
    > > std::string name;
    > > std::cin >> name;
    > > std::cout << "Hello, " << name
    > > << std::endl << "And what is yours? ";
    > > std::cin >> name;
    > > std::cout << "Hello, " << name
    > > << "; nice to meet you too!" << std::endl;
    > > return 0;
    > > }
    > >
    > > Do you know why?
    > > Thanks.

    >
    > When you read a string from cin via operator>>, the delimiter is
    > whitespace. Each operator>> will
    > stop after a whitespace, while the next operator>> will ignore
    > whitespace and continue parsing the
    > input.
    >
    > so, if I type;
    >
    > tolga ceylan<return>
    >
    > then the first operator>> gets "tolga"
    >
    > and stops at whitespace. The next operator>> will skip whitespace and
    > get "ceylan" and stop at <return>
    >
    > Hope this helps,
    >
    > Tolga Ceylan


    Default delimiter is Whitespace!
     
    Zero Lee, Jan 2, 2007
    #7
    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. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,936
    Bryan Bullard
    Jul 11, 2003
  2. Christian Seberino
    Replies:
    21
    Views:
    1,718
    Stephen Horne
    Oct 27, 2003
  3. Ian Bicking
    Replies:
    2
    Views:
    1,053
    Steve Lamb
    Oct 23, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    750
    Michael Hudson
    Oct 24, 2003
  5. HappyHippy
    Replies:
    6
    Views:
    770
    Bruno Desthuilliers
    Feb 13, 2006
Loading...

Share This Page