Solved

converting string to int

Posted on 2004-04-28
6
192 Views
Last Modified: 2010-04-15
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.
0
Comment
Question by:naz4viper
  • 3
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 10943416
use atoi
0
 
LVL 30

Accepted Solution

by:
Axter earned 25 total points
ID: 10943422
Example:
char data[] = "123";
int num_data = atoi(data);
0
 
LVL 30

Expert Comment

by:Axter
ID: 10943431
If you need to convert it to float, then you can use atof
0
 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 25 total points
ID: 10945447
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

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Estimate porting issues. 9 119
C dll call freezes 5 101
C language IDE – Compilers installation 14 71
How to creat good software interface ? 1 66
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question