1
I’m developing a program that reads an entire line takes the last three values and replaces the comma by dot and then turns this number to double. But I’m not able to develop a method if the number is, for example, 1**.**345,50
.
The point needs to be deleted in these cases first so that I can then replace the comma by point and then turn into double.
Follows the code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_LINE 500
double strtodouble (char *array[MAX_LINE], int position){
double num;
char *ptr;
ptr = strpbrk(array[position], ","); //retorna um ponteiro para a posição da string ","
if(ptr != NULL) *ptr = '.'; //faz a substituição da ',' por '.'
num = atof(array[position]); //converte string para float printf("num = %f", num);
return num;
}
int main(void)
{
char sFrase[]="1337-000/2018 01/10/2018 KI BARATO MERCEARIA DE DESCONT SECRETARIA DA CAMARA SECRETARIA DA CAMARA AÇÃO LEGISLATIVA Dispensa - Isento Compras e Serviços 2.343,50 343,40 343,30";
int count = 0;
char *p = strtok(sFrase, " ");
char *array[500];
//float valor = atof(*array);
char *ptr;
double num1, num2, num3;
while (p)
{
array[count] = p;
p = strtok (NULL, " ");
count++;
}
//printf("%s\n", array[cont-1]);
//printf("%s\n", array[cont-2]);
//printf("%s\n", array[cont-3]);
//array[cont-3] = 343,50
//ptr=array[cont-3][3]
num1 = strtodouble(array, count-3);
printf("\nESTE É O VALOR EMPENHADO --> %2.2f", num1 );
num2 = strtodouble(array, count-2);
printf("\nESTE É O VALOR LiQUIDADO --> %2.2f", num2 );
num3 = strtodouble(array, count-1);
printf("\nESTE É O VALOR PAGO --> %2.2f\n", num3 );
return 0;
}
But what is not done? Or in other words, what remains to be done? There is some error ?
– Isac
Unless I can eliminate the point of the number 2.343,50.
– Amanda Santos
Within the strtodouble function I was able to replace the dot with 0. Now I can’t think of how to remove this 0. I want it to remove the dot and continue reading the number. The number is like this: 2.343,50 I want it to stay 2343,50 .... ptr = strpbrk(array[position], "."); //returns a pointer to the position of the string "." if(ptr != NULL)cheg *ptr = '0'; //does the substitution of '.' for '0' }
– Amanda Santos