Thread: Problem with homework

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

    Problem with homework

    Good evening,

    This program complies, but right around the program starts reading the file I get the error that weather.exe has stopped working. I am at a loss as to why the program is not working Can anyone see what I have done wrong? Thanks.

    Code:
    #include <stdio.h>
    
    // Declare variables to recieve user input
    
    char cityFileName[20];
    int month, day;
    
    int main()
    {
    
    // Prompt user with questions
    
    printf ("What is the month and day of your wedding?\n");
    scanf("%d %d", &month, &day);
    
    // Adjusting month and day values to align with array
    
    month = month -1;
    day = day - 1;
    
    printf("What file stores your city's temperature data?\n");
    scanf("%s", cityFileName);
    
    // Program will open the filename the user inputted
    
    FILE *ifp = fopen(cityFileName,"r");
    
    // Initialize the weather array to recieve temperature averages
    
    float weatherArray[12][31];
    int dummyMonth, dummyDay;
    
    for (dummyMonth=0; dummyMonth < 12; dummyMonth++)
        {
           for (dummyDay=0; dummyDay < 31; dummyDay++)
            {
                weatherArray[dummyMonth][dummyDay] = 0;
            }
        }
    
    // Read temperature file and store temperature averages to array
    
    float tempTemp;
    int dummyVar;
    float tempAverage = 0.0;
    
    fscanf(ifp, "%d", dummyMonth);
    printf("AFTER FSCANF\n");
    
    while (dummyMonth!= -1)
        {
            fscanf(ifp, "%d", dummyDay);
            fscanf(ifp, "%d", dummyVar);
            fscanf(ifp, "%f", tempTemp);
            if (tempTemp != -99)
            {
                tempAverage = (tempAverage + tempTemp)/2.0;
                weatherArray [dummyMonth][dummyDay] = tempAverage;
            }
            fscanf(ifp, "%d", dummyMonth);
        }
    
    // Calculate the average temperature for the month of the chosen wedding date
    
    int monthAverageTemp = 0;
    
        for (dummyDay=0; dummyDay < 31; dummyDay++)
            {
                monthAverageTemp = (monthAverageTemp + weatherArray[month][dummyDay]) / 2;
            }
    
    // Print calculations to the user
    
    printf("The average temperature on your wedding day is %.2f degrees F.\n",
           weatherArray[month][day]);
    
    printf("The average temperature on your wedding month is %.2f degrees F.\n",
           monthAverageTemp);
    
    // Condition that states whether wedding should be held outside or not
    
    if ((weatherArray[month][day] >= 60) && (weatherArray [month][day] <= 75))
        printf("The weather looks good for an outdoor wedding");
    else
        printf("It’s probably best to move the wedding indoors, sorry.");
    
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    -1 is supposed to denote the end of the txt file that contacts month day year and temperature information, in case anyone was unsure about the value.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while (dummyMonth!= -1)
        {
            fscanf(ifp, "%d", dummyDay);
            fscanf(ifp, "%d", dummyVar);
            fscanf(ifp, "%f", tempTemp);
            if (tempTemp != -99)
            {
                tempAverage = (tempAverage + tempTemp)/2.0;
                weatherArray [dummyMonth][dummyDay] = tempAverage;
    Try printing out Var and Day before you try accessing those elements in the array, so you can see if your numbers are a valid range for that array.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by somniferium View Post
    Good evening,

    This program complies, but right around the program starts reading the file I get the error that weather.exe has stopped working. I am at a loss as to why the program is not working Can anyone see what I have done wrong? Thanks.

    Code:
    #include <stdio.h>
    
    // Declare variables to recieve user input
    
    char cityFileName[20];
    int month, day;
    
    int main()
    {
    
    // Prompt user with questions
    
    printf ("What is the month and day of your wedding?\n");
    scanf("%d %d", &month, &day);
    
    // Adjusting month and day values to align with array
    
    month = month -1;
    day = day - 1;
    
    printf("What file stores your city's temperature data?\n");
    scanf("%s", cityFileName);
    
    // Program will open the filename the user inputted
    
    FILE *ifp = fopen(cityFileName,"r");
    
    // Initialize the weather array to recieve temperature averages
    
    float weatherArray[12][31];
    int dummyMonth, dummyDay;
    
    for (dummyMonth=0; dummyMonth < 12; dummyMonth++)
        {
           for (dummyDay=0; dummyDay < 31; dummyDay++)
            {
                weatherArray[dummyMonth][dummyDay] = 0;
            }
        }
    
    // Read temperature file and store temperature averages to array
    
    float tempTemp;
    int dummyVar;
    float tempAverage = 0.0;
    
    fscanf(ifp, "%d", dummyMonth);
    printf("AFTER FSCANF\n");
    
    while (dummyMonth!= -1)
        {
            fscanf(ifp, "%d", dummyDay);
            fscanf(ifp, "%d", dummyVar);
            fscanf(ifp, "%f", tempTemp);
            if (tempTemp != -99)
            {
                tempAverage = (tempAverage + tempTemp)/2.0;
                weatherArray [dummyMonth][dummyDay] = tempAverage;
            }
            fscanf(ifp, "%d", dummyMonth);
        }
    
    // Calculate the average temperature for the month of the chosen wedding date
    
    int monthAverageTemp = 0;
    
        for (dummyDay=0; dummyDay < 31; dummyDay++)
            {
                monthAverageTemp = (monthAverageTemp + weatherArray[month][dummyDay]) / 2;
            }
    
    // Print calculations to the user
    
    printf("The average temperature on your wedding day is %.2f degrees F.\n",
           weatherArray[month][day]);
    
    printf("The average temperature on your wedding month is %.2f degrees F.\n",
           monthAverageTemp);
    
    // Condition that states whether wedding should be held outside or not
    
    if ((weatherArray[month][day] >= 60) && (weatherArray [month][day] <= 75))
        printf("The weather looks good for an outdoor wedding");
    else
        printf("It’s probably best to move the wedding indoors, sorry.");
    
    
    return 0;
    
    }
    Ok... here we go...
    Move lines 5 and 6 inside your main() function. Global variables should be avoided when possible.
    Move lines 30 and 31 to the top of main()
    Move lines 43, 44 and 45 to the top of main() Not all compilers allow mid-stream declarations.
    On line 26... How do you know that file actually opened? You need to check the return value of fopen().

    Also your text formatting could use a little work...

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    Using printf's, I have determined that the program works up until it tries to scan numbers in cityFileName via fscanf. For cityFileName, I am inputting "filename.txt" which contains the proper data.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by somniferium View Post
    Using printf's, I have determined that the program works up until it tries to scan numbers in cityFileName via fscanf. For cityFileName, I am inputting "filename.txt" which contains the proper data.
    What folder is that file in?
    What folder is the program running in?

    If you are on Windows newer than XP Sp2 you should also go into control panel, folder options, view and turn off the "Hide extensions for known file types" option (stupidest idea Microsoft ever had, that)... you may discover your file is actually named filename.txt.txt

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    The compiled executable and text file are both in the same folder. My file extensions are visible.

    I have made all the changes you suggested, but I don't understand what you mean by my "text formatting could use a little work."

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by somniferium View Post
    The compiled executable and text file are both in the same folder. My file extensions are visible.
    So, is the file actually opening?

    Also from your original post...
    lines 47, 52, 53, 54 and 60... you probably need & (address of) before your variable names.


    I have made all the changes you suggested, but I don't understand what you mean by my "text formatting could use a little work."
    Just tidy up your indentation a little... Some spots are pretty uneven. It may not seem very important but when done correctly it's a lot easier to troubleshoot... Might as well form good habits from the git-go....

  9. #9
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    I thought my indentation was fairly readable. Can you give me a line example of what needs to be cleaned up.

    I added the & sign to the variables. I'm surprised it compiled without them, but I guess the compiler deals with fscanf variables differently than others.

    I put printf's into a section of the code and received 2621168, 2621168, and 2621164 as values... haha.

    I ignored cityFileName and had fopen open city.txt directly, which gave those weird values during the first run through of the while loop.

    Code:
    fscanf(ifp, "%d", &dummyMonth);
    
    while (dummyMonth!= -1)
        {
            fscanf(ifp, "%d", &dummyDay);
            printf("%d - ", &dummyDay);
            fscanf(ifp, "%d", &dummyVar);
            fscanf(ifp, "%f", &tempTemp);
            printf("%d - %d", &dummyDay, &dummyVar);

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok from your last bit... take the & out of your printf() calls... you're printing addresses, not values.

    scanf() needs the address to know where to stuff the data.
    printf() needs the value to know how to print the data.

  11. #11
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    Whoops. Thanks. It's 3am here... I'm losing it, haha.

    Latest version of my code. It is calling orlando.txt and running through the numbers once, then crashing:
    Code:
    #include <stdio.h>
    
    int main()
    {
    
    // Declare variables
    
    char cityFileName[20];
    int month, day, dummyMonth, dummyDay, dummyVar;
    float weatherArray[12][31], tempTemp;
    float tempAverage = 0.0;
    
    // Prompt user with questions
    
    printf ("What is the month and day of your wedding?\n");
    scanf("%d %d", &month, &day);
    
    // Adjusting month and day values to align with array
    
    month = month -1;
    day = day - 1;
    
    printf("What file stores your city's temperature data?\n");
    scanf("%s", cityFileName);
    
    // Program will open the filename the user inputted
    
    FILE *ifp = fopen("orlando.txt","r");
    
    // Initialize the weather array to recieve temperature averages
    
    for (dummyMonth=0; dummyMonth < 12; dummyMonth++)
        {
           for (dummyDay=0; dummyDay < 31; dummyDay++)
            {
                weatherArray[dummyMonth][dummyDay] = 0;
            }
        }
    
    // Read temperature file and store temperature averages to array
    
    fscanf(ifp, "%d", &dummyMonth);
    
    while (dummyMonth != -1)
        {
            fscanf(ifp, "%d", &dummyDay);
            printf("%d - ", dummyDay);
            fscanf(ifp, "%d", &dummyVar);
            fscanf(ifp, "%f", &tempTemp);
            printf("%d - %.2f", dummyDay, tempTemp);
            if (tempTemp != -99)
            {
                tempAverage = (tempAverage + tempTemp)/2.0;
                weatherArray [dummyMonth][dummyDay] = tempAverage;
            }
            fscanf(ifp, "%d", &dummyMonth);
        }
    
    // Calculate the average temperature for the month of the chosen wedding date
    
    int monthAverageTemp = 0;
    
        for (dummyDay=0; dummyDay < 31; dummyDay++)
            {
                monthAverageTemp = (monthAverageTemp + weatherArray[month][dummyDay]) / 2;
            }
    
    // Print calculations to the user
    
    printf("The average temperature on your wedding day is %.2f degrees F.\n",
           weatherArray[month][day]);
    
    printf("The average temperature on your wedding month is %.2f degrees F.\n",
           monthAverageTemp);
    
    // Condition that states whether wedding should be held outside or not
    
    if ((weatherArray[month][day] >= 60) && (weatherArray [month][day] <= 75))
        printf("The weather looks good for an outdoor wedding");
    else
        printf("It’s probably best to move the wedding indoors, sorry.");
    
    
    return 0;
    
    }
    Last edited by somniferium; 10-15-2011 at 01:31 AM.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... you STILL aren't checking to be sure that file actually opens...

    Code:
    ifp = fopen(filename,"r");
    if (! ifp)
     { printf("The file did not open");
        exit (255); }
    You need to check this stuff... error trapping, especially with I/O functions is very important to keep things sane...

    And exactly what do you mean by "running through the numers once then crashing" Exactly where is it crashing?

  13. #13
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    Professor hasn't taught or required us to check to make sure that the file opened, but I understand and agree with your point regarding why.

    I added the code and the txt file is definitely opening. The loop gets to the data for December 11 and stops: "12 - 11 - 48.70" and then gives the error that weather.exe has stopped working.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem is your dummyMonth variable. It's set to 12 when this happens:
    Code:
    while (dummyMonth != -1)
        {
            fscanf(ifp, "%d", &dummyDay);
            printf("%d - ", dummyDay);
            fscanf(ifp, "%d", &dummyVar);
            fscanf(ifp, "%f", &tempTemp);
            printf("%d - %.2f", dummyDay, tempTemp);
            if (tempTemp != -99)
            {
                tempAverage = (tempAverage + tempTemp)/2.0;
                weatherArray [dummyMonth][dummyDay] = tempAverage;
            }
            fscanf(ifp, "%d", &dummyMonth);
        }
    Because this just happened:
    Code:
    for (dummyMonth=0; dummyMonth < 12; dummyMonth++)
    Because for that loop to exit, dummyMonth isn't less than 12 anymore. That means you are trying to access weatherArray[ 12 ][ ... ] which is invalid. If you had been a little more generous with those printf statements you might have found it on your own.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by somniferium View Post
    Professor hasn't taught or required us to check to make sure that the file opened, but I understand and agree with your point regarding why.
    Trust me... any teacher who would begrudge a student the initiative to leap ahead and learn some extra stuff should not be teaching.

    All through my high school and later education I used summer holidays for the usual silly stuff kids do... but I also pre-read big chunks of my text books for the next year. By the time school stated I was reading mid-year material. My parents were right... it got a lot easier when I did and the teachers thought it was wonderful. (The other kids? Well, not so much)

    I added the code and the txt file is definitely opening. The loop gets to the data for December 11 and stops: "12 - 11 - 48.70" and then gives the error that weather.exe has stopped working.
    Quzah seems to have a handle on that so I'll leave you in his capable care...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. homework problem Please Help!
    By alexwink in forum C Programming
    Replies: 24
    Last Post: 10-27-2006, 08:20 AM
  2. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  3. One More Homework Problem
    By joxerjen in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2005, 04:39 PM
  4. homework problem
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 08-09-2002, 07:12 PM
  5. Homework problem
    By bacon in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 07:21 AM