473,408 Members | 2,832 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,408 software developers and data experts.

Inputting integers with commas

How to do? Such a simple #@%^&@ thing and I'm spending hours on it.

For example,

long mynum;

istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;

yields "380".

I've tried setting the locale of mystr,

mystr.imbue(locale("en_US"));

to various things but no joy.

Yes, I know I could iterate through and .erase the commas, but I
was hoping there was a more elegant and/or built-in solution.
--
% Randy Yates % "Bird, on the wing,
%% Fuquay-Varina, NC % goes floating by
%%% 919-577-9882 % but there's a teardrop in his eye..."
%%%% <ya***@ieee.org> % 'One Summer Dream', *Face The Music*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #1
10 2233
Randy Yates schrieb:
How to do? Such a simple #@%^&@ thing and I'm spending hours on it.

For example,

long mynum;

istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;

yields "380".


And correctly so, you're only reading the first number ;-)
Apart from that you could just read the comma into a char:
char c;
long mynum1, mynum2;
mystr >> mynum1 >> c >> mynum2;

HTH,
Malte
Jul 22 '05 #2
Malte Starostik schrieb:
Randy Yates schrieb:
How to do? Such a simple #@%^&@ thing and I'm spending hours on it.
For example,

long mynum;

istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;

yields "380".

And correctly so, you're only reading the first number ;-)


Sorry, I gather that comma is not supposed to separate individual
numbers but a thousand separator? (not used to that, in my de_DE locale
the thousand separator is a . while a decimal point is a ,)
In that case maybe something like this:

long mynum;
istringstream mystr("380,900", istringstream::in);
for ( mynum = 0; mystr; )
{
long tmp;
if (mystr >> tmp) mynum += tmp;
char c;
if (mystr >> c && c == ',' ) mynum *= 1000;
}
cout << mynum << endl;

The error checking is quite suboptimal though and it reads things like
380,-900 as 379100, 380,1,0 as 380001000 etc.

Regards,
Malte
Jul 22 '05 #3
Malte Starostik <ma*************@t-online.de> writes:
Randy Yates schrieb:
How to do? Such a simple #@%^&@ thing and I'm spending hours on
it. For example,
long mynum;
istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;
yields "380".
And correctly so, you're only reading the first number


I see. So when your boss emails you and asks

"Hey Malte, please order 1,173 T-Shirts for the company picnic",

you say,

"Hey boss, which is it, 1 or 173?"

;(
;-) Apart from that you could just read the comma into a char:
char c;
long mynum1, mynum2;
mystr >> mynum1 >> c >> mynum2;
As I said, I know there are ways AROUND it - I was looking for the
elegant/proper way.
HTH,
Not really.
Malte


--
% Randy Yates % "Maybe one day I'll feel her cold embrace,
%% Fuquay-Varina, NC % and kiss her interface,
%%% 919-577-9882 % til then, I'll leave her alone."
%%%% <ya***@ieee.org> % 'Yours Truly, 2095', *Time*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #4
Randy Yates wrote:
How to do? Such a simple #@%^&@ thing and I'm spending hours on it.

For example,

long mynum;

istringstream mystr("380,900", istringstream::in);
I suppose that 380,900 represents 380900?
mystr >> mynum;
cout << mynum;

yields "380".

I've tried setting the locale of mystr,

mystr.imbue(locale("en_US"));

to various things but no joy.

Yes, I know I could iterate through and .erase the commas, but I
was hoping there was a more elegant and/or built-in solution.


Nope.
Jul 22 '05 #5
Malte Starostik <ma*************@t-online.de> writes:
Malte Starostik schrieb:
Randy Yates schrieb:
How to do? Such a simple #@%^&@ thing and I'm spending hours on it.
For example,

long mynum;

istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;

yields "380".

And correctly so, you're only reading the first number ;-)


Sorry, I gather that comma is not supposed to separate individual
numbers but a thousand separator? (not used to that, in my de_DE
locale the thousand separator is a . while a decimal point is a ,)
In that case maybe something like this:

long mynum;
istringstream mystr("380,900", istringstream::in);
for ( mynum = 0; mystr; )
{
long tmp;
if (mystr >> tmp) mynum += tmp;
char c;
if (mystr >> c && c == ',' ) mynum *= 1000;
}
cout << mynum << endl;

The error checking is quite suboptimal though and it reads things like
380,-900 as 379100, 380,1,0 as 380001000 etc.


Thanks, that helps (if there is no "right" way to do it).
--
% Randy Yates % "I met someone who looks alot like you,
%% Fuquay-Varina, NC % she does the things you do,
%%% 919-577-9882 % but she is an IBM."
%%%% <ya***@ieee.org> % 'Yours Truly, 2095', *Time*, ELO
http://home.earthlink.net/~yatescr
Jul 22 '05 #6
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> writes:
Randy Yates wrote:
How to do? Such a simple #@%^&@ thing and I'm spending hours on
it. For example,
long mynum;
istringstream mystr("380,900", istringstream::in);


I suppose that 380,900 represents 380900?
mystr >> mynum;
cout << mynum;
yields "380". I've tried setting the locale of mystr,
mystr.imbue(locale("en_US"));
to various things but no joy.
Yes, I know I could iterate through and .erase the commas, but I
was hoping there was a more elegant and/or built-in solution.


Nope.


I can understand that. Converting human-readable numeric strings to
integers is not that common of a task.
--
% Randy Yates % "Ticket to the moon, flight leaves here today
%% Fuquay-Varina, NC % from Satellite 2"
%%% 919-577-9882 % 'Ticket To The Moon'
%%%% <ya***@ieee.org> % *Time*, Electric Light Orchestra
http://home.earthlink.net/~yatescr
Jul 22 '05 #7
"Randy Yates" <ya***@ieee.org> wrote in message
news:wt**********@ieee.org...
How to do? Such a simple #@%^&@ thing and I'm spending hours on it.

For example,

long mynum;

istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;

yields "380".


Having done some reading I think num_get is what you need but at the moment
I am struggling to make it work.

Probably no help whatsoever

Adrian
Jul 22 '05 #8
"Randy Yates" <ya***@ieee.org> wrote in message
news:wt**********@ieee.org...
istringstream mystr("380,900", istringstream::in);
mystr >> mynum;
cout << mynum;

yields "380".


Haven't done this myself. But I think one uses the numpunct facet. My
approach would be:
[ ] create a class standardnumpunct derived from numpunct<char> and override
the appropriate member functions -- at least do_grouping to return
std::string(1,3), which is the string of one char(0x03). I think you need
just this one function.
class standardnumpunct : public std::numpunct<char> {
protected:
string_type do_grouping {
return std::string(1,3);
}
};

[ ] create a new locale as follows
std::locale groupinglocale(std::locale(), new standardnumpunct);

[ ] imbue your istringstream mystr with this locale. there is no need to
delete the new standardnumpunct, as the destructor of groupinglocale will do
this for us.
mystr.imbue(groupinglocale);
My post "Re: convert hex to oct" on April 02, 2004 might be helpful. Let us
know if you get it to work.
Jul 22 '05 #9
Malte Starostik wrote:

Sorry, I gather that comma is not supposed to separate individual
numbers but a thousand separator? (not used to that, in my de_DE locale
the thousand separator is a . while a decimal point is a ,)
In that case maybe something like this:

long mynum;
istringstream mystr("380,900", istringstream::in);
for ( mynum = 0; mystr; )
{
long tmp;
if (mystr >> tmp) mynum += tmp;
char c;
if (mystr >> c && c == ',' ) mynum *= 1000;
}
cout << mynum << endl;

The error checking is quite suboptimal though and it reads things like
380,-900 as 379100, 380,1,0 as 380001000 etc.


I would do:

Take the input string, remove all ',' characters:

#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;

bool IsComma( char c )
{
return c == ',';
}

int main()
{
string Input = "380,900";
remove_if( Input.begin(), Input.end(), IsComma );
istringstream mystr(Input);

int mynum;

mystr >> mynum;
cout << mynum;
}

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10
Karl Heinz Buchegger schrieb:
I would do:

Take the input string, remove all ',' characters:

bool IsComma( char c )
{
return c == ',';
}

string Input = "380,900";
remove_if( Input.begin(), Input.end(), IsComma );


Very nice, except this should be:
Input.erase( remove_if( Input.begin(), Input.end(), IsComma ),
Input.end() );

As remove_if doesn't actually remove anything.

Regards,
Malte
Jul 22 '05 #11

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

Similar topics

9
by: Roy Riddex | last post by:
When asking a user to input a number it's easy to make sure it's higher, lower etc then a specified amount. What I'm struggling with is if you ask the user for a number and they enter a letter. How...
22
by: ineedyourluvin1 | last post by:
Hello all! I've been looking for a way to strip characters from strings such as a comma. This would be great for using a comma as a delimiter. I show you what I have right now. ...
7
by: AES | last post by:
Encountered a URL containing a comma the other day -- the first time I've ever noticed that, so far as I can recall. It worked fine, however, and I gather commas are legal in URLs. Out of...
13
by: Nicholas | last post by:
How can I compare char* with integers and characters contained in the str, where integers can be one digit or more? void Access(char *str) { char *pt = str; while (pt != '0') { if...
27
by: Peter Ammon | last post by:
My code obfuscator gave me this: char buff; to which gcc retorted: "ISO C90 forbids variable-size array 'buff'" and checking the standard, it appears that commas are indeed forbidden...
3
by: Robert Scheer | last post by:
Hi. I have a regularexpression validator control on a page. This regular expression validates a textbox to accept only numbers and commas: validationexpression="*" I am trying to modify this...
4
by: striker | last post by:
I have a comma delimited text file that has multiple instances of multiple commas. Each file will contain approximatley 300 lines. For example: one, two, three,,,,four,five,,,,six one, two,...
4
by: andreas.fabri | last post by:
I have a problem reading integers separated by commas with VC8 This program: ___________________________ // read.C #include <iostream> int main()
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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,...
0
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...
0
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
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,...

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.