Thread: Program Crashing - please help!

  1. #1
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16

    Program Crashing - please help!

    Everytime I go to run this it crashes. I think it might have something to do with opening the data file, but the error check I wrote in isnt catching anything so I am at a bit of a loss as to what is wrong. :/



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /*Declaring all Variables and pointers*/
    
    int FCOUNT, BCOUNT, ECOUNT, TOTTOTAL, TOT500, ERROR, NUMBER, HOURS;
    char NAME, *CLASS, CODE, BUFFER, *REASON;
    float CHARGE, GST, PST, TOTAL, AVGTOT;
    
    
    FILE *error, *data;
    
    
    int main()
    {
     /*Process Modules*/ 
       
          initialize ();
          DisplayHeadings ();
          GetValidRecord ();
          
          /*While Not End of File Process Records and Outputs*/
          
          while (!feof(data)){
                if (ERROR == 0){
                   ProcessRecord();
                   DisplayDetails ();
                         }
                GetValidRecord ();
                }   
                /*Process Summary Modules*/
                
         PrepareSummary ();
         DisplaySummary ();
         
               
        
    
          system("PAUSE");
          return 0;
    }
    
    int initialize ()
    {
        /*Initialize Counters*/
        FCOUNT = 0;
        BCOUNT = 0;
        ECOUNT = 0;
        TOTTOTAL = 0;
        TOT500 = 0;
        
        /*Open Error File*/
        error = fopen("error.dat" , "w+");
        
        /*Check for Error Creating Error File*/
        
              if(error==NULL)
              {
                        puts("Error Creating Error File!\n");
                                                                            
               }
        
        /*Open Customer Data File for Reading*/
                             
        data = fopen("cust.dat" , "r");
        
        /*Check for Error Opening Customer Data File*/
        
             if(data==NULL)
    
             { 
                        puts("Error Opening Customer Data File!\n");
              }
                        
    
    
    }
    
    int DisplayHeadings ()
    
    {
        /*Display Title*/
        
        printf("Fly By Night Airlines - Customer Billing System (NJC)\n++================================================++\n");
        
        /*Display Headers*/
        
        printf ("\n                               ($) Base   ($) GST   ($) PST    ($) Total\n");
        printf ("Customer Name    Class Type     Charge     Charge    Charge     Charge\n");
        printf ("++++++++++++++++ +++++++++++++ ++++++++++ +++++++++ ++++++++++ +++++++++++++\n\n") ;
         }
         
    int GetValidRecord ()
    
    {
        /*Process ReadRecord Module*/
        
        ReadRecord ();
        
        /*Initialize ERROR to Zero*/
        
        ERROR == 0;
        
        /*While its not the end of file run the ValidateRecord Module*/
        
              while (!feof(data)){
                ValidateRecord ();
                
                /*If ERROR is not 0, Process Writeto ErrorFile Module*/
                
                   if (ERROR != 0){
                   WriteToErrorFile();
                   }
                           }   
    
    
    }    
    
    int ProcessRecord ()
    
    {
      /*Process Modules to Determine the Total Charge*/
      
      DetermineBaseCharge ();
      CalculateTotals ();
      UpdateTotals ();
    }
    
    int DisplayDetails ()
    
    {
        /*Display all Details to Screen*/
        
        printf("%s", NAME);
        printf("%s", CLASS);
        printf("%f", CHARGE);
        printf("%f", GST);
        printf("%f", PST);
        printf("%f", TOTAL);
    
    }
    
    int PrepareSummary ()
    
    {
        /*Determine the Average Total*/
        
        if (FCOUNT + BCOUNT > 0)
           {
           AVGTOT = TOTTOTAL/(FCOUNT + BCOUNT + ECOUNT); }
        else
                  {
                  AVGTOT = 0;
                   }
                   
                   /*Close both files*/
        fclose(data);
        fclose(error);
    }
    
    int DisplaySummary ()
    
    {
        /*Display Summary of Counts and Averages*/
        
        printf("First Class Count: %d\n", FCOUNT);
        printf("Business Class Count: %d\n", BCOUNT);
        printf("Economy Class Count: %d\n", ECOUNT);
        printf("Average Total Charge: %d\n", AVGTOT);
        printf("Number of Customers Billed over $500.00: %d\n", TOT500);
    } 
    
    int ReadRecord ()
    
    {
        /*Read Record from Data File*/
        
         
         fscanf(data, "%s\n", NAME);
         fscanf(data, "%d\n", NUMBER);
         fscanf(data, "%d\n", CODE);
         fscanf(data, "%d\n", HOURS);
    }
    
    int ValidateRecord ()
    
    {
        /*Validate NUMBER, CODE and HOURS, Set ERROR*/
        ERROR = 0;
        if (NUMBER < 1138 || NUMBER > 5782)
           {
           ERROR =1; }
          else if (CODE != 'F' & CODE != 'B' & CODE != 'E')
               {
               ERROR = 2; }
                    else if (HOURS <= 0)
                    {
                    ERROR = 3;
                    }
    
    }
    
    
    int WriteToErrorFile ()
    
    {
        /*Determine Reason for Error and print Error Message to File*/
        
       if(ERROR = 1)
         {
         REASON = "ID Number Invalid";
         }
           else if(ERROR = 2)
           {
           REASON = "Class Code Invalid";
           }
             else if(ERROR = 3)
             {
             REASON = "Hours Invalid";
             }
             
        fprintf(error, "%s\n", NAME);
        fprintf(error, "%d\n", NUMBER);
        fprintf(error, "%d\n", CODE);
        fprintf(error, "%d\n", HOURS);
        fprintf(error, "%s\n", REASON);
    
    }
    
    int DetermineBaseCharge ()
    
    {
        /*Determine which Module to Process*/
        
        if (CODE = 'F')
          {
           ProcessFirstClass ();
           }
            else if (CODE = 'B')
            {
                 ProcessBusiness ();
                 }
         else 
         {ProcessEconomy ();}
                           
    }
    
    int CalculateTotals ()
    
    {
        /*Determine if there will be a discount*/
        if (HOURS > 100)
        {
                  CHARGE = CHARGE - CHARGE * 0.12;
                  }
          /*Calculate GST, PST and Total Charge*/
                  
        GST = CHARGE * 0.07;
        PST = CHARGE * 0.08;
        TOTAL = CHARGE + GST +PST;          
    }
    
    int UpdateTotals ()
    
    {
        /*Update Counters*/
        if (CODE = 'F')
        {
            FCOUNT = FCOUNT + 1;
            }
            else if (CODE = 'B')
                 {
                  BCOUNT = BCOUNT + 1;
                  }
                  else 
                  {ECOUNT = ECOUNT + 1;} 
                  
          /*update TOTTOTAL*/     
             
         TOTTOTAL = TOTTOTAL + TOTAL;
         
         /*update TOT500 Counter*/
         
         if (TOTAL > 500)
         {
                   TOT500 = TOT500 + 1;
                   }        
    }
    
    int ProcessFirstClass ()
    {
        /*Assign Class with "First Class"*/
        
        CLASS = "First class";
        
        /*Determine what to charge according to HOURS*/
        
        if (HOURS <= 10)
        {
                  CHARGE = 70*HOURS;
                  }
                  else
                  {
                      CHARGE = 10*70+(HOURS-10)*65;
                      }
        
        
    }
    
    int ProcessBusiness ()
    {
        /*Assign CLASS with "Business"*/
        
        CLASS = "Business";
        
        /*Determine what to charge according to HOURS*/
        
        if (HOURS <= 20)
        {
                  CHARGE = 60*HOURS;
                  }
                  else
                  {
                      CHARGE = 20*60+(HOURS-20)*55;
                  }
    }
    
    int ProcessEconomy ()
    
    {
        /*Determine what to charge according to HOURS*/
        CLASS = "Economy";
        CHARGE = 45*HOURS;
        
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Crank up the warnings on your compiler. Fix all of them -- then run it. If you have questions about the messages, post the messages with your question.
    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.*

  3. #3
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    I'm using Dev-C++ and can't find an option to show all warnings, I was working on the assumption that it just did :/

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Let me know if this is out of date.
    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.*

  5. #5
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    Yea thats a lot better - other then getting warnings from the header libaries. Thanks a lot

    It should take me awhile to fix everything I did wrong. :/

  6. #6
    Registered User Surfin_Bird's Avatar
    Join Date
    Jan 2005
    Location
    Kanada
    Posts
    16
    Ok I think I found the problem ... It is in the ReadRecord Function, for some reason everytime it goes to scan the integers from the file it crashes the program. When I comment those lines out it doesnt crash although the program still doesnt work.

    I spent hours last night fixing little problems and then a few more hours looking through the posts on here and various websites to try and figure out what is wrong.

    (I put in printf in this function just to test if it is scanning right but I am removing them later as there is a whole other function in my program that does this.)

    Is there a better way to get numbers from a file?


    Code:
    void ReadRecord ()
    
    {
         /*Read info from the file*/
    
         fscanf(data, "%s", NAME);
         printf("%s", NAME);
         fscanf(data, "%s", LNAME); 
         printf("%s", LNAME);
        
         fscanf(data, "%d", NUMBER);
         printf("%d", NUMBER);
         fscanf(data, "%d", CODE);
         printf("%d", CODE);
         fscanf(data, "%d", HOURS);
         printf("%d", HOURS);
         
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Surfin_Bird
    Ok I think I found the problem ... It is in the ReadRecord Function, for some reason everytime it goes to scan the integers from the file it crashes the program. When I comment those lines out it doesnt crash although the program still doesnt work.

    I spent hours last night fixing little problems and then a few more hours looking through the posts on here and various websites to try and figure out what is wrong.

    (I put in printf in this function just to test if it is scanning right but I am removing them later as there is a whole other function in my program that does this.)

    Is there a better way to get numbers from a file?


    Code:
    void ReadRecord ()
    
    {
         /*Read info from the file*/
    
         fscanf(data, "%s", NAME);
         printf("%s", NAME);
         fscanf(data, "%s", LNAME); 
         printf("%s", LNAME);
        
         fscanf(data, "%d", &NUMBER);
         printf("%d", NUMBER);
         fscanf(data, "%d", &CODE);
         printf("%d", CODE);
         fscanf(data, "%d", &HOURS);
         printf("%d", HOURS);
         
    }
    I would have thought that gcc would have pointed that out. Strings need more than one character, and a pointer that doesn't point to memory you can use won't cut it.
    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. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  3. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM