Thread: NEED HELP READING FILE and PRINTING

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    6

    Lightbulb NEED HELP READING FILE and PRINTING

    I have written this code to read a file with the uses of arrays input implement the data and print the data as follows. I have no compile errors, i just have troubles reading and printing the file, i dunno if its my fscanf or print details. someone please help. its really ........ing me off.

    the .txt file is:

    2009_wbb_stats.txt

    01-08 80-76 2190
    01-11 81-65 6090
    01-18 70-61 2107
    01-22 84-66 4326
    01-25 82-68 12067
    01-29 58-67 4119
    02-01 66-55 6887
    02-05 72-65 3266
    02-08 81-54 3696
    02-12 81-69 3216
    02-15 63-58 2478
    02-19 70-73 5295
    02-22 65-59 7006
    03-01 94-57 5616


    Code:
    #include <stdio.h>
    #include <math.h>
    #define FILENAME "2009_wbb_stats.txt"
    #define G 20
    
      int readStats (int month[], int day[], int auburn[], int opp[], int attendance[]);
        
      void printReport (int month[], int day[], int auburn[], int opp[], int game, int attendance[]);
        
      int printDetails (int month[], int day[], int auburn[], int opp[], int game, int attendance[]);
        
      void printTop ();
        
      void printSummary ( int auburn[], int opp[], int game, int win);
        
      double mean (int attendance[], int game);
      int maxi (int attendance[], int game);
      int mini (int attendance[], int game);
        
        
       int main (void)
      {
         int month[G], day[G], auburn[G], opp[G], attendance[G];
         int game;
       
         game = readStats( month, day, auburn, opp, attendance);
       
         printReport( month, day, auburn, opp, game, attendance);
       
      }
        
        //reads data from text file
        
       int readStats (int month[], int day[], int auburn[], int opp[], int attendance[])
      {
         int x=0;
         char dash;
         FILE *inFile;
       
         inFile = fopen(FILENAME, "r");
       
         if(inFile == NULL)
            printf ("Error opening file.");
           
         else
         { 
            while( fscanf(inFile,"%d%c%d %d%c%d %d", &month[x], &dash, &day[x], &auburn[x], &dash, &opp[x], &attendance[x]) != EOF) x++;
         
         }
         return x;
           
      }
      
       double mean (int attendance[], int game)
      {
         int h;
         double sum=0;
       
         for (h=0; h<attendance[h]; h++)
         {
            sum += attendance[h];
         }
       
         return sum/h;
      }
      
       int printDetails (int month[], int day[], int auburn[], int opp[], int game, int attendance[])
      {
         int var, max1, min1, b, n;
         char num1, num2;
         n = mean(attendance, game);
         max1 = max( attendance, game);
         min1 = min( attendance, game);
         double win = 0;
         char Win[G];
       
         if (auburn [b]> opp[b])
         {
            Win[b] = 'W';
            win++;
         }
         
         else if (auburn[b] < opp[b])
            Win[b] = 'L';
           
         for (b=0; b<game; b++)
         {
            printf("%d/%d %d/%d %c %d", month[b], day[b], auburn[b], opp[b], Win[b], attendance[b]);
         }
       
         if (attendance[b]>n)
            printf("*");
         if (attendance[b]==max1)
            printf("+");
         if (attendance[b]==min1)
            printf("-");
           
         printSummary( auburn, opp, game, win);
       
         return var;
       
      }
      
       void printReport( int month[], int day[], int auburn[], int opp[], int game, int attendance[])
      
      {
         printTop();
         printf("this program is wild");
         printDetails( month, day, auburn, opp, game, attendance);
       
      }
        
       void printSummary( int auburn[], int opp[], int game, int win)
      {
         int b;
       
         printf("\nAuburn has won %d of its %d games.", win, game);
         printf("\nAuburn's average score is %d.", auburn[b]);
         printf("\nThe opponents' average score is %d", opp[b]);
         printf("\n*attendance is above average");
         printf("\n+high attendance");
         printf("\n-low attendance");
       
      }
      
       int max( int attendance[], int game)
      {
         int b;
         int max;
       
         for (b=0; b<game; b++)
         {
            if (attendance[b] > max)
               max = attendance[b];
           
         }
       
         return max;
      }
      
       int min( int attendance[], int game)
      {
         int b;
         int min;
       
         for(b=0; b<game-1; b++)
         {
            if (attendance[b] < min)
               min = attendance[b];
         
         }
       
         return min;
      
      }
        
       void printTop()
      {
         printf("Date  Score     Result  W/L  Attendance\n");
         printf("---------------------------------------\n");
       
      }

  2. #2
    Registered User
    Join Date
    Apr 2009
    Posts
    6
    the only display i get is

    ----jGRASP exec: C:\Users\Geoffro\Documents\a.exe

    Date Score Result W/L Attendance
    ---------------------------------------

    ----jGRASP cygwin wedge2: process died on signal 11.
    ----jGRASP: operation complete.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why don't you debug your functions one at a time? Start scattering printf statements around to show you where you are in the program, and what the data is.


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

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Don't you want h < game in mean?

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    In your printDetails function, the variable b is not initialised and when you try to use that variable to index through the auburn[b] and many other subsequent array it end up with the Seg Fault. Try initialize the b value to 0 or anything og your requirnment, things should work.

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading and printing a file
    By T1m in forum C Programming
    Replies: 1
    Last Post: 01-08-2009, 01:29 PM
  2. Replies: 8
    Last Post: 12-06-2008, 02:43 PM
  3. reading a text file printing line number
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 09-16-2005, 10:31 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM