Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   splitting up char and converting to int (http://www.programmingforums.org/showthread.php?t=13595)

jokr004 Jul 20th, 2007 11:22 AM

splitting up char and converting to int
 
Ok, I need to take user input formatted like this
098|87|94|101|108|115|122|129|136|143|150|157|164|171|178|185|192|199|206|213|220|227|234|241|248

from a char array and separate the numbers and put them into an int array. Kind of like unix cut command using | as the delimiter. Any easy way to do this?

DaWei Jul 20th, 2007 12:48 PM

One of the strtok family.

jokr004 Jul 20th, 2007 12:53 PM

yeah, I tried this
:

#include <string.h>
#include <stdio.h>

const char * ServerMessageDelimiters = "|";
#define MaxTokens  10
#define MaxTokenLength 255
#define MaxMessageLength (MaxTokens*(MaxTokenLength+1)-1)

int main()
{
  char msg[MaxMessageLength+1] =  "ABC|123456|654321|234234";

  char msgParts[MaxTokens][MaxTokenLength+1];

  int partNumber = 0;

  char * token = strtok( msg, ServerMessageDelimiters );

  while (  token != NULL
        && partNumber<MaxTokens
        && strlen(token) <  MaxTokenLength
        )
  {
      strcpy( msgParts[partNumber], token );
      token = strtok( NULL,  ServerMessageDelimiters );
      ++partNumber;
  }

  int i;
  for ( i = 0; i < partNumber; ++i )
  {
      printf( "%s\n", msgParts[i] );
  }
}


but I still have the problem of getting the char array into an int array

Ben.Dougall Jul 20th, 2007 2:30 PM

are vectors out of the question for this program? push_back( atoi( token ) ) would work.

DaWei Jul 20th, 2007 2:35 PM

One presumes, by the headers, that he's using C; thus no vectors. One you've separated out the strings, just convert them to numbers. Strtol comes to mind, as does sscanf. Atoi is bad news because you can't tell whether a returned zero is an error or a valid conversion.

Ben.Dougall Jul 20th, 2007 2:39 PM

never thought about that when using atoi, I'll have to keep that in mind. My C knowledge is very minimal, seeing as this is posted in the C++ forum, i thought i offer a C++ oriented solution.

thanks again for the insight on atoi.

just looking at the code here...
:

#define MaxMessageLength (MaxTokens*(MaxTokenLength+1)-1)

int main()
{
  char msg[MaxMessageLength+1] =  "ABC|123456|654321|234234";


why have the operations if your going to cancel them out ? unless i'm too tired to see why... :confused:

jokr004 Jul 20th, 2007 3:08 PM

hey, I am using c++
did manage to get it to work with atoi(&msgParts[alphct])
but, my problem now is that in the bit of code I use to split up the char initializes the msgParts with char msgParts[MaxTokens][MaxTokenLength+1];

when I go to do something like this

while (alphct < textl)
{
msg2[alphct] = atoi(&msgParts[alphct]);
alphct += 1;
}
I get this
error: invalid conversion from ‘char*’ to ‘char’
Im guessing because I initialised msg2 with just
int msg2 [6000];

pegasus001 Jul 20th, 2007 4:16 PM

So how did you manage it to work when you`re getting an error. And read DaWei`s post about atoi.

jokr004 Jul 20th, 2007 4:19 PM

ok... here is the whole code. dont ask exactally what it does, its a bit obscure at the moment, but

http://www.core-switchnet.com/source

where you see
:

char msg3 [6000];
    int msg2 [6000];
   
    cout << "1";
    while (alphct < textl)
    {
            msg3[alphct] = msgParts[alphct][alphct];
            alphct += 1;
    }


should really be
:

char msg3 [6000];
    int msg2 [6000];
   
    cout << "1";
    while (alphct < textl)
    {
            msg3[alphct] = msgParts[alphct];
            alphct += 1;
    }


added the seccond [alphct] because for some strange reason, in the code in my previous post, they initialize msgParts as a multi dimensional variable, which I think is my problem. with the second [alphct] it compiled without errors, but stopped at a segment fault right after
:

while (  token != NULL
                && partNumber<MaxTokens
                        && strlen(token) <  MaxTokenLength
                )
          {
              strcpy( msgParts[partNumber], token );
              token = strtok( NULL,  ServerMessageDelimiters );
              ++partNumber;
          }

          int i;
          for ( i = 0; i < partNumber; ++i )
          {
              printf( "%s\n", msgParts[i] );
              textl += 1;
          }
          cout << "1";

and doesnt even reach the cout<< "1";

jokr004 Jul 20th, 2007 4:21 PM

Quote:

Originally Posted by pegasus001 (Post 130917)
So how did you manage it to work when you`re getting an error. And read DaWei`s post about atoi.

I tried running that little bit by itself


All times are GMT -5. The time now is 2:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC