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

[n00b] Variable with space is split

P: n/a
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.

Jan 2 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Charles" <la********@gmail.comwrote in message
news:11**********************@v33g2000cwv.googlegr oups.com
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

Jan 2 '07 #2

P: n/a

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 '07 #3

P: n/a

to***********@yahoo.com 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?

Jan 2 '07 #4

P: n/a
to***********@yahoo.com 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!

Jan 2 '07 #5

P: n/a

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

Jan 2 '07 #6

P: n/a

to***********@yahoo.com 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!

Jan 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.