So at the moment, I am working with file I/O and data files. I'm working on a program that can select different days, months, and temperatures ranging from the years 1995 to 2011. My first step in solving the problem is getting the program to select the correct data from a very small list of five values. This is the code:
Code:
#include <stdio.h>
const int BASE_YEAR = 1995;
const int CURR_YEAR = 2011;
int main()
{
int user_month, user_day;
int file_month, file_day, file_year;
double file_temperature, temp_sum_month, temp_sum_day;
char filename[20];
double average_temp_month, average_temp_day;
printf("What is the month and day of your wedding? \n");
scanf("%d%d", user_month, user_day);
printf("What file stores your city's temperature data? \n");
scanf("%s", filename);
FILE *ifp = fopen(filename, "r");
int year_range = CURR_YEAR - BASE_YEAR;
double sum = -1.0;
while(sum != 0.0)
{
fscanf(ifp, "%d%d%d%lf", &file_month, &file_day, &file_year, &file_temperature);
printf("The number is %d. \n", file_day);
if(user_day == file_day)
printf("Good work! \n");
else
printf("Keep trying! \n");
sum = file_month + file_day + file_year + file_temperature;
printf("The sum is %.1lf. \n", sum);
}
return 0;
}
(Ignore the unused stuff; I'll incorporate that after I get this part out of the way.) The sample input I'm using is 3 3 for the first question and FLORLAND2.txt for the second, but call it whatever you want. And here's what the data file looks like:
1 1 1995 66.7
1 2 1995 63.9
1 3 1995 60.3
1 4 1995 55.9
0 0 0 0.0
The goal here is to read all the numbers, saying "Keep trying!" to represent that they don't match the user input (3), and "Good job!" when it does. The code looks like it should say "Good job!" when the program reads in the 3, but it doesn't because for some reason, the user_day doesn't equal the file_day. (I also tried it the other way around, just in case.) What's the one thing I did wrong to prevent the desired result?