473,770 Members | 4,558 Online
Bytes | Software Development & Data Engineering Community
+ 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 2280
"Charles" <la********@gma il.comwrote in message
news:11******** **************@ v33g2000cwv.goo glegroups.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***********@y ahoo.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***********@y ahoo.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...Interest ing. 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***********@y ahoo.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
1575
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: John Smith, 12345 Main Street, Anytown, NY 98776 John Smith, 12345 Main Street, Anytown, NY, 98876 John Smith, c/o Jane Smith, 12345 Main Street, Anytown, NY 98876 John Smith, c/o Jane Smith, 12345 Main Street, Anytown, NY, 98876
16
8257
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. So before I begin on this can anyone tell me if there is already a routine to do this: I am dividing up a delimited textstring using string.split. The delimiter character is a space, but
6
1507
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 you today?" Have googled, and worked my way through the first 7 chapters of Byte of
15
16354
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 to end at the end of a word, if you catch my drift. For example, I have a large string variable containing the text: "I've seen things you people wouldn't believe. Attack ships on fire
5
1490
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 non-integer character. I plan to put my input into an array, and while I have a max size I don't have a min and don't know exactly how many arguments to expect. I would really appreciate any ideas. Cheers.
28
8200
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 the "this_is_a_very_long_variable_name " into 2
24
4853
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 multiple lines. It would appear though that every distinct set of data starts with a 'code' that is always the 25 characters long. The text is variable however. Assuming i've read the contents of the file into the string myfile, how do i split my...
3
2181
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 into problem when there is many spaces in the filename. eg.
7
23308
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
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10237
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.