Need help to solve consecutive comma problem..

R

RaulGonz

char *token = strtok(str,",");
int i = 0;
while(token != NULL)
{
switch(i)

(str = abc,cde,0.5,,hi,0)

in this case, case0 = abc, case1 = cde, case2 = 0.5, case3 = hi and
case4 = 0. however, it is missing the fourth "data" in this case. i
want to display case0 = abc, case1 = cde, case2 = 0.5, case3 = NA,
case4 = hi and case5 = 0.

the problem here is that once the code sees the first comma, it is
preparing to break down the string but however, it faces yet another
comma immediately, so the "data" between the consecutive commas is
ignored. is there any way that i can overcome this by displaying "NA"
whenever two consecutive commas appears because it could be from
element 1 to 6 that is missing or not available in the string.

please advise. thank you!
 
F

Francesco

char *token = strtok(str,",");
                int i = 0;
                while(token != NULL)
                {
                    switch(i)

(str = abc,cde,0.5,,hi,0)

in this case, case0 = abc, case1 = cde, case2 = 0.5, case3 = hi and
case4 = 0. however, it is missing the fourth "data" in this case. i
want to display case0 = abc, case1 = cde, case2 = 0.5, case3 = NA,
case4 = hi and case5 = 0.

the problem here is that once the code sees the first comma, it is
preparing to break down the string but however, it faces yet another
comma immediately, so the "data" between the consecutive commas is
ignored. is there any way that i can overcome this by displaying "NA"
whenever two consecutive commas appears because it could be from
element 1 to 6 that is missing or not available in the string.

please advise. thank you!

Hi Raul,
first of all, it seems to me that you're using the comma operator in a
very weird way. It isn't going to do what you mean at all. You should
enumerate cases with the "case" keyword, not with commas. You should
also be using std::string instead of char*.

Have a look here:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8
and come back with something that we can copy, paste and compile, or
at least something that comes close to that, then we will gladly help
you refine or fix your code.

Best regards,
Francesco
 
F

Francesco

Hi Raul,
first of all, it seems to me that you're using the comma operator in a
very weird way. It isn't going to do what you mean at all. You should
enumerate cases with the "case" keyword, not with commas. You should
also be using std::string instead of char*.

I correct myself. You're not speaking about the comma operator, but
about the commas into the string - hence I suppose you know how to
write a switch block.

Fine, the problem then is either into the strtok() function or into
the code that builds the CSV string. In any case you should post your
code for us to review.

Best regards,
Francesco
 
F

Francesco

Hi Raul,
first of all, it seems to me that you're using the comma operator in a
very weird way. It isn't going to do what you mean at all. You should
enumerate cases with the "case" keyword, not with commas. You should
also be using std::string instead of char*.

I correct myself. You're not speaking about the comma operator, but
about the commas into the string - hence I suppose you know how to
write a switch block.

Fine, the problem then is either into the strtok() function or into
the code that builds the CSV string. In any case you should post your
code for us to review.

Best regards,
Francesco
 
P

Puppet_Sock

char *token = strtok(str,",");
                int i = 0;
                while(token != NULL)
                {
                    switch(i)

(str = abc,cde,0.5,,hi,0)

in this case, case0 = abc, case1 = cde, case2 = 0.5, case3 = hi and
case4 = 0. however, it is missing the fourth "data" in this case. i
want to display case0 = abc, case1 = cde, case2 = 0.5, case3 = NA,
case4 = hi and case5 = 0.

the problem here is that once the code sees the first comma, it is
preparing to break down the string but however, it faces yet another
comma immediately, so the "data" between the consecutive commas is
ignored. is there any way that i can overcome this by displaying "NA"
whenever two consecutive commas appears because it could be from
element 1 to 6 that is missing or not available in the string.

please advise. thank you!

Some suggestions:

First, when you post, try to post your code, not some confusing
fragments. Try to get a minimal, compilable code that shows what
you want to show. That way, people who want to help you can just
cut-n-paste it into their compiler and try it.

I have to guess how you are calling strtok to get the next item.
I have to guess if you are doing the right thing about what
strtok returns.

Next, read up on strtok in your language manual. Read it very
carefully, all the way to the end. What does strtok do when it
finds two instances of the seperator in a row?

Finally, whatever the docs say about strtok, it has a meta-
purpose. It's in the language to teach newbies not to use it.
That is, it's part of the reasons that things like std::string
were invented. Look up how to use them, and how to tokenize
using them.
Socks
 
R

RaulGonz

I correct myself. You're not speaking about the comma operator, but
about the commas into the string - hence I suppose you know how to
write a switch block.

Fine, the problem then is either into the strtok() function or into
the code that builds the CSV string. In any case you should post your
code for us to review.

Best regards,
Francesco- Hide quoted text -

- Show quoted text -

Hi Francesco,

you are correct. i am referring to the comma in the string. the string
actually comes from a .txt file and they are fixed (cant make changes
to them as these are actually datas coming in from another equipment).
the file contains 10 sentences with each sentence containing the same
number of data (i.e. each contain 6 data with 5 commas in between
them). i did not show the switch case code as i have narrowed down the
error to the delimiter. following is an example of the 3 sentences in
the txt file:

ad,23,45,oiu,er,0
ad,,45,oiu,er,0
ad,23,45,oiu,,0

i have no problem breaking down the 1st sentence into what i want as
it will output as follow:
first data is ad
second data is 23
third data is 45
fourth data is oiu
fifth data is er
last data is 0

this is because all the all the 6 data are available. however, in the
second sentence, problem appeared due to the fact that one data is not
available(the equipment will output into the txt file consecutive
comma in the event that a data is not available). following is the
output:
first data is ad
second data is 45
third data is oiu
fourth data is er
fifth data is 0

in this case, there is no cout of "last data is <<token<<endl;" as it
has reached the end of the string at 0. therefore, the output
displayed the wrong data from the third line onwards. i wish to output
"NA" whenever a data is not available. as in the case of th second
sentence,

first data is ad
second data is NA
third data is 45
fourth data is oiu
fifth data is er
last data is 0


one way i thought of is to go into the txt file and add a space in
between every consecutive comma and use "char *token = strtok(str,",
");", which will break the string whenever a space or comma is
detected. but problem is there are times when the txt file consist of
100 sentences, so i thought manually inserting a space in between is
not ideal.

please advise.
 
R

RaulGonz

Some suggestions:

First, when you post, try to post your code, not some confusing
fragments. Try to get a minimal, compilable code that shows what
you want to show. That way, people who want to help you can just
cut-n-paste it into their compiler and try it.

I have to guess how you are calling strtok to get the next item.
I have to guess if you are doing the right thing about what
strtok returns.

Next, read up on strtok in your language manual. Read it very
carefully, all the way to the end. What does strtok do when it
finds two instances of the seperator in a row?

Finally, whatever the docs say about strtok, it has a meta-
purpose. It's in the language to teach newbies not to use it.
That is, it's part of the reasons that things like std::string
were invented. Look up how to use them, and how to tokenize
using them.
Socks- Hide quoted text -

- Show quoted text -

hi socks, thanks alot. i will read up on what you have suggested.
 
J

Jerry Coffin

char *token = strtok(str,",");
int i = 0;
while(token != NULL)
{
switch(i)

(str = abc,cde,0.5,,hi,0)

in this case, case0 = abc, case1 = cde, case2 = 0.5, case3 = hi and
case4 = 0. however, it is missing the fourth "data" in this case. i
want to display case0 = abc, case1 = cde, case2 = 0.5, case3 = NA,
case4 = hi and case5 = 0.

the problem here is that once the code sees the first comma, it is
preparing to break down the string but however, it faces yet another
comma immediately, so the "data" between the consecutive commas is
ignored. is there any way that i can overcome this by displaying "NA"
whenever two consecutive commas appears because it could be from
element 1 to 6 that is missing or not available in the string.

You've gotten a few hints about what _not_ to do (i.e. what you're
doing now) but a lot less about what you _should_ do.

One easy thing to do would be to read the data items with
std::getline, specifying comma as the delimiter, then checking for an
empty string, and replacing it with "NA" (or whatever you want).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top