[n00b] Variable with space is split

C

Charles

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.
 
J

John Carson

Charles said:
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.
 
T

tolgaceylanus

Charles said:
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
 
M

Mathematician

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?
 
C

Charles

(e-mail address removed) 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!
 
D

Deniel Rose'

Mathematician said:
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
 
Z

Zero Lee

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!
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top