473,386 Members | 1,775 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

spaces & special characters in C++

Whenever I enter a line with spaces in a cin >> .. line my program seems
to skip all cin lines after that point. Could someone please explain me
why this is? I'm calling this procedure from main, to enter certain data
about a product:

Product* getData()
{
// Create new item
Product* pProd = new Product;

// Read product name
cout << "\nProduct Name:";
cin >> pProd->szProductName;

// if 'exit' entered, return 0 to display results and exit program
if ((stricmp(pProd->szProductName,"exit") == 0))
{
// ... het lege object verwijderen...
delete pProd;

// ... en een nul retourneren om de invoer te beëindigen.
return 0;
}

// Read price
cout << "Product Price:";
cin >> pProd->nPrice;

cout << "Expecting Usetime in years";
cin >> pProd->nEUY;
pProd->pNext = 0;

// Return new Product
return pProd;
}
Whenever I enter a space in the product name, I seem to get the cout
lines over and over again. I'm calling this procedure from main until a
0 is returned meaning that no more entries are wanted.
Jun 5 '06 #1
7 3124
On 2006-06-05 10:03, Niels wrote:
Whenever I enter a line with spaces in a cin >> .. line my program seems
to skip all cin lines after that point. Could someone please explain me
why this is? I'm calling this procedure from main, to enter certain data
about a product:

Product* getData()
{
// Create new item
Product* pProd = new Product;

// Read product name
cout << "\nProduct Name:";
cin >> pProd->szProductName;

// if 'exit' entered, return 0 to display results and exit program
if ((stricmp(pProd->szProductName,"exit") == 0))
{
// ... het lege object verwijderen...
delete pProd;

// ... en een nul retourneren om de invoer te beëindigen.
return 0;
}

// Read price
cout << "Product Price:";
cin >> pProd->nPrice;

cout << "Expecting Usetime in years";
cin >> pProd->nEUY;
pProd->pNext = 0;

// Return new Product
return pProd;
}
Whenever I enter a space in the product name, I seem to get the cout
lines over and over again. I'm calling this procedure from main until a
0 is returned meaning that no more entries are wanted.


