Thread: Program crashes and I can't figure out why

  1. #1
    Unregistered
    Guest

    Unhappy Program crashes and I can't figure out why

    I pulled out an old program from school and started updating it because I was bored. It may be because I was using a dinosaur of a compiler when I was in school, but now every time I try to run the program it either crashes before starting, crashes after I select option 1, or crashes my computer right out.

    If anyone can see something that I can't, let me know. I have a big problem finding problems in code that I write :P

    (Note: I cut out most of it and only included the parts that are troublesome due to space constraints)
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    /*struct definitions*/
    typedef struct{
      char First[16];
      char Last[16];
    }Cust_Name;
    
    typedef struct{
      Cust_Name Name;
      long int Number;
      char Line1[26];
      char Line2[26];
      char Line3[26];
      char Line4[26];
      char City[16];
      char State[3];
      long int Zip;
      int Num_Lines;
    }Cust_Info;
    
    /*function prototypes*/
    void Read(Cust_Info *x);
    //void Search(Cust_Info *x);
    //void Update(Cust_Info *x);
    //void Sort(Cust_Info *x);
    //void Print(Cust_Info *x);
    
    int main(){
      Cust_Info Rec[30];
      int selection;
    
      do{
        printf("1. Read customer information data file.\n"
    	   "2. Search for customer record.\n"
    	   "3. Update customer information data file.\n"
    	   "4. Sort customer records.\n"
    	   "5. Print mailing labels.\n"
    	   "6. End Program.\n"
    	   "Choose an option (1-6): ");
        scanf("%d", &selection);
        switch(selection){
          case 1:
            Read(Rec);
    	break;
          case 2:
            //Search(Rec);
    	break;
          case 3:
            //Update(Rec);
    	break;
          case 4:
            //Sort(Rec);
    	break;
          case 5:
    	//Print(Rec);
    	break;
          case 6:
            exit(0);
    	break;
          default:
    	printf("Invalid selection\n");
    	break;
        }
      }
      while(selection!=6);
      return 0;
    }
    
    /*read in the data from a file*/
    void Read(Cust_Info *x){
      FILE *in;
      int i, j, k;
    
      /*open the file for input-check for errors*/
      if((in=fopen("a:\Supply.dat", "r"))==NULL){
        printf("Error opening file!");
        exit(0);
      }
    
      /*input data*/
      i=0;
      do{
        fscanf(in, "%ld%s%s%s%s%ld%d", &x[i].Number, &x[i].Name.Last,
    	       &x[i].Name.First, &x[i].City, &x[i].State, &x[i].Zip,
    	       &x[i].Num_Lines);
        /*change state to upper case if not already*/
        for(j=0; j<2; j++){
          if(x[i].State[j] >= 'a' && x[i].State[j] <= 'z'){
    	x[i].State[j] += 'A'-'a';
          }
        }
        for(j=0; j<26; j++){
          x[i].Line1[i] = '\0';
          if(x[i].Num_Lines > 1) {x[i].Line2[i] = '\0';}
          if(x[i].Num_Lines > 2) {x[i].Line3[i] = '\0';}
          if(x[i].Num_Lines > 3) {x[i].Line4[i] = '\0';}
        }
        /*Read the first address line (not optional)*/
        j=-1;
        do{
          j++;
          fscanf(in, "%c", &x[i].Line1[j]);
          if(x[i].Line1[j]=='\n'){
    	x[i].Line1[j]='\0';
          }
        }
        while(x[i].Line1[j]!='\0');
        /*read the second line if present*/
        j=-1;
        if(x[i].Num_Lines > 1){
          do{
    	j++;
    	fscanf(in, "%c", &x[i].Line2[j]);
    	if(x[i].Line1[j]=='\n'){
    	  x[i].Line2[j]='\0';
    	}
          }
          while(x[i].Line2[j]!='\0');
        }
        /*read the third line if present*/
        j=-1;
        if(x[i].Num_Lines > 2){
          do{
    	j++;
    	fscanf(in, "%c", &x[i].Line3[j]);
    	if(x[i].Line3[j]=='\n'){
    	  x[i].Line3[j]='\0';
    	}
          }
          while(x[i].Line3[j]!='\0');
        }
        /*read the last line if present*/
        j=-1;
        if(x[i].Num_Lines > 3){
          do{
    	j++;
    	fscanf(in, "%c", &x[i].Line4[j]);
    	if(x[i].Line4[j]=='\n'){
    	  x[i].Line4[j]='\0';
    	}
          }
          while(x[i].Line4[j]!='\0');
        }
      }
      while(!feof(in));
    }
    -Prelude

  2. #2
    Unregistered
    Guest

    umm

    Quite a long post for a homework question that your checking out out of bordem. You've had 11 views and this reply.

    hmmmm

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Just some points about the code that I don't understand...

    Code:
    for(j=0; j<26; j++){
          x[i].Line1[i] = '\0';
          if(x[i].Num_Lines > 1) {x[i].Line2[i] = '\0';}
          if(x[i].Num_Lines > 2) {x[i].Line3[i] = '\0';}
          if(x[i].Num_Lines > 3) {x[i].Line4[i] = '\0';}
        }
    Am pretty sure this isn't intentional, but don't see why it'd crash anything.

    Code:
    if(x[i].Line1[j]=='\n'){
    	  x[i].Line2[j]='\0';
    	}
    Hehe, this could cause crash... but I'm not so sure it would.
    Last edited by QuestionC; 09-19-2001 at 04:59 PM.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    hmm, well, maybe no one can figure it out.

    I have no idea what you were trying to say, but thanks for at least pretending you cared.

    -P

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    For QuestionC:
    Code:
    for(j=0; j<26; j++){
          x[i].Line1[i] = '\0';
          if(x[i].Num_Lines > 1) {x[i].Line2[i] = '\0';}
          if(x[i].Num_Lines > 2) {x[i].Line3[i] = '\0';}
          if(x[i].Num_Lines > 3) {x[i].Line4[i] = '\0';}
        }
    In this I'm setting every element of all four lines to '\0', that way I don't have trash in there when I print them out, and it helps with the way I was inputting the data to each array.
    Code:
    if(x[i].Line1[j]=='\n'){
          x[i].Line2[j]='\0';
        }
    If the current character is a return character, change it to a null. In the original program, I was printing out these arrays character by character and the null character was my stopping condition.

    -Prelude

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    I don't think I was forceful enough the first time.

    The first code is wrong. The variable j does not occour at any point inside the for loop.

    For the second code, it checks to see if the current position in Line1 is '\n', but it's the code for Line2.

    Of course, I'm not sure there aren't more errors in there. The only way to really know is to go through it with a debugger.

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You're right, originally I used the variable i as the inner counter and n for the structure array. I forgot to change that part as well as the Line1 Line2 thing.

    I thought you might be right though that didn't sound like something that would crash a program and tried it, it still has the same problem but now when I get it working again I won't have crazy output ;p

    What's really sad is that you pointed out the code with errors and I STILL didn't see them :P

    -Prelude
    Last edited by Prelude; 09-19-2001 at 05:38 PM.

Popular pages Recent additions subscribe to a feed