How can I write a program that reads an input string and converts the string into a float number.
I'm a bit stuck for the function:
#include <stdlib.h>
#include <string.h>
double convert_to_double (char *);
int main(void)
{
char *s;
s = malloc(10 * sizeof(char));
printf("Enter text: ");
fgets(str, 10, stdin);
printf("The number is %lf", convert_to_double (str));
return 0;
}
double convert_to_double (char *str) {
double convert_to_double (char *str) {
char *s;
double result;
result = strtod(str, &s);
if (s != NULL) {
char *anotherEnd;
double anotherResult = strtod(s, &anotherEnd);
}
if (isalnum(s) == 0){
printf("Wrong digit entered..");
}
return result;
}
CodePudding user response:
If you want to strictly validate the input string, please try:
double convert_to_double(char *str)
{
char *e;
double d = strtod(str, &e);
if (*e != '\n') {
fprintf(stderr, "input error %c\n", *e);
exit(1);
}
return d;
}
The character *e
is the first character remaining after the valid number
representation is converted; usually a newline in this usage.
If you do not have to check the input string so strictly, just say:
double convert_to_double(char *str)
{
double d = strtod(str, (char **)NULL);
return d;
}
You may not have to functionize it.
CodePudding user response:
You are doing the wrong way for strtod :
double convert_to_double (char *str) {
{
char *s;
double result;
// first parameter is the string you want to convert
// second pram is a pointer to the first char which was not converted.
// this can be used if you have more than one data to convert in the same string
result = strtod(str, &s);
// example of using returned pointer :
if (s != NULL) {
char *anotherEnd;
double anotherResult = strtod(s, &anotherEnd);
}
return result;
}
CodePudding user response:
How do you convert a string to
double
?
if (s != NULL)
is not a useful test. The end pointer s
is never NULL
when attempting to convert a string.
Use strtod()
. Test for potential errors.
double convert_to_double (const char *str) {
// Optional test of str, which suppose to point to a string
if (str == NULL) {
fprintf(stderr, "Null pointer\n");
return NAN;
}
char *endptr; // Location of end of conversion.
errno = 0;
double result = strtod(str, &endptr);
if (str == endptr) {
fprintf(stderr, "No conversion of <%s>\n", str);
return NAN;
}
// Consume trailing white-space.
while (isspace((unsigned char) *endptr)) {
endptr ;
}
if (*endptr) {
fprintf(stderr, "Trailing junk <%s>\n", str);
return result;
}
if (errno) {
perror("Error:");
return result;
}
return result;
}
Pedantically the last test should be more forgiving as underflow is usually not a real problem.
if (errno) {
if (errno != ERANGE && fabs(result > DBL_MIN)) {
perror("Error:");
return result;
}
}
A double
may be hundreds of digits in text form. Be generous in buffer size.
// s = malloc(10 * sizeof(char));
#define DBL_STR_SIZE 2000
str = malloc(sizeof *str * DBL_STR_SIZE);
if (str) {
printf("Enter text: ");
if (fgets(str, DBL_STR_SIZE, stdin)) {
// process str