help me on a simple 'atof' function

#include<iostream.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
void main()
{
char a[99];
float b;
cin>>a;
if(!isalpha(a[0]))
{
b=atof(a[0]);
}
b+=10;
cout<<b;
getch();
}
// above program doesn't result in desired way... help me to debug.... i'm using 'turbo c++ 3.0'
Last edited on
above program doesn't result in desired way
and what is the desired way?
Could you be more specific about the problem you are having?

Also, just a heads up - [code]int a; // some code[/code] = int a; // some code .
atof() takes a C string, you're passing a character.
and what is the desired way?


actually it's bigger program.... but I simplified to review the specific problem with 'atof' function.

as you know the above program doesn't has real world application..

in this program i tried to get a string value as input and check the value is not an alphabetic letter.
then the string value will be converted into float value. And the float value+10 should be my output . But the program not even compile. it shows two errors and they are...

(1.)can't convert to 'int' to constant 'char *'

(2.)Type mismatch in parameter '__s' in call to 'atof(constant char *)'
atof() takes a C string, you're passing a character.

could you please debug the program?
I am a beginner.
Last edited on
check the value is not an alphabetic letter.

Alphabetic letters aren't generally illegal in floating point number expressions - eg. 1.005e+20 is a legal expression. And just because a character is no alphabetic letter doesn't guarantee it's a number.

PS: You are using ancient C++ here. It's int main, not void main (since a very long time too) - same for iostream.h and stdlib.h and ctype.h (instead of iostream, cstdlib and cctype). atof takens an entire string, not just a single character like you do.
double n = atof(a); // atof returns a double, not a float
And just because a character is no alphabetic letter doesn't guarantee it's a number.

yeah.. I know...
but it's not my real problem.
double n = atof(a); // atof returns a double, not a float


after change float to double still error (can't convert to 'int' to constant 'char *') remains...
Are you passing a like we've been telling you or a[0]?
It's solved.....

thank you filipe... yes isalpha takes a character and atof takes whole string.....
again thanks filipe...
after this change float value too compile instead of double...
what it's actual return type?? why both got compiled?
A double (like the value returned by atof) will convert to float if you assign it to one.

EDIT: fixed typo.
Last edited on
A double (like the value returned by atof) will convert to float is you assign it to one.

thank you....
thank you all replied here...
Topic archived. No new replies allowed.