I have to write a function to find the sum of the numbers from a string. The sum needs to be printed out for each row in main. I'm using strtok and token to strip off comma's. Then atof to convert string to double. I'm heaving hard time to connect these pieces togather.
#include <stdio.h>
int main(void) {
int i;
double sum=0;
char* str[] = {"12, 34, 56, 78",
"82.16, 41.296",
"2, -3, 5, -7, 11, -13, 17, -19",
"9.00009, 90.0009, 900.009, 9000.09, 90000.9"};
for(i=0;i<(sizeof(str)/sizeof(*str));i++)
{
//print//
printf("sum = %3.6f n",sum);
}
}
double sum(char* s[])
{
const char str[100];
char *token;
char *del = ", ";
token = strtok(str, del);
while( token != NULL )
{
token = strtok(NULL, del);
}
double atof( const char *s
double n;
double sum=0;
for(n=0;n<str(s);n++)
{
sum = sum + s[n];
}
return (sum);
}
Aucun commentaire:
Enregistrer un commentaire