1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Program stops unexpectedly!

Discussion in 'C' started by jose_peeterson, Jun 18, 2012.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Hi all,
    There is a lot of code below but i believe only a small excerpt is causing the problem. In the main function after security is cleared using (username = 1 and password = 1). I use an infinite while loop to capture new operations (denoted by the variable op) using the scanf function. This works fine the first time but after the first loop completes the program quits. I would like it to remain inside this while loop unitill the user exits by pressing ctrl c.

    The problem (part of the main function.)

    Code:
    
    while(1)
       {
        fflush(stdin);                    
        printf("\n\tPress    1   - ADD New MEMBER\n\n");
        printf("\tPress    2   - DELETE a MEMBER\n\n");
        printf("\tPress    3   - VIEW All members\n\n");
        printf("\tPress ctrl c - EXIT\n\n\n");
        printf("You Entered --> : ");
        scanf("%d",&op);    
        
    /*     if(op == 1)
          {                                       // read from keyboard
             printf("\nEnter FULL NAME with _ for spaces AND(after a space) Today's date <dd/mm//yy>.\n");
             fflush(stdin);
             fgets(members_init,58,stdin);          
             fflush(stdin);
                                                                  
             store_in_memfile(members_init);      // append to members.txt file   
                                                          
          }
          
         if(op == 2) 
          {      
            printf("\n\tWARNING : You are about to DELETE a member.\n");
            printf("\nEnter the FULL NAME with _ for spaces.\n");
            fflush(stdin);
            scanf("%s",members_name);
           
            delete_from_memfile(members_name,member);    // delete a member, sending MEMBER NAME ONLY
            
          }
          
         if(op == 3) 
          {
           show();
          
          }
         if(op == 4) 
          {
          
          }
          
         total_members = update_total_members();       // update total_membters after store or delete members, printf("\n\ntotal members = %d",total_members);
         
    */     load_members(member);                         // load members NAME and JOINED DATE from file into structure
        /* test*/
           
         
       
       }
    
    
    system("pause > null");
    return 0;
    }                                                  // END OF MAIN  
    
    
    

    The full main program.
    Code:
    
                   // START of MAIN
    int main()
    {
      char username[10],password[10];
      int op=1;
      
      char members_init[58],members_det[58];             // name and joined_date                                                                       
      char members_name[50];                             // name to DELETE
      int total_members;                                 // takes the value of line counter
      struct members member[1000];
      
      printf("\n\t\tCalvary Life Assembly Digital Library\n\n\n");
      
      while(1)                                      // security while loop
       {
        printf("Please enter your USERNAME and PASSWORD\n");
        strcpy(username,"x");                       // clear the u_n and p_w    
        strcpy(password,"x");
        scanf("%s%s",username,password); 
        if(security(username,password))
         {
         continue;
         }
        else
         {
          printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");  // hide the p_w
          printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
          printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
          printf("\nSECURITY CLEARED!\n\n"); 
          break;
         }
       }                                            // security while loop
     
     
      printf("\n\t\tCalvary Life Assembly Digital Library\n\n\n\n");
     
      while(1)
       {
        fflush(stdin);                    
        printf("\n\tPress    1   - ADD New MEMBER\n\n");
        printf("\tPress    2   - DELETE a MEMBER\n\n");
        printf("\tPress    3   - VIEW All members\n\n");
        printf("\tPress ctrl c - EXIT\n\n\n");
        printf("You Entered --> : ");
        scanf("%d",&op);    
        
    /*     if(op == 1)
          {                                       // read from keyboard
             printf("\nEnter FULL NAME with _ for spaces AND(after a space) Today's date <dd/mm//yy>.\n");
             fflush(stdin);
             fgets(members_init,58,stdin);          
             fflush(stdin);
                                                                  
             store_in_memfile(members_init);      // append to members.txt file   
                                                          
          }
          
         if(op == 2) 
          {      
            printf("\n\tWARNING : You are about to DELETE a member.\n");
            printf("\nEnter the FULL NAME with _ for spaces.\n");
            fflush(stdin);
            scanf("%s",members_name);
           
            delete_from_memfile(members_name,member);    // delete a member, sending MEMBER NAME ONLY
            
          }
          
         if(op == 3) 
          {
           show();
          
          }
         if(op == 4) 
          {
          
          }
          
         total_members = update_total_members();       // update total_membters after store or delete members, printf("\n\ntotal members = %d",total_members);
         
    */     load_members(member);                         // load members NAME and JOINED DATE from file into structure
        /* test*/
           
         
       
       }
    
    
    system("pause > null");
    return 0;
    }                                                  // END OF MAIN           
    
    
    
    
    
    the full program.

    Code:
    #include<iostream.h>
    #include<stdio.h>
    #include<string.h>
    
    struct borrowed
    {
     char member_name[50];                 // to access from borrowed.txt file         
     char items_name[150];
     char type;
     char due_date[8];               
    };
    
    struct members
    {
     char name[50];
     char joined_date[8];
     struct borrowed borrowed_items[5];
    };
    
                                                                                                                            
    int security(char username[10],char password[10]);  // fucntion prototypes
    void store_in_memfile(char members_init[58]);       // stores name and joined date to file
    void delete_from_memfile(char members_name[50],struct members member[1000]);    // deletes the member
    int update_total_members(void);
    int load_members(struct members member[1000]);     // ONLY loads NAME and JOINED DATE to structure members, RETURNS total number of members 
    void show(void);
    
                    // START of MAIN
    int main()
    {
      char username[10],password[10];
      int op=1;
      
      char members_init[58],members_det[58];             // name and joined_date                                                                       
      char members_name[50];                             // name to DELETE
      int total_members;                                 // takes the value of line counter
      struct members member[1000];
      
      printf("\n\t\tCalvary Life Assembly Digital Library\n\n\n");
      
      while(1)                                      // security while loop
       {
        printf("Please enter your USERNAME and PASSWORD\n");
        strcpy(username,"x");                       // clear the u_n and p_w    
        strcpy(password,"x");
        scanf("%s%s",username,password); 
        if(security(username,password))
         {
         continue;
         }
        else
         {
          printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");  // hide the p_w
          printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
          printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
          printf("\nSECURITY CLEARED!\n\n"); 
          break;
         }
       }                                            // security while loop
     
     
      printf("\n\t\tCalvary Life Assembly Digital Library\n\n\n\n");
     
      while(1)
       {
        fflush(stdin);                    
        printf("\n\tPress    1   - ADD New MEMBER\n\n");
        printf("\tPress    2   - DELETE a MEMBER\n\n");
        printf("\tPress    3   - VIEW All members\n\n");
        printf("\tPress ctrl c - EXIT\n\n\n");
        printf("You Entered --> : ");
        scanf("%d",&op);    
        
    /*     if(op == 1)
          {                                       // read from keyboard
             printf("\nEnter FULL NAME with _ for spaces AND(after a space) Today's date <dd/mm//yy>.\n");
             fflush(stdin);
             fgets(members_init,58,stdin);          
             fflush(stdin);
                                                                  
             store_in_memfile(members_init);      // append to members.txt file   
                                                          
          }
          
         if(op == 2) 
          {      
            printf("\n\tWARNING : You are about to DELETE a member.\n");
            printf("\nEnter the FULL NAME with _ for spaces.\n");
            fflush(stdin);
            scanf("%s",members_name);
           
            delete_from_memfile(members_name,member);    // delete a member, sending MEMBER NAME ONLY
            
          }
          
         if(op == 3) 
          {
           show();
          
          }
         if(op == 4) 
          {
          
          }
          
         total_members = update_total_members();       // update total_membters after store or delete members, printf("\n\ntotal members = %d",total_members);
         
    */     load_members(member);                         // load members NAME and JOINED DATE from file into structure
        /* test*/
           
         
       
       }
    
    
    system("pause > null");
    return 0;
    }                                                  // END OF MAIN           
    
    
    int security(char username[10],char password[10]) // 0 is correct and 1 is wrong
    {
      if(!(strcmp(username,"1")) && !(strcmp(password,"1")))
       return 0;
      else
       return 1; 
    }
    
    void store_in_memfile(char members_init[58])
    {
     FILE *members;
     
     members = fopen("members.txt","a");
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return;
      }
                                                                
     fprintf(members,"%s",members_init);        // add the new member              
     fclose(members);  
                            
    return;                             
    }
    
    int update_total_members(void)
    {
     FILE *members;
     int line_no=0;
     char members_det[58];
     
     members = fopen("members.txt","r");
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return 0;
      }
    
     while(fgets(members_det,58,members))       // update total_members
      {
      line_no = line_no + 1;  
      }  
                                                                
     fclose(members); 
                              
    return line_no;                             // update total_members 
    }
    
    void delete_from_memfile(char members_name[50],struct members member[1000])
    {                                           // members_name = MEMBERS NAME ONLY.          
     char members_det[58];                      // this string reads a line from file (here DUMMY)
     int total_members,ctr,i=1;                 // ctr is the line number in file.
     char members_del[58];                      // holds name and joined_date together from struct
     
     total_members = load_members(member);                     // load the structure from file
     
     FILE *members;
     
     members = fopen("members.txt","w");                       // write to file the DELETED member
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return;
      }
       
      for(ctr=0;ctr<total_members;ctr++)
       {
         members_del[0] = '\0';                          // clears the string
                                                                            
        if(!(strcmp(members_name,(member[ctr].name))))   // use ctr as line number to write
         {
                                                         // ctr as member to be deleted.
            strcpy((member[ctr].name),"-");
            strcpy((member[ctr].joined_date),"-\n");             
                                                         /* test printf("\n%s%s\n",(member[ctr].name),(member[ctr].joined_date));*/     
         }
                                                         /* test else printf("\n%s%s\n",(member[ctr].name),members_name); */
                                                
          strcat(members_del,(member[ctr].name));        // here do the string concatanate for ALL strings.
          strcat(members_del," ");
          strcat(members_del,(member[ctr].joined_date));
           
          fprintf(members,"%s",members_del);             // write line by line(ctr as line number) to file
       
                                                         /* test printf("%s\n",members_del); */
          members_del[0] = '\0';                         // clears the string
       }
                                                         /* test printf("%s\n",members_del); */
     fclose(members);   
     
    total_members = load_members(member);                // load the structure from file
    return;
    }
    
    
    int load_members(struct members member[1000])               // loads ONLY NAME and JOINED DATE to structure
    {
     FILE *members;
     char members_det[58];                                      // a line from file
     char *temp_name,*temp_joined_date;                         // strings to hold PARSE tokens
     int i=0;                                                   // struct counter
     
     members = fopen("members.txt","r");
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return 0;
      }
      
     while(fgets(members_det,58,members))       // load a line into members_det string
      {
        temp_name = strtok(members_det," ");    // parse the string members_det into tokens, first token is from 1st char to " ".
        temp_joined_date = strtok(NULL,"");     // second token(string) is from char after " " to end of word/line.
        
        strcpy((member[i].name),temp_name);
        strcpy((member[i].joined_date),temp_joined_date);
        i++;
      }  
                                                // printf("\n%s  %s\n",(member[9].name),(member[9].joined_date));
                                                                     
     fclose(members); 
     
    return i;
    }
    
    void show(void)
    {
     char members_det[58];                                      // a line from file              
     FILE *members;              
     
     members = fopen("members.txt","r");
     
     if(members == NULL)
      {
       printf("\n\tWARNING : UNABLE to open members.txt\n");
       return;
      }
      
        printf("\n\n\n\t\tNAME   JOINED-DATE\n");
        
      while(fgets(members_det,58,members))             // load a line into members_det string
       {   
        printf("\t\t%s",members_det);
     
       }
        printf("\n\n");
    
    fclose(members);
    return;
    }
    
    
    
    
    
     
  2. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    i think the problem is with the load_members function.
     
  3. priyatendulkar

    priyatendulkar New Member

    Joined:
    Jun 20, 2011
    Messages:
    20
    Likes Received:
    1
    Trophy Points:
    0
    I tested your code with some fixes and it worked fine..

    Fix 1 :while(1)
    {
    fflush(stdin);
    printf("\n\tPress 1 - ADD New MEMBER\n\n");
    printf("\tPress 2 - DELETE a MEMBER\n\n");
    printf("\tPress 3 - VIEW All members\n\n");
    printf("\tPress ctrl c - EXIT\n\n\n");
    printf("You Entered --> : ");
    scanf("%d",&op);
    getchar()----> Add this as scanf inserts a \n in the buffer...Though you have used fflush(stdin)..the \n is not flushed as fflush doenot work with input streams
    Ref: man fflush


    Fix 2 : if(op == 1)
    { // read from keyboard
    printf("\nEnter FULL NAME with _ for spaces AND(after a space) Today's date <dd/mm//yy>.\n");
    fflush(stdin);
    memset(members_init,0,sizeof(members_init); -----> Initiaze the buffer as it would copy Unwanted junk data to your buffer

    Fix3: int load_members(struct members member[1000]) // loads ONLY NAME and JOINED DATE to structure
    {
    FILE *members;
    char members_det[58];
    memset(members_det,0,sizeof(members_det)); -----Same reason as fix 2...

    When I ran on my system I got segfault n
    strcpy((member.joined_date),temp_joined_date);----As Strcpy copies data upto Null character..
    Hence the buffer needs to be initialsed so the it doesnt copy data untill null is found which was your case
     
    jose_peeterson likes this.

Share This Page