Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 20th, 2007, 10:22 AM   #1
jokr004
Newbie
 
Join Date: Jan 2006
Posts: 10
Rep Power: 0 jokr004 is on a distinguished road
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?
jokr004 is offline   Reply With Quote
Old Jul 20th, 2007, 11:48 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
One of the strtok family.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 20th, 2007, 11:53 AM   #3
jokr004
Newbie
 
Join Date: Jan 2006
Posts: 10
Rep Power: 0 jokr004 is on a distinguished road
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
jokr004 is offline   Reply With Quote
Old Jul 20th, 2007, 1:30 PM   #4
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
are vectors out of the question for this program? push_back( atoi( token ) ) would work.
__________________
My site :: http://www.freewebtown.com/dougalbe
I'll try to be nicer, if you try being smarter.
Ben.Dougall is offline   Reply With Quote
Old Jul 20th, 2007, 1:35 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jul 20th, 2007, 1:39 PM   #6
Ben.Dougall
Programmer
 
Ben.Dougall's Avatar
 
Join Date: Jul 2007
Location: London, Ontario, Canada
Posts: 34
Rep Power: 0 Ben.Dougall is on a distinguished road
Send a message via MSN to Ben.Dougall
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...
__________________
My site :: http://www.freewebtown.com/dougalbe
I'll try to be nicer, if you try being smarter.
Ben.Dougall is offline   Reply With Quote
Old Jul 20th, 2007, 2:08 PM   #7
jokr004
Newbie
 
Join Date: Jan 2006
Posts: 10
Rep Power: 0 jokr004 is on a distinguished road
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];
jokr004 is offline   Reply With Quote
Old Jul 20th, 2007, 3:16 PM   #8
pegasus001
Hobbyist Programmer
 
pegasus001's Avatar
 
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2 pegasus001 is on a distinguished road
So how did you manage it to work when you`re getting an error. And read DaWei`s post about atoi.
__________________
You never test the depth of a river with both feet.
The believer is happy. The doubter is wise.
Free speech carries with it some freedom to listen.
The next generation will always surpass the previous one. It`s one of the never ending cycles of life.
pegasus001 is offline   Reply With Quote
Old Jul 20th, 2007, 3:19 PM   #9
jokr004
Newbie
 
Join Date: Jan 2006
Posts: 10
Rep Power: 0 jokr004 is on a distinguished road
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 is offline   Reply With Quote
Old Jul 20th, 2007, 3:21 PM   #10
jokr004
Newbie
 
Join Date: Jan 2006
Posts: 10
Rep Power: 0 jokr004 is on a distinguished road
Quote:
Originally Posted by pegasus001 View Post
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
jokr004 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Phone Number to Name... NDawg28 Visual Basic 9 Mar 10th, 2007 9:40 PM
Assigning an array of lists deanosrs C 42 Apr 13th, 2006 1:35 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:20 AM.

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