Solved

converting string to int

Posted on 2004-04-28
6
202 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

861 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