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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

pls debug this issue in the following C program

Status
Not open for further replies.

SwatiA

Programmer
May 10, 2002
9
IN
I am trying to compile a C program named " atof" (ref: Kernigham ritchie,second edition "The C programing language", pg 71). using a microsoft visual c++ compiler - version 6.

THe problem which I am getting is that when I compile and run the program for the value 1.234 (of the input string) and executing it, the results are fine. but when I am debugging the same frogram using the f10 and f11 keys the result printed is as follows:
-1.#IND000000000.
can any one explain this mystry.


here is the entire code for quick reference:
/* atof: converts string to double*/
#include <stdio.h>
#include<ctype.h>
#define MAX 10
double atof(char x[]);
main()
{
char s[MAX];
printf(&quot;pls enter the string you want to convert to a double\n&quot;);
scanf(&quot;%s&quot;,s);
printf(&quot;%f&quot;,atof(s));
}
double atof(char s[])
{
double val,power;
int i,sign;
for(i=0;isspace(s);i++)/*skip white spaces*/
;
sign = (s=='-')? -1:1;
if (s=='+'||s=='-')
i++;
for(val=0.0;isdigit(s);i++)
val=10.0*val+(s-'0');
if(s=='.')
i++;
for (power=1.0;isdigit(s);i++)
{
val = 10.0*val+(s-'0');
power *=10.0;
}
return sign*val/power;
}
 
hi,
You are incrementing 'i' not 's'? or do you really mean s everywhere you say s?
 
hi tdagod,
yes, the code is s [ i] in place of s. I had cut and paste the code onto this forum ,but the s [ i] 's did not get copied properly. the original problem still persists.

swati.
 
/* atof: converts string to double*/
#include <stdio.h>
#include<ctype.h>
#define MAX 10
double atofi(char s[]);
main()
{
char s[MAX];
printf(&quot;pls enter the string you want to convert to a double\n&quot;);
scanf(&quot;%s&quot;,s);
printf(&quot;%f&quot;,atofi(s));
}

double atofi(char a[])
{
int i,sign,power;
double val;
for(i=0; isspace(a);i++)
;
sign=(a=='-')? -1:1;
if (a=='-' ||sign=='+')
i++;
for (val=0.0;isdigit(a);i++)
val=val*10+(a-'0');
if(a=='.')
i++;
for(power=1;isdigit(a);i++)
{
val=10.0*val+(a-'0');
power= power *10;
}
return val*sign/power;
}

 
The program looks fine. When you're running it inside VC++, are you sure you're entering the input in the IDE in exactly the same way you are when running it outside the IDE?
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top