Conversion from char to double

March 27, 2010 at 11:19:15
Specs: Windows 7, 14GB
int main()
char xx,nn,a,b,c,d;
double x,n;
char exits;
system ("cls");

do // added check for number only function
cout<<"Enter values for X and N separated by a space: (NUMBERS ONLY)\n";
while (check(xx,nn)==false);

x= atof(xx); //This two lines don't work
n= atof(nn);

cout<<"You have choosen X="<<x<<" and N="<<n<<"\n";
a=first (x,n);
b=middle (x,n);
c=last (x,n);
d=final (a,b,c);

cout<<"\nInput E to exit or any other number to continue..."; //added the E for exit function
while (exits!='e');
return 0;

See the lines "x=atof(xx);"

Error log:
invalid conversion from `char' to `const char*'
initializing argument 1 of `double atof(const char*)'
[Warning] converting to `char' from `double' <---- This warning I get if the 'char to double conversion' not successful because the function that triggers the warning uses double, hence the need for conversion from char to double.

... these got me stuck. Someone please point out the error for me. Thanks !

See More: Conversion from char to double

Report •

March 27, 2010 at 16:15:24
You need do have double as an int as it is not recognised on its own, and const may be invalid because it might/does change it's value, hope this helped :)

There is no spoon, I couldn't be bothered to upload one.

Report •

March 27, 2010 at 16:28:23
i think you've got the wrong function trying to do the job.
atof converts ascii to number ("F"loat?), (atoi does integer conversion).
in other words, atof wants a single-ascii byte which can be any ascii value: A, z, !, #
and converts it to it's ascii index:
65, 123, 33 etc.
so the input should be a single-byte char, not a string.
are you wanting to convert string "123" to number 123?
strtoi converts a string to integer, but only one byte at a time.
(same goes for strtod, strtol, and strtoul)
i don't know if there's another function to do a string, couldn't find one offhand. If not, you'll need a loop.

Report •

March 27, 2010 at 17:22:38
You need to read up on the difference between char and char* as
you seem to be getting these confused.

I guess, from what your program appears to be attempting to do,
that you intended xx and nn to be char* instead of char.

Report •

Related Solutions

April 28, 2010 at 18:35:48
atof() is expecting a null-terminated string, not a single character (char) as you have. You'll need to declare xx and nn to be strings of some sort, so that they can accept multicharacter inputs. atof() will then return a double numeric value.

Report •

Ask Question