473,492 Members | 4,279 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

[n00b] Variable with space is split

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
6 2266
"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

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

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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
1542
by: Slonocode | last post by:
Hello I am trying to display a USA address properly in a multiline textbox. Unfortunately the address I must process is contained in a string variable and the format is not uniform. Examples: ...
16
8220
by: simonc | last post by:
One of the things that drives me mad about vb.net is spending time trying to make a program do something then finding that there's already an inbuild procedure that does exactly the same thing. ...
6
1493
by: HappyHippy | last post by:
More of a minor niggle than anything but how would I remove the aforementioned space? eg. strName = 'World' print 'Hello', strName, ', how are you today?' comes out as "Hello World , how are...
15
16277
by: Daren | last post by:
Hi, I need to be able to split large string variables into an array of lines, each line can be no longer than 70 chars. The string variables are text, so I would additionally like the lines...
5
1477
by: moostafa | last post by:
Hi, I'm writing a program that performs arithmetic operations on integers. I want to be able to type in a bunch of integers seperated by any amount of white space then terminate input with a...
28
8151
by: Sriram Rajagopalan | last post by:
Hi, I was interested to know if there is a way to use a variable name split across multiple lines in C. For example : int this_is_a_very_long_variable_name = 10; I would like to split...
24
4800
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
3
2161
by: skyy | last post by:
Hi.. i have a string that i would want split on a single blank space only. eg. $string = File: abc.txt using split with white space i can get the name of the file : abc.txt However i run...
7
23273
by: ganesh gajre | last post by:
Hi all, I want to read file which is mapping file. Used in to map character from ttf to unicode. eg Map file contain data in the following way: 0 ० 1 १
0
6980
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6862
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7364
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5452
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4886
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3087
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
282
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.