C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-16-2009, 01:54 PM   #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");
   
  }
geoffr0 is offline   Reply With Quote
Old 04-16-2009, 01:59 PM   #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.
geoffr0 is offline   Reply With Quote
Old 04-16-2009, 03:17 PM   #3
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,262
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.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 04-16-2009, 04:55 PM   #4
Registered User
 
Join Date: Sep 2006
Posts: 2,512
Don't you want h < game in mean?
Adak is offline   Reply With Quote
Old 04-16-2009, 05:26 PM   #5
Protocol Test Engineer
 
ssharish2005's Avatar
 
Join Date: Sep 2005
Location: fseek(UK)
Posts: 1,316
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
ssharish2005 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading and printing a file T1m C Programming 1 01-08-2009 01:29 PM
printing "\n" every ten characters and also at the end of a file. Sgemin_001 C Programming 8 12-06-2008 02:43 PM
reading a text file printing line number bazzano C Programming 4 09-16-2005 10:31 AM
System drdroid C++ Programming 3 06-28-2002 10:12 PM
simulate Grep command in Unix using C laxmi C Programming 6 05-10-2002 04:10 PM


All times are GMT -6. The time now is 08:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22