![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2006
Posts: 10
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2006
Posts: 10
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#4 |
|
Programmer
|
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. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#6 |
|
Programmer
|
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. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jan 2006
Posts: 10
Rep Power: 0
![]() |
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]; |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
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. |
|
|
|
|
|
#9 |
|
Newbie
Join Date: Jan 2006
Posts: 10
Rep Power: 0
![]() |
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"; |
|
|
|
|
|
#10 |
|
Newbie
Join Date: Jan 2006
Posts: 10
Rep Power: 0
![]() |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |