Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Multiple Variable Types In One String?

SephirothSephiroth Fayetteville, NC, USAMember Posts: 1,035
Is it possible to somehow grab a variable out of a string of chars? I want to grab a single integer from a string consisting of characters and numbers. Here is my current method (which isn't working). I am going to manually set the string variable here so you will know what the strings look like.
[code]
char LoadoutString[1024] = "Benelli_Close//M3-1,#M3FLSH-1,*M3-5,";
unsigned char Ammo[16];

void ParseConfig()
{
unsigned char Position;
short int Loop;

ItemCount = 0;
ZeroMemory(&Items, sizeof(Items));
ZeroMemory(&Ammo, sizeof(Ammo));

for(Loop = 0; Loop < strlen(LoadoutString); Loop++)
{
if(LoadoutString[Loop] == ',')
ItemCount++;
}

Loop = 0;
while(LoadoutString[Loop - 1] != '/' && LoadoutString[Loop] != '/')
{
LoadoutName[Loop] = LoadoutString[Loop];
Loop++;
}

//This routine stores JUST the item name up to the '-'
//in my Items array, and is supposed to read the number
//right behind the '-', which is the count of the item.
Loop++;
for(unsigned char Parse = 0; Parse < ItemCount; Parse++)
{
Loop++;
Position = 0;
while(LoadoutString[Loop] != '-')
{
Items[Parse][Position] = LoadoutString[Loop];
Loop++;
Position++;
}
Loop++;
Ammo[Parse] = LoadoutString[Loop];
Loop++;
}
//The line three lines above is my current failing method.

return;
}
[/code]
Am I setting this wrong or is Windoze just giving me a (%*&$@ hard time once again? I need to stick to Linux coding, lol!

-[italic][b][red]S[/red][purple]e[/purple][blue]p[/blue][green]h[/green][red]i[/red][purple]r[/purple][blue]o[/blue][green]t[/green][red]h[/red][/b][/italic]

Comments

  • chick80chick80 Member Posts: 349
    Try using strtok, I think it's simplier!

    Just do something like this:

    [code]
    #include

    void main()
    {
    // If I'm not wrong the string looks like this...
    char string[50] = "var1-45,var2-42";
    char separators[] = "-,";
    char *names[10];
    int values[10];

    for (int i=0; i<10; i++)
    names[i] = (char*) malloc(sizeof(char)*50);

    // This reads the first token, separated by '-' or ','
    names[0] = strtok(string, separators);
    // Giving NULL as the first parameter will continue parsing previous string
    values[0] = atoi(strtok(NULL, separators));

    for (i=1; i<10; i++)
    {
    names[i] = strtok(NULL, separators);
    if (names[i] == NULL)
    break;
    values[i] = atoi(strtok(NULL, separators));
    }

    for (int j=0; j<i; j++)
    printf("%s = %d
    ", names[j], values[j]);
    }

    [/code]


    [hr]
    [italic]How beautiful, if sorrow had not made Sorrow more beautiful than Beauty itself.[/italic]
    JOHN KEATS


  • Justin BibJustin Bib USAMember Posts: 0

    < http://forcoder.org /> free video tutorials and ebooks about | JavaScript Visual Basic Java C Visual Basic .NET Ruby C# Go Objective-C MATLAB PHP Perl Assembly Scratch PL/SQL C++ R Python Delphi Swift COBOL VBScript Crystal Hack Prolog Clojure ABAP Transact-SQL ML Logo Erlang Bash Kotlin Dart F# D Rust Scheme Lua Fortran Scala LabVIEW Lisp SAS Julia Alice FoxPro Ada Apex Awk | _______

Sign In or Register to comment.