Thread: Correction needed to make the program work?

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    25

    Correction needed to make the program work?

    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?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    $ gcc bar.c
    bar.c: In function ‘main’:
    bar.c:15: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    bar.c:15: warning: format ‘%d’ expects type ‘int *’, but argument 3 has type ‘int’

    You forgot some & on your scanf calls.

    Also, if the last line always begins with file_month == 0, then there is no need for the overly complicated sum (which might end up 0 with large -ve temperatures).
    Just test
    while ( file_month != 0 )
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    25
    ...I could've sworn I put those in there. Well, it works now. Thanks for the good eyes!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You mean good compiler.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    25
    That too.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make the following program work?
    By lijr07 in forum C Programming
    Replies: 2
    Last Post: 06-24-2011, 09:23 AM
  2. Do...While Correction Needed
    By jae5086 in forum C++ Programming
    Replies: 7
    Last Post: 10-21-2010, 12:04 PM
  3. How can I make this program work? Problem with arrays
    By babe20042004 in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2009, 07:14 PM
  4. will this work? just a quick test needed
    By shoobsie in forum C Programming
    Replies: 22
    Last Post: 07-11-2005, 11:31 AM
  5. Replies: 2
    Last Post: 03-25-2002, 05:49 AM