Day 97/100 100 Days of Code

Day 97/100 100 Days of Code

Jumping Ball

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.