Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-04-2009, 02:01 PM   #1
Registered: Oct 2008
Posts: 36

Rep: Reputation: 15
Smile How to convert characters with notations into double

#include <stdio.h>
#include <string.h>

void rd_vconfig()
int index=0;
int i, j=0;
int length = strlen(strtemp);
printf("The length of the temperature string is %d\n", length);
char *str1;
double temp[8];
char *strtemp = "+13.56+3.184+5.295+4.775+12.633+11.44-11.74-12.996";

for(i=0; i<length; i++)
if((i != 0) && (strtemp[i] == '+' || strtemp[i] == '-'))
str1[j] = '\0';
printf("%s\n", str1);
temp[index] = atof(str1);
str1[j] = strtemp[i];

// for(i=0; i<index; i++)
// printf("%d\t", temp[i]);
It seems that the last number -12.996 doesn't display.
I don't know why. And the atof() function doesn't work.
Could anyone help me?
Old 02-04-2009, 03:13 PM   #2
LQ 5k Club
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,397
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Several things bad here. For starters, when posting source code, please use [CODE] tags to preserve formatting and prevent whitespace folding.
Next, you have used str1 as an array, but declared it as a pointer to char. If you are going to store something at the place that str1 points, then str1 should have been initialized to point at some writeable memory (try malloc), or should be defined as an array.
You convert strings to doubles with atof(), but then try to print them as integers.
Your loop terminates at the end of the literal string, but you only do a conversion to floating point when you find a '+' or '-'. This would explain your 'last number' question.
I assume that after all these years, someone else would have noticed if atof() really didn't work, so it is probably your expectation of what it should do that is incorrect. What exactly do you mean by "doesn't work"?
Since you seem to know a priori that your string contains exactly 8 numbers, perhaps it would be easier to use sscanf() to do your conversions. This would reduce your conversion code to one line:
    sscanf( strtemp, "%lf%lf%lf%lf%lf%lf%lf%lf", &temp[0],&temp[1],&temp[2],&temp[3],&temp[4],&temp[5],&temp[6],&temp[7] );
--- rod.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to convert a string to double in C#? darkangel29 Programming 1 05-18-2008 07:17 AM
convert chinese characters -> pinyin secretlydead Linux - General 0 02-07-2008 11:19 PM
Convert binary to integers and double array in C Jose_Manu Programming 1 01-05-2007 07:14 AM
tcsh double click text selection - extra characters Kyoseki Linux - Desktop 4 10-17-2006 12:03 AM
Convert special characters in text file nyk Linux - Software 1 01-05-2005 03:20 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:07 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration