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

program terminates at fgets();

Discussion in 'C' started by jose_peeterson, Dec 14, 2011.

  1. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    Dear all,
    i am trying to make a scheduler program. so in the following code the part where
    Code:
              if(op == 2)  
                 printf("ENTER descrption, start time and duration.\n");
                  fgets(desc,100,stdin); 
                  scanf("%d%*d",&start_h);  
                  scanf("%d%*d",&dur);       
                  add(appo,desc,start_h,dur);
    
    is for adding appointments to a structure with the add fucntion.
    in this part i ask for a description of the appointment, the start time and the duration.
    to ask for the description, i am using the fgets(); function. but the program terminates at this point saying "schedule.exe has stopped working".

    this entire part is inside the main, inside an infinite loop like this

    Code:
     
    char desc[100];
    int start_h,dur,clash,op;
    
    while(1)
      {
       printf("SHOW - 1,ADD - 2,DEL - 3 appointments, QUIT - 4\n");
       scanf("%d",&op);
        
       if(op == 1)
        {
         printf("\n If NOT shown then FREE\n\n"); 
         show(appo);
        } 
       if(op == 2) 
        {
         printf("ENTER descrption, start time and duration.\n");
         fgets(desc,100,stdin);                        // PROGRAM TERMINATES HERE
    scanf("%d%*d",&start_h);
     scanf("%d%*d",&dur);
    
     clash = add(appo,desc,start_h,dur);
         
         
         if(clash == 1)
          {
           printf("\n\tWARNING : UNABLE to add due to CLASHES\n");
          }
         else
          {
           printf("\n\tSUCCESSFULLY added to schedule\n");
          } 
        }
       if(op == 3)
        {
         printf("op 3\n"); 
        }
        
       if(op == 4) 
        {
         return 0;
        }
      }
      
    
    so i want to know why it terminates and how to get the description which is 1 line long? thanks for your help.

    for your reference
    Code:
    struct appointments 
    {
     char desc[100];                    
     int start_h;
     int dur;
    };
    
    the add function for your reference.
    Code:
    int add(struct appointments appo[24],char desc[100],int start_h,int dur)
    {
     int var = start_h;
    
     if(!strcmp((appo[start_h].desc),"FREE"))
      {
       strcpy((appo[start_h].desc),desc);
       (appo[start_h].start_h) = start_h;
       (appo[start_h].dur) = dur;
       
       while(var <= (start_h + dur))
       {
        strcpy((appo[var].desc),"Continued | Occupied");
        (appo[var].start_h) = start_h;
        (appo[var].dur) = dur;  
        var++;
       }
       return 0; 
      }
     else
      return 1;
    }
    
     
    Last edited: Dec 14, 2011
  2. jose_peeterson

    jose_peeterson New Member

    Joined:
    May 19, 2011
    Messages:
    56
    Likes Received:
    1
    Trophy Points:
    0
    dear all,
    i have used the trial and error method to solve the above problem. the code below works just as i expected.however i did am not writing to any file in this program. maybe i will try that later.
    for now it works when the program is running.

    I DON'T UNDERSTAND HOW THE ABOVE DID NOT WORK. i have put the SCANF(); before the fgets(); and it magically worked. can anyone please tell me why the above did now work but the following did. THANKS A LOT.

    please just run this code.

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<iostream.h>
    #include<string.h>
    
    struct appointments 
    {
     char desc[100];                    
     int start_h;
     int dur;
    };
    
    void show(struct appointments appo[24]);
    int add(struct appointments appo[24],char desc[100],int start_h,int dur);
    void del(struct appointments appo[24],int start_h,int dur);
    
    int main()
    {
             
     char desc[100];
     int start_h,dur,clash,op;
      
      struct appointments appo[24] = {{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0},{"FREE",0,0}};
      //memset(&appo[0],0,24*sizeof(struct appointments));
      
      printf("\n\t\t\tSCHEDULER program.\n\n");
      printf("THIS is a SIMPLE digital scheduler. The three parameters that can be input are\n");
      printf("a ONE Line description, WHOLE NUMBER start time(in railway hours ONLY) and\n");
      printf("Duration(in 1 <= hours <= 23 ONLY).\n\n\n");
      
      while(1)
      {
       printf("\nSHOW - 1,ADD - 2,DEL - 3 appointments, QUIT - 4\n");
       scanf("%d",&op);
        
       if(op == 1)
        {
         printf("\n\n\t\tIf appointments NOT shown then it is FREE to use.\n\n\n"); 
         show(appo);
        } 
       if(op == 2) 
        {
         printf("\nENTER start time, duration and descrption.\n");
         
        scanf("%d",&start_h);
        
         scanf("%d",&dur);
         
         if(dur == 0)
          {
           printf("\n\n\t\tWARNING : UNABLE to add, Duration must be > 0\n\n");
           continue;
          }
         fflush(stdin); 
         fgets(desc,100,stdin);
         clash = add(appo,desc,start_h,dur);
         
         if(clash == 1)
          {
           printf("\n\n\t\tWARNING : UNABLE to add due to CLASHES\n\n");
          }
         else
          {
           printf("\n\n\t\tSUCCESSFULLY added to schedule\n\n");
          } 
        }
       if(op == 3)
        {
         printf("\n\tWARNING : Enter the Start time and Duration to DELETE appointments\n");
         scanf("%d%d",&start_h,&dur); 
         del(appo,start_h,dur); 
        }
        
       if(op == 4) 
        {
         return 0;
        }
      }
      
    system("pause > null");
    return 0;
    }
    
    
    void show(struct appointments appo[24])
    {
     int i=0;
     
     while(i<24) 
      {
       if(strcmp((appo[i].desc),"FREE"))
        {
         printf("\t\tDescription : %s\n",(appo[i].desc));
         printf("\t\tStart Time  : %d\n",(appo[i].start_h));
         printf("\t\tDuration    : %d\n\n",(appo[i].dur)); 
        }  
       i++;
      }                
    return;                 
    }
    
    int add(struct appointments appo[24],char desc[100],int start_h,int dur)
    {
     int var = start_h + 1;
     int d = dur - 1;
     
     if(!strcmp((appo[start_h].desc),"FREE"))
      {
       strcpy((appo[start_h].desc),desc);
       (appo[start_h].start_h) = start_h;
       (appo[start_h].dur) = dur;
       
       while(var < (start_h + dur))
       {
        strcpy((appo[var].desc),desc);
        (appo[var].start_h) = var;
        (appo[var].dur) = d;  
        var++;
        d--;
       }
       return 0; 
      }
     else
      return 1;
    }
    
    void del(struct appointments appo[24],int start_h,int dur)
    {
     int var = start_h;
     
     while(var < (start_h + dur))
     {               
      if(strcmp((appo[var].desc),"FREE"))
       {
        strcpy((appo[var].desc),"FREE");
        (appo[var].start_h) = 0;
        (appo[var].dur) = 0;
       }
      var++; 
     }
     printf("\n\n\t\tSuccessfully DELETED, now Free to use.\n\n");
    
    return;
    }
    
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Difficult to say. scanf is a very confusing function, if you don't know *exactly* what's going on (which is common for a learner) and it goes wrong, it can be very difficult to determine why.

    I suggest you drop scanf and use fgets to read a line of input from the user, then parse that line to get the data you want. So if the data is in the form "description,value 1,value 2" then you can split it at the commas and copy the bits between the commas to various variables.

    So for example if you replace commas with \0 bytes and store the address plus one of those bytes into, say, ptrarray[], then you can do:
    Code:
    strcpy(appo[0].desc, ptrarray[0]); // which would be the same as the start address of the string
    appo[0].start_h=atoi(ptrarray[1]);
    appo[0].dur=atoi(ptrarray[2]);
    
     

Share This Page