Pointers and Arrays Problem

Discussion in 'C++' started by Adam Thickett, Feb 11, 2004.

  1. Hey,

    I call this function as a call back after the user has inputted some
    numbers seperated by commas into the char maninput[500] variable (eg,
    "30,31,32,33"). I then intend the below function to seperate the
    string into the seperate numbers (i.e. 30,31,32,33) and store them in
    the float data[500] in the positions [0], [1], [2], [3] respectivly as
    the variable i increases. I've never done much with strings like this
    before and i can't figure out why the code below doesn't put the data
    into 'data'??

    Please help :)

    int mancalculate()
    int i=0, j=0;
    char *p;

    p = strtok(maninput, ",");

    while(p != NULL)
    data = maninput[*p];
    p = strtok(NULL, ",");

    return 0;

    Thanks in advance

    Adam Thickett
    Adam Thickett, Feb 11, 2004
    1. Advertisements

  2. They aren't C++ strings. In C++ we have the std::string class, which is what
    most people (at least in this NG) will understand as strings. What you're
    talknig about is null terminated C strings.

    This is wrong. For a start p is pointing to the data you want there no need
    to use maninput, secondly the data is still text, you have to convert it to
    a float.

    This should work

    data = atof(p);

    using the standard atof function to do the conversion.

    John Harrison, Feb 11, 2004
    1. Advertisements

  3. Adam Thickett

    Evan Carew Guest

    Hash: SHA1


    Since this is the C++ group, let's use the C++ tools we have at our
    disposal in the standard. First, strings and then string handling
    routines. This in mind, I think the following should get you through to
    the next problem you might have.

    //C++ News Group test program for tokenizing strings

    #include <string>
    #include <boost/tokenizer.hpp>

    using namespace std;
    using namespace boost;
    typedef tokenizer<char_separator<char> > Tokenizer;
    int main(){
    string input_stuff = "30,31,32,33";
    char_separator<char> sep(",");
    Tokenizer tok(input_stuff, sep);
    for (Tokenizer::iterator tok_iter = tok.begin();
    tok_iter != tok.end(); ++tok_iter)
    std::cout << "<" << *tok_iter << "> ";

    Hope this helps,
    Evan Carew
    Version: GnuPG v1.0.6 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    -----END PGP SIGNATURE-----
    Evan Carew, Feb 12, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.