473,396 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Pointers and Arrays Problem

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[i]'??

Please help :-)

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

p = strtok(maninput, ",");

while(p != NULL)
{
data[i] = maninput[*p];
i++;
ndata++;
p = strtok(NULL, ",");
}

return 0;
}

Thanks in advance

Adam Thickett
Jul 22 '05 #1
2 1366

"Adam Thickett" <vi***********@hotmail.com> wrote in message
news:a4*************************@posting.google.co m...
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[i]'??
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[i] = 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[i] = atof(p);

using the standard atof function to do the conversion.

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

return 0;
}

Thanks in advance

Adam Thickett


john
Jul 22 '05 #2
-----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-----
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
11
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the...
79
by: Me | last post by:
Just a question/observation out of frustration. I read in depth the book by Peter Van Der Linden entitled "Expert C Programming" (Deep C Secrets). In particular the chapters entitled: 4: The...
4
by: Richard Hayden | last post by:
Hi, Why does gcc (3.3.2) give me a 'initialization from incompatible pointer type' warning when compiling: int main(int argc, char** argv) { int testa; int** testp = testa; }
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
36
by: raphfrk | last post by:
I have the following code: char buf; printf("%lp\n", buf); printf("%lp\n", &buf); printf("%lp\n", buf); printf("%lp\n", buf); printf("%d\n", buf-buf);
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
8
by: Klaas Vantournhout | last post by:
Hi all, I'm in need of a matrix of function pointers, and to be honest. No 'nice' solution has been found yet on that big big internet. It is possible to declare a matrix of function pointers...
8
by: Piotrek | last post by:
Hi, Like almost all of beginners I have problem understanding pointers. Please, look at this piece of code, and please explain me why myswap function doesn't work as it's supposed to do, whereas...
17
by: DiAvOl | last post by:
Hello everyone, merry christmas! I have some questions about the following program: arrtest.c ------------ #include <stdio.h> int main(int agc, char *argv) {
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.