Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

stdtod function in stdlib

Status
Not open for further replies.

mmalcolm

Programmer
Jul 2, 2000
4
US
Hi everyone,<br><br>I am tying to use the &quot;stdtod&quot; that takes the first part of a string and makes it into a number (until it comes to a non-numeric character).&nbsp;&nbsp;I keep getting zero, no matter what the string is.&nbsp;&nbsp;here's my code:<br><br>&nbsp;<br>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;string.h&gt;<br><br>void main() {<br><br> char input[] = &quot;23&quot;, *pEnd;<br> double number = 0.0;<br><br>// gets(input);<br> number = strtod(input, (char **)NULL);<br>// number = atof(input);<br> <br> printf(&quot;The input is %s\n&quot;, input);<br> printf(&quot;The number is %d&quot;, number);<br>}<br><br>//END OF CODE<br><br>As you can see, I've also tried the &quot;atof&quot; function but that did not help either.&nbsp;&nbsp;Can anyone help me fix this code?<br><br>Thanks,<br><br>Rio
 
actually their is already a funciton that does this to some extent it is called sprintf();&nbsp;&nbsp;I think you can do this to some extent.&nbsp;&nbsp;The other problem you might have is if you are bringing in a charecter say 0 that in integer will acutally be the ascii value of i tink 48 or something.&nbsp;&nbsp;You simply have to subtract the 0 ascii value to find the original value.<br><br>hope this helps i put something similar up on my website. <p>moses<br><a href=mailto:tmoses@iname.com>tmoses@iname.com</a><br><a href= my site</a><br>"In the beginning there was HTML, and it was good"<br>
by Nick Heinle, Designing with JavaScript<br>
<br>
<br>
 
hi rio,<br><br>&nbsp;the function works fine. there is an error in your printf<br>statement. actually you are trying to print out a double<br>value using <b>%d</b>. try <b>%f</b> instead and your output will be<br>something like 23.000000.<br><br>-- ralf<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top