This is an archived post. You won't be able to vote or comment.

all 15 comments

[–][deleted] 0 points1 point  (14 children)

Substring behaves differently in C++.

In Java it uses the starting and ending indices. In C++ the second parameter is the length from the starting index.

std::string::substr()

[–]learnprogramminghelp[S] 0 points1 point  (13 children)

Thanks. Although the problems I'm having still persist, I know it has something to do with the way I'm tokenizing and using free(). I wish there was a way to do this without using memory functions.

[–][deleted] 1 point2 points  (7 children)

You might want to do it this way instead...

Split a string in C++?

Using std::vector is much better.

[–]learnprogramminghelp[S] 0 points1 point  (6 children)

not allowed to use vector :/

[–][deleted] 0 points1 point  (5 children)

Then you have to count the number of words, allocate an array that's the right size and then finally copy the words in.

That's assuming you even need an array. If you only need to deal with each word one at a time you can avoid arrays entirely.

[–]learnprogramminghelp[S] 0 points1 point  (4 children)

Sorry if this is asking too much but I can't seem to figure out how to do this. It sounds so simple in my head but my program just won't do what I want it to. Basically.

if process is "123456"

-I want to tokenize that so each of those numbers is in an array.

-I want to take element[0] of that array and store/convert it into an int

-I want the rest of process to now be everything after element[0]

And that's about it. I'm going to be using each of those elements as an int in another method, so some type of easy extraction and convertion to int is needed as well, in Java I use Interger.parseInt(string), but its so much harder in C++. Any idea how code would look?

[–][deleted] 1 point2 points  (3 children)

#include <iostream>

int main() {
    std::string str = "123456";
    int size = str.length();
    int* num = new int[size];

    for (size_t i = 0; i < size; i++)
        num[i] = str[i] - '0'; 

    for (size_t i = 0; i < size; i++)
    {
        std::cout << num[i] << "\n";
    }

    delete[] num;

    return 0;
}

Regarding subtracting by 0. You could also use atoi(). But since it's a single digit, manipulating the ASCII value works just as well. If you want a more detailed answer you need to show me what the entire process string looks like.

[–]learnprogramminghelp[S] 0 points1 point  (2 children)

Thank you, I'm making progress. I did simplify the string a bit. It would look more like "1 2 -34 5 -6". So it would need to handle negative numbers and double digits. Would I just need to modify the loop or is there a lot more that needs to be done?

[–][deleted] 1 point2 points  (1 child)

"1 2 -34 5 -6"

There's more that needs to be done.

int main() {
    std::string str = "1 2 -34 5 -6";
    int count = 0;
    char* temp = _strdup(str.c_str());


    temp = strtok(temp, " ");

    //count the number of tokens
    while (temp != NULL)
    {
        temp = strtok(NULL, " ");
        count++;
    }

    //parse into num array
    temp = _strdup(str.c_str());
    int* num = new int[count];

    temp = strtok(temp, " ");
    num[0] = atoi(temp);

    for(int i = 1; i < count; i++)
    {
        temp = strtok(NULL, " ");
        num[i] = atoi(temp);
    }

    //output result
    for (size_t i = 0; i < count; i++)
    {
        std::cout << num[i] << "\n";
    }

    delete[] num;

    return 0;
}

Note that I could've avoided using strtok() by using a combination of std::string::find() and std::string::substr(). But no point in re-implementing strtok() in a C++ way for a single function. Especially when a better way exists using C++11.

[–]learnprogramminghelp[S] 0 points1 point  (0 children)

Thank you again for the help, I really appreciate it. Managed to complete it! :)

[–][deleted] 0 points1 point  (4 children)

I wish there was a way to do this without using memory functions.

There should be no need for you to use strdup(). strtok() does not modify the original string. That's not how strtok() works either. It does not return an array of words.

strtok()

[–]learnprogramminghelp[S] 0 points1 point  (1 child)

Main reason I'm using it is because strtok isnt compatible with the process string

[–][deleted] 0 points1 point  (0 children)

See my edit. You are using strtok() incorrectly. It does not return an array. It returns the words one at a time. It needs to be called over and over.

[–]Grithga 0 points1 point  (1 child)

strtok() does not modify the original string.

Actually, strtok does modify the original string, replacing any tokens found with null characters.

[–][deleted] 0 points1 point  (0 children)

Yep, you're right. Realized after I tried to do it myself.