• C

converting string to int

hello.  I have a string that represents the coefficients of an equation.  i need to parse this and eventually convert each coefficient to it's integer or float equivalent.
for example,
"8 6 -6/7 +22/16 "
means 8 = 6(x1) -6/7(x2) +22/16(x3)

i'm thinking of using strtok( ) to get each of the coefficients separately, but then what do i do next? i tried to cast the string to an integer, but that doesn't work.   please help.  thank you.  i'm sorry i can't offer that many points for this, it's b/c i'm a new user.
naz4viperAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
AxterConnect With a Mentor Commented:
Example:
char data[] = "123";
int num_data = atoi(data);
0
 
AxterCommented:
use atoi
0
 
AxterCommented:
If you need to convert it to float, then you can use atof
0
 
ankuratvbConnect With a Mentor Commented:
Adding to what Axter has already pointed out,
atoi() & atof() require stdlib.h

And they take care of your signs as well,
if you have char ptr[]="-123";
int p=atoi(ptr);

then,p=-123;

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.