Run-Time Check Failure #3 - The variable 'weathedata' is being used without being initialized?

Hi I'm new to C language programming. I was writing this program but I always keep facing this problem "Run-Time Check Failure #3 - The variable 'x' is being used without being initialized”. Can anyone please help me?

Here’s my program:

int main(void)
{
double chicago[MONTH];
double sanfran[MONTH];
//Declare and iniialize variables.
int dates;
int k;
int h;
int l;
int numofDays = 31;
double totalchicago;
double totalsanfran;
double avgchicagotemp;
double avgsanfrantemp;
double maxchicago = -999;
double minsanfran = 999;
FILE *weatherdata;
FILE *moreweatherdata;

//Check to make sure file opened properly.
if (weatherdata == NULL)
printf("Error opening input file. Redefine directory location. \n");
else
{
//This while loop reads in the data.
fopen_s(&weatherdata, "weathers.txt", "r");
dates=0;
while(fscanf_s(weatherdata,"%lf %lf", &chicago[dates], &sanfran[dates])==2)
{
dates++;
}

//This for loop calculates the total of the temperatures in Chicago and San Francisco during August 2009; the averages are calculated outside the loop
totalchicago=0;
totalsanfran=0;
avgchicagotemp=0;
avgsanfrantemp=0;
for(dates=0; dates<numofDays; dates++)
{
totalchicago += chicago[dates];
totalsanfran += sanfran[dates];
}
avgchicagotemp=(float)totalchicago/dates;
avgsanfrantemp=(float)totalsanfran/dates;

//This for loop calculates the days that San Francisco had a higher temperature than Chicago
k=0;
for(dates = 0; dates<numofDays; dates++)
{
if(sanfran[dates]>chicago[dates])
{
k = dates;
}
}

//This for loop calculates the highest temperature in Chicago, and the day in which it happened.
h=0;
for(dates = 0; dates<numofDays; dates++)
{
if(chicago[dates] > maxchicago)
{
maxchicago = chicago[dates];
h = dates;
}
}

//This for loop calculates the lowest temperature in San Francisco, and the day in which it happened.
l=0;
for(dates=0; dates<numofDays; dates++)
{
if(sanfran[dates] < minsanfran)
{
minsanfran = sanfran[dates];
l = dates;
}
}

//Opens new file
fopen_s(&moreweatherdata, "weatherdata2.txt", "w");

//Print summary information
fprintf(moreweatherdata, "Highest temperature (Fahrenheit) in Chicago during August: %.2lf\n", maxchicago);
fprintf(moreweatherdata, "This happened on August %d\n", h);
fprintf(moreweatherdata, "Average temperature (Fahrenheit) in Chicago during August: %.2lf\n", avgchicagotemp);
fprintf(moreweatherdata, "Average temperature (Fahrenheit) in San Francisco during August: %.2lf\n", avgsanfrantemp);
fprintf(moreweatherdata, "San Francisco had a higher temperature than Chicago on August %d\n", k);
fprintf(moreweatherdata, "Lowest temperature (Fahrenheit) in San Francisco during August: %.2lf\n", minsanfran);
fprintf(moreweatherdata, "This happened on August %d\n", l);
fprintf(moreweatherdata, "Highest temperature (Fahrenheit) in Chicago during August: %.2lf\n", maxchicago);
fprintf(moreweatherdata, "This happened on August %d\n", h);

//Close file and exit progam
fclose(moreweatherdata);
}
}
1
2
    if (weatherdata == NULL)
        printf("Error opening input file. Redefine directory location. \n");

Thar check should be after attempt to open file, not before it.
Thanks for pointing that out! I now understand why it kept giving me that error. Also, in the part where I have to calculate the days in which San Francisco had a higher temperature, I wanted to print all the temperatures, but it only gives me one date. I'm kind of confused in this part too. Can anyone point out what I should do?

//This for loop calculates the days that San Francisco had a higher temperature than Chicago
k=0;
for(dates = 0; dates<numofDays; dates++)
{
if(sanfran[dates]>chicago[dates])
{
k = dates;
}
}
You have only one value to store day, so it is impossible to store everything in it.
Either do in-place write like (in pseudocode):
print "San Francisco had a higher temperature than Chicago on days:\n"
foreach(day in range 0..30)
    if sanfran[day] > chicago[day]
        print day + "\n"

Or store days when SF wha hotter than Chicago in array.
Topic archived. No new replies allowed.