OneCompiler

Can someone please help code doesn't work

#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

// For Red, Blue And Yellow Color
#define RED "\x1B[31m"
#define BLU "\x1B[34m"
#define YEL "\x1B[33m"
// Color Reset
#define RESET "\033[0m"

// Print Long Date In Human Readable Format
void printDate(long date) {
time_t seconds = date;
struct tm* tm = localtime(&date);
char months[][4] = {
"JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC"
};

// Print Fomatted Date
printf("%02d-%s-%d %02d:%02d:%02d", tm->tm_mday, months[tm->tm_mon], tm->tm_year + 1900,
    tm->tm_hour, tm->tm_min, tm->tm_sec);

}

// Convert Reading To Fahrenheight
double readingToFahrenheit(double reading) {
double mv = ((reading / 2047) * 5000);
double kelvin = mv / 10;
return (kelvin - 273.15) * (9.0/5.0) + 32;
}

// Print Colored Temperature Output
void printColoredOutput(double temp) {
if (temp < 50.0) {
printf("%s%.2f F%s", BLU, temp, RESET);
} else if (temp > 90.0) {
printf("%s%.2f F%s", RED, temp, RESET);
} else {
printf("%s%.2f F%s", YEL, temp, RESET);
}
}

// Main
int main(void) {
char filePath[256];

// Read File Name From The User
printf("Enter The File Path: ");
fgets(filePath, sizeof(filePath) - 1, stdin);

// Remove New Line From The End Of File Path
filePath[strlen(filePath) - 1] = '\0';

FILE * fp;
fp = fopen(filePath, "r");

if (fp == NULL) {
    printf("Error: %s Cannot Be Opened!\n", filePath);
    return -1;
} else {
    // Process File
    long date;
    int temp1, temp2, temp3;
    int lineCount = 0;
    double sumTemp1, sumTemp2, sumTemp3;
    
    // First Line
    if((fscanf(fp, "%ld,%d,%d,%d", &date, &temp1, &temp2, &temp3)) == 4) {
        printf("Starting Time: ");
        printDate(date);
        sumTemp1 = temp1;
        sumTemp2 = temp2;
        sumTemp3 = temp3;
        lineCount++;
    }

    // Rest Of The Lines
    while ((fscanf(fp, "%ld,%d,%d,%d", &date, &temp1, &temp2, &temp3)) == 4) {
        sumTemp1 += temp1;
        sumTemp2 += temp2;
        sumTemp3 += temp3;
        lineCount++;
    }

    // Display Result
    printf("\nEnding Time: ");
    printDate(date);

    // Compute Average
    double temp1Avg = sumTemp1 / lineCount;
    double temp2Avg = sumTemp2 / lineCount;
    double temp3Avg = sumTemp3 / lineCount;

    // Print Average
    printf("\nTemperature Sensor 1 Average: ");
    printColoredOutput(readingToFahrenheit(temp1Avg));
    printf("\nTemperature Sensor 2 Average: ");
    printColoredOutput(readingToFahrenheit(temp2Avg));
    printf("\nTemperature Sensor 3 Average: ");
    printColoredOutput(readingToFahrenheit(temp3Avg));
    printf("\n");
}


return 0;

}

this is what I get

this is what I should get

                Starting time Fri Aug 06 13:06:40 2010

                 Ending time Thu Apr 10 13:09:09 2014

                 Temperature Sensor 1 Average:  -459.7ºF

                  Temperature Sensor 2 Average: 440.3ºF

                 Temperature Sensor 3 Average:  77.2ºF

No answers yet!

4 years ago by