1

I want to convert some elements from an array to double (example: '2', '3', '6', ',' '9', '5' to 236,95) but it doesn't work. I tried atof() but it didn't work, because everything behind the comma is getting replaced by zeros. Typecasting also didn't do the work.

Error messages:

error: pointer value used where a floating point value was expected
   22 |  firstDoubleNum = (double)(numOne);
error: pointer value used where a floating point value was expected
   26 |  secondDoubleNum = (double)(numTwo);

The code:

#include <stdlib.h>
#include <math.h>
#include <conio.h>

#define cSizeOfNum 9

//Task 1 from Course Task
char getRealNumber(char numArr[], char numFromArr[], int size);

void comparisonOfTwoRealNumbers(double firstIntNum, double secondIntNum, int size);

int main()
{

    setvbuf( stdout, NULL, _IONBF, 0 );
    setvbuf( stdin, NULL, _IONBF, 0 );

    char firstValue[cSizeOfNum], secondValue[cSizeOfNum];
    char numOne[cSizeOfNum], numTwo[cSizeOfNum];
    double firstDoubleNum, secondDoubleNum;

    printf("Enter two real numbers: \n");
    getRealNumber(firstValue, numOne, cSizeOfNum);
    firstDoubleNum = (double)(numOne);

    printf("\n");
    getRealNumber(secondValue, numTwo, cSizeOfNum);
    secondDoubleNum = (double)(numTwo);

    printf("\n");
    comparisonOfTwoRealNumbers(firstDoubleNum, secondDoubleNum, cSizeOfNum);

    return 0;
}

char getRealNumber(char onlyVal[], char numFromArr[], int size)

{

    int index, anotherIndex;

    index = 0;
    while(index < size)
    {
        onlyVal[index] = getch();
        if((onlyVal[index] >= '0') && (onlyVal[index] <= '9'))
        {
            putch(onlyVal[index]);
            index++;
        }
        else if((onlyVal[index] == ',') || (onlyVal[index] == '.'))
        {
            putch(onlyVal[index]);
            index++;
        }
        else if(onlyVal[index] == '\r')
        {
            onlyVal[index] = '\0';
            break;
        }
        else
        {
            printf("\n");
            printf("Invalid input! Enter again:");
            index = 0;
            printf("\n");
        }
    }
    index = 0;
    anotherIndex = 0;
    while(onlyVal[index] != '\0')
    {
        numFromArr[anotherIndex] = onlyVal[index] - '\0';
        index++;
        anotherIndex++;
    }
    return numFromArr[index < '\0'];
}

void comparisonOfTwoRealNumbers(double numberOne, double numberTwo, int size)
{

    if(numberOne > numberTwo)
    {
        printf("\n");
        printf("%.4lf is bigger than %.4lf", numberOne, numberTwo);
    }
    else if(numberOne < numberTwo)
    {
        printf("\n");
        printf("%.4lf is smaller than %.4lf", numberOne, numberTwo);
    }
    else
    {
        printf("\n");
        printf("%.4lf is equal to %.4lf", numberOne, numberTwo);
    }
    return;
}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • The first step should be to change `#define cSizeOfNum 9` to something like `#define cSizeOfNum 100` so you don't get into trouble with tiny buffer sizes. – Weather Vane Sep 09 '20 at 14:47
  • Use `setlocale` to parse numbers using comma as in: https://stackoverflow.com/a/61981974/1216776 – stark Sep 09 '20 at 14:58

2 Answers2

2

I tried atof() but it didn't work, because everything behind the comma is getting replaced by zeros.

Well, first off, dbush's answer is correct as far as it goes: you do need to call a library function to convert a string to a double, and the function to use is strtod, not atof. But don't give NULL for the second argument. Supply an endptr, so that you can check for input errors properly, like this:

double xstrtod(char *s)
{
    char *endp;
    double val;

    errno = 0;
    val = strtod(s, &endp);
    if (endp == s || *endp != '\0' || errno) {
        fprintf(stderr, "Invalid number: %s\n", s);
        exit(1);
    }
    return val;
}

Second off, by default the C library operates in the "C" locale, and among other things that means strtod parses decimal fractions according to the US-English syntax: it looks for a decimal point, "236.95", not a decimal comma, "236,95". To change this you need to set the locale to one where decimal fractions are marked with a comma. Adding #include <locale.h> to the top of your program, and then adding setlocale(LC_ALL, ""); at the beginning of main, may be sufficient. Try that first. If it doesn't work, consult your operating system's documentation to find out what strings are accepted as the second argument to setlocale, find one that uses a decimal comma, and put it there instead. (Regrettably, there is no way to just ask for decimal commas.)

[EDIT: It's been pointed out to me that your getRealNumber function doesn't produce a NUL-terminated "C string" in the array that it writes to. You will also need to change that in order to use strtod. I recommend scrapping that entire function and instead using fgets.]

zwol
  • 135,547
  • 38
  • 252
  • 361
1

You can't cast a string to a double. What your code is attempting to do is take the pointer value stored in numOne and numTwo and convert it to a double. Such a conversion is not allowed.

What you want is to use strtod which converts a string to a double:

firstDoubleNum = strtod(numOne, NULL);
secondDoubleNum = strtod(numTwo, NULL);
dbush
  • 205,898
  • 23
  • 218
  • 273