Pointers and Arrays Problem

A

Adam Thickett

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];
i++;
ndata++;
p = strtok(NULL, ",");
}

return 0;
}

Thanks in advance

Adam Thickett
 
J

John Harrison

Adam Thickett said:
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'??


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.
Please help :)

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

p = strtok(maninput, ",");

while(p != NULL)
{
data = maninput[*p];


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.

i++;
ndata++;
p = strtok(NULL, ",");
}

return 0;
}

Thanks in advance

Adam Thickett

john
 
E

Evan Carew

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Adam,

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD4DBQFAKvO+oo/Prlj9GScRAhHNAJ4hmsPPNMWDFUa7IMP637YZakzkJwCY0n5u
cAGMyZm+C/xWhHxxcdMhCw==
=+EiO
-----END PGP SIGNATURE-----
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top