By default input with cin is terminated by any whitespace (don't know if
it's possible to change this behaviour). You can use getline() to read a
whole line.

std::string prodName;
std::getline(std::cin, prodName);
std::cout << prodName;

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
Jun 5 '06 #2


Erik Wikström schreef:
On 2006-06-05 10:03, Niels wrote:
Whenever I enter a line with spaces in a cin >> .. line my program
seems to skip all cin lines after that point. Could someone please
explain me why this is? I'm calling this procedure from main, to enter
certain data about a product:

Product* getData()
{
// Create new item
Product* pProd = new Product;

// Read product name
cout << "\nProduct Name:";
cin >> pProd->szProductName;

// if 'exit' entered, return 0 to display results and exit program
if ((stricmp(pProd->szProductName,"exit") == 0))
{
// ... het lege object verwijderen...
delete pProd;

// ... en een nul retourneren om de invoer te beëindigen.
return 0;
}

// Read price
cout << "Product Price:";
cin >> pProd->nPrice;

cout << "Expecting Usetime in years";
cin >> pProd->nEUY;
pProd->pNext = 0;

// Return new Product
return pProd;
}
Whenever I enter a space in the product name, I seem to get the cout
lines over and over again. I'm calling this procedure from main until
a 0 is returned meaning that no more entries are wanted.


By default input with cin is terminated by any whitespace (don't know if
it's possible to change this behaviour). You can use getline() to read a
whole line.

std::string prodName;
std::getline(std::cin, prodName);
std::cout << prodName;

Erik Wikström


That worked great! Thank you!

Niels.
Jun 5 '06 #3
Niels wrote:
Whenever I enter a line with spaces in a cin >> .. line my program seems
to skip all cin lines after that point. Could someone please explain me
why this is? I'm calling this procedure from main, to enter certain data
about a product:

Product* getData()
{
// Create new item
Product* pProd = new Product;

// Read product name
cout << "\nProduct Name:";
cin >> pProd->szProductName;
From the stricmp call below, I guess that pProd->szProductname is an array
of char? You should better use std::string. Otherwise, your program has no
way of ensuring that the user doesn't enter more characters than the array
could handle. A buffer overflow (which is the reason for most of the
security holes in computer programs) would be the result.
// if 'exit' entered, return 0 to display results and exit program
if ((stricmp(pProd->szProductName,"exit") == 0))
{
// ... het lege object verwijderen...
delete pProd;

// ... en een nul retourneren om de invoer te beëindigen.
return 0;
}

// Read price
cout << "Product Price:";
cin >> pProd->nPrice;

cout << "Expecting Usetime in years";
cin >> pProd->nEUY;
pProd->pNext = 0;

// Return new Product
return pProd;
}
Whenever I enter a space in the product name, I seem to get the cout
lines over and over again.


Well, the space is the separator. cin stops reading there. Afterwards, you
try to read the price, which i assume is some kind of numerical type. So
cin tries to read the part after the space as number. If that fails, cin
goes to fail state and won't give you any more input until you clear that
state.

Jun 5 '06 #4
In article <C6****************@newsb.telia.net>, Erik-
wi******@telia.com says...

[ ... ]
By default input with cin is terminated by any whitespace (don't know if
it's possible to change this behaviour). You can use getline() to read a
whole line.


It's sort of possible to change this behavior. Input
using string extractor normally terminates with
whitespace, but the definition of "whitespace" depends on
the stream's locale. The locale has a ctype facet that
tells what is whitespace for that locale.

You could define a locale that only says '\r' and '\n'
are whitespace, and tell a stream to use that locale, and
afterwards, using a string extractor from that stream
would read characters up to a '\r' or '\n', but would
include '\t', '\v', ' ', etc.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 9 '06 #5

Jerry Coffin wrote:
In article <C6****************@newsb.telia.net>, Erik-
wi******@telia.com says...

[ ... ]
By default input with cin is terminated by any whitespace (don't know if
it's possible to change this behaviour). You can use getline() to read a
whole line.


It's sort of possible to change this behavior. Input
using string extractor normally terminates with
whitespace, but the definition of "whitespace" depends on
the stream's locale. The locale has a ctype facet that
tells what is whitespace for that locale.

You could define a locale that only says '\r' and '\n'
are whitespace, and tell a stream to use that locale, and
afterwards, using a string extractor from that stream
would read characters up to a '\r' or '\n', but would
include '\t', '\v', ' ', etc.

--
Later,
Jerry.


how exactly would you do that i understand a little what your trying to
say but how would you set up the syntax. the reason im asking is that
im writing a program to take inputs add the proper tags and create an
html page from it!! i tryuseing getline() is it in the cstdlib? im
currently using iostream which has cin.getline() but the syntax is
difrent if someone could clarify this it would be nice

Jun 10 '06 #6
By default input with cin is terminated by any whitespace (don't know if
it's possible to change this behaviour). You can use getline() to read a
whole line.

im
currently using iostream which has cin.getline() but the syntax is
difrent if someone could clarify this it would be nice


I'm almost completely confused by cin, cin.get() and cin.getline - if
anyone knows of a good webpage that covers them very clearly, it'd be
appreciated.

My Prata text just has little bits and pieces strewn about his book :(

Jun 10 '06 #7
In article <1149954973.474742.97210
@u72g2000cwu.googlegroups.com>, Mr*********@gmail.com
says...

[ ... I had said: ]
You could define a locale that only says '\r' and '\n'
are whitespace, and tell a stream to use that locale, and
afterwards, using a string extractor from that stream
would read characters up to a '\r' or '\n', but would
include '\t', '\v', ' ', etc.

[ ... ]
how exactly would you do that i understand a little what your trying to
say but how would you set up the syntax. the reason im asking is that
im writing a program to take inputs add the proper tags and create an
html page from it!! i tryuseing getline() is it in the cstdlib? im
currently using iostream which has cin.getline() but the syntax is
difrent if someone could clarify this it would be nice


http://tinyurl.com/fgu9o

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 11 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jukka K. Korpela | last post by:
The HTML specifications define the entities &zwj;, &zwnj;, &lrm;, &rlm; as denoting zero-width joiner, zero-width non-joiner, left to right mark, and right to left mark. Is there any evidence of...
7
by: Mindy Geac | last post by:
Is it possible to delete special caracters from a string and multiple spaces? When the input is : "a&*bbb cc/c d!d" I want the result : "abbb ccc dd" thnx. MJ <?php
2
by: Thierry Lam | last post by:
Let's say I have the following xml tag: <para role="source">a & b</para> Currently, an xml parser will treat & as a special character. Does anyone know the special characters to use around...
11
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white...
11
by: Lucas Campos | last post by:
Hello, I'm using a dropdownlist web control. This dropdownlist has a problem with some descriptions I'd like to show. The description is "XXX JJJ", but it shows in the browser "XXX JJJ" (it...
1
by: RJN | last post by:
Hi I'm using XMLTextReader to parse the contents of XML. I have issues when the xml content itself has some special characters like & ,> etc. <CompanyName>Johnson & Jhonson</CompanyName>...
14
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only...
7
by: Taras_96 | last post by:
Hi all, I was hoping to get some clarification on a couple of questions I have: 1) When should htmlspecial characters be used? As a general rule should it be used for text that may contain...
2
by: David | last post by:
Sent this to alt.php a couple of days back, but doesn't look like I'll get an answer, so trying here. I'm trying to convert a script to use friendly URLs, I've done this before, but my PHP...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.