Thread: help! reading from file and computing average

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I suggest this:
    Code:
    while((fscanf(tempsIn, "%lf", &nextVal)) != EOF) {
          tempcount++;
          total += nextVal;
    }
    All files you've open, should be closed after they've been processed. How do you know fclose() is not working?

  2. #17
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    i was using that code before, i already changed the format of my program, that won't work. i have added fclose to my program but that didn't change anything. come on im not asking anyone to write my code for me...i already wrote it. is there really no one that can help me??

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by cakestler View Post
    i was using that code before, i already changed the format of my program, that won't work. i have added fclose to my program but that didn't change anything. come on im not asking anyone to write my code for me...i already wrote it. is there really no one that can help me??
    If you had been using that form, then you wouldn't have a problem. If you insist on using feof to control your loop, then no you cannot be helped, as the fix is not to use feof. fclose is completely irrelevant to your problem.

  4. #19
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    sure when i use that form it does fix the problem of counting everything correctly, but it messes up everything else

  5. #20
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    i changed it but it still doesn't work can someone PLEASSEE!!! help me!!! i dont understnad what i am doing wrong

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    
    {
      char inFileName[160];
      int x;
      int tempcount = 0;
      int max = 0;
      int min = 10000;
      double sum = 0;
      FILE *tempsIn;
      
       printf("Temperature Analyzer v1.1\n");
       printf("\n");
       printf("What file to analyze?\n");
       scanf("%s",&inFileName);
      
       tempsIn = fopen("c:\\documents and settings\\christina kestler\\desktop\\temps.txt", "r");
     
       if (inFileName == NULL) {
       printf("File could not be opened.\n");
       system("pause");
       exit(EXIT_FAILURE);    
      }
      
      while((fscanf(tempsIn, "%lf", &sum)) != EOF) {
        tempcount++;
        sum += x;      
       
        if(x > max)
        max = x;
        
        if(x < min)
        min = x;
    }
      
      fclose(tempsIn);
      
      double avg = (sum / tempcount);
      
      printf("# of daily temps: %d\n", tempcount);
      printf("Average daily temp: is %0.5lf\n", avg);
      printf("Highest temp: %d\n", max);
      printf("Lowest temp: %d\n", min);
      
      system("PAUSE");	
      return 0;
    }

  6. #21
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    i got it....looks like things were messed up with my file NOT my code....it was fine using feof!!!

  7. #22
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Sympathies for your frustration. I just got back, and I'll take a look at it right now, and see what's up.

    Stay around, if you can.

    Well, never mind - and congrats.

    Feof does work fine - sometimes.

    The one I posted works fine - all the time.

  8. #23
    Registered User
    Join Date
    Feb 2009
    Posts
    36
    thank you for being understanding, i am new at this and have been working on this code now for about 6 hours yesterday and about 5 now today...i was reaching my breaking point. thank you for your help!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doxygen failing
    By Elysia in forum A Brief History of Cprogramming.com
    Replies: 19
    Last Post: 04-16-2008, 01:24 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. reading pictures and array
    By sunoflight77 in forum C++ Programming
    Replies: 0
    Last Post: 05-09-2005, 03:16 PM
  4. a file problem !!
    By girlzone in forum C++ Programming
    Replies: 17
    Last Post: 04-15-2003, 07:55 PM
  5. Counting # of elements in array or file.
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 05:33 AM