Thread: need some help on this program.

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    1

    need some help on this program.

    hi,
    im just working on a program write now which im having some difficulty with. this is probably because i dont think i fully understand while loops and counts and stuff.i started writin the code jus for the hot days part of the question to test if the concept was working but even that hasnt worked and also EQF is coming up as an error.any help and suggestions would be greatly appreciated!thank you so much.

    heres the Q problemand underneath is my code)the problem is as follows:
    write a program to process a collection of daily high temperatures.your program should count and print the number of hot days(85 and higher),pleasant days(60-84) and the number of cold days(less than 60)

    use the following datainput.txt)
    55 62 68 74 59 45 41 58 60 67 65 78 82 88 91 92 90 93 87 80.

    [CODE]
    #include <stdio.h>

    int
    main(void)
    {
    int temperature,
    temperature_input,
    hot_days,
    pleasant_days,
    cold_days;

    FILE *inp;

    inp=fopen("a4q1temp.txt","r");

    printf ("Temperatures\n");

    temperature_input=fscanf (inp,"%d",&temperature);
    while (temperature_input != EQF && temperature > 85) {
    printf("%d",temperature);
    hot_days=hot_days+1;
    temperature_input=fscanf(inp,"%d",&temperature);
    return (hot_days);
    }

    printf ("\nThe number of hot days (high temperature of 85 and higher) is: %d\n",hot_days);

    fclose (inp);

    system("pause");
    return 0;
    }


    [CODE]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > return (hot_days);
    Just remove that line - it should do something then

    Then add
    if ( temp >= 85 ) hot_days = hot_days + 1;
    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
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >also EQF is coming up as an error

    It should be EOF.

    [EDIT]
    But then again, it might be better to do this.
    Code:
    while ( fscanf (inp, "%d", &temperature) == 1 )
    {
       /* ... */
    }
    And you'll probably want to initialize 'hot_days', etc. to 0.
    [/EDIT]
    Last edited by Dave_Sinkula; 10-21-2003 at 03:33 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM