Day 97/100 100 Days of Code
Back to working on the results of the mini-app. I had trouble getting the results because strtod
always output a value of 0.0. I tried atof
, but the same problem occurred. I then tried creating my own functions for char*
to double
conversion and accidentally discovered what the problem was.
The getResult function was getting the correct value, but there was an issue with strtod(). While building my own conversion, I discovered that using &getresult[0] printed the correct value. I decided to use this instead of getResult alone, and now everything works!
for (int i = 0; i < 3; i++)
{
char *characterLoc = strchr(tempCopy, ',');
size_t index = (size_t)(characterLoc - tempCopy);
char *toCopy = calloc(strlen(tempCopy) - index, sizeof(char));
char *getResult = calloc(index, sizeof(char));
strncpy(getResult, tempCopy, index);
strncpy(toCopy, tempCopy + index + 1, strlen(tempCopy )- index);
tempCopy = toCopy;
char *endString;
switch(i)
{
case 0:
//results.maxHeight = atof(&getResult[0]);
results.maxHeight = strtod(getResult, &endString);
break;
case 1:
//results.timeToMaximumHeight = atof(&getResult[0]);
results.timeToMaximumHeight = strtod(getResult, &endString);
break;
case 2:
//results.timeToLand = atof(&getResult[0]);
results.timeToLand = strtod(getResult, &endString);
break;
}
free(getResult);
}
Also, I tried to fix the remaining segmentation faults, but I couldn't resolve all of them. The issues have been reduced, but they still exist.
Subscribe to my newsletter
Read articles from Chris Douris directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Chris Douris
Chris Douris
AKA Chris, is a software developer from Athens, Greece. He started programming with basic when he was very young. He lost interest in programming during school years but after an unsuccessful career in audio, he decided focus on what he really loves which is technology. He loves working with older languages like C and wants to start programming electronics and microcontrollers because he wants to get into embedded systems programming.