![]() |
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? |
One of the strtok family.
|
yeah, I tried this
:
#include <string.h>but I still have the problem of getting the char array into an int array |
are vectors out of the question for this program? push_back( atoi( token ) ) would work.
|
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.
|
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)why have the operations if your going to cancel them out ? unless i'm too tired to see why... :confused: |
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]; |
So how did you manage it to work when you`re getting an error. And read DaWei`s post about atoi.
|
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];should really be :
char msg3 [6000];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 |
Quote:
|
| 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