If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
-
2003-02-08
The C++ purists will hate this, but I am an embedded systems programmer, and usually avoid the C++ standard library. That is no reason for you to do so if there is a better C++ approach. but here goes:
Where instring is a C style string of comma separated numeric integer values:
Note that strtok() modifies instring by inserting null characters - the token are not copied, if it is necessary to keep the original string intact, copy it first. It ignores whitespace. so the tokens for the string "123, 55, 66" are "123", "55", and "66". These are converted in this example using atoi(). The separator string parameter od strtok() can contain multiple characters.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Just a note, don't mix c headers with c++. The headers as stdlib.h in c, in c++ use cstdlib the C++ version of this header.. Eventhough the understanding is c++ is just a superset of c, the differences in the headers are important .
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I am still finishing touches on that program I was working on before, when Wayne helped me with the math, but I cant figure out how you remove the unwanted charachter ',' from the integer. It seems like cin.ignore(','); would work, but it doesnt, I get errors.Please help. By the way, the program is C++
--nilson
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
it reads and discards characters until either num characters have been ignored, or the delim character is reached.
Doesn't look like it does what you want, I would input the number as a sting, remove the commas, and convert it back to a number.
blackadder
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
How can you eliminate commas from inputed numbers? I've tried cin.ignore(','); But that doesnt work.
ex.
-----------------------------------------------
cout<<"Input for 'a'\n";
cin.ignore(',');
cin>>a;
-------------------------------------------------
--nilson
Perhaps input the whole string and write your own code to parse it separately. This probably allows more robust error checking.
You could use strtok() to do this. I know it is from the C library, but I don't get too religious about that.
Clifford
How would you use strtok()?
--nilson
The C++ purists will hate this, but I am an embedded systems programmer, and usually avoid the C++ standard library. That is no reason for you to do so if there is a better C++ approach. but here goes:
Where instring is a C style string of comma separated numeric integer values:
char* token ;
int i ;
int valarray[MAX_VAL_COUNT]
token = token strtok( instring, "," ) ;
for( i = 0; i < MAX_VAL_COUNT && token != 0; i++ )
{
valarray[i] = atoi( token ) ;
token = token strtok( 0, "," ) ;
}
Note that strtok() modifies instring by inserting null characters - the token are not copied, if it is necessary to keep the original string intact, copy it first. It ignores whitespace. so the tokens for the string "123, 55, 66" are "123", "55", and "66". These are converted in this example using atoi(). The separator string parameter od strtok() can contain multiple characters.
Just a note, don't mix c headers with c++. The headers as stdlib.h in c, in c++ use cstdlib the C++ version of this header.. Eventhough the understanding is c++ is just a superset of c, the differences in the headers are important .
I am still finishing touches on that program I was working on before, when Wayne helped me with the math, but I cant figure out how you remove the unwanted charachter ',' from the integer. It seems like cin.ignore(','); would work, but it doesnt, I get errors.Please help. By the way, the program is C++
--nilson
ignore() takes 2 parameters, here's the prototype for it:
istream &ignore(streamsize num=1, int_typ delim=EOF);
it reads and discards characters until either num characters have been ignored, or the delim character is reached.
Doesn't look like it does what you want, I would input the number as a sting, remove the commas, and convert it back to a number.
blackadder