Results 1 to 5 of 5

Thread: C program not working

  1. #1
    Join Date
    Feb 2013
    Beans
    9

    C program not working

    This is the exact code i have written there are no errors in the code as it compiled successfully but in the output when it asks the user to enter "enter 1 for char blah blah......." if i press 1 and enter it is not taking the input instead it is printing the printf statements later..........






    Code:
    #include<stdio.h>
    #include<string.h>
    void charstuff(char *txt,char *sdat)
     {
      int ln=strlen(txt),i,j;
      sdat[0]='\0';
      strcat(sdat,"stxdle");
      for(i=0,j=6;i<ln;i++,j++)
       {
        sdat[j]=txt[i];
        if(!strncmp(txt+i,"dle",3))
         {
          sdat[j+1]='\0';
          strcat(sdat,"ledle");
          j+=5;
          i+=2;
         }
       }
      sdat[j]='\0';
      strcat(sdat,"dleetx");
     }
    void bitstuff(char *txt,char *sdat)
     {
      int ln=strlen(txt),i,j;
      sdat[0]='\0';
      strcat(sdat,"01111110");
      for(i=0,j=8;i<ln;i++,j++)
       {
        sdat[j]=txt[i];
        if(!strncmp(txt+i,"11111",5))
         {
          sdat[j+1]='\0';
          strcat(sdat,"11110");
          j+=5;
          i+=4;
         }
       }
      sdat[j]='\0';
      strcat(sdat,"01111110");
     }
    void main()
     {
      int ch; 
      char txt[500],sdat[500];
      printf("\n enter 1 for character stuffing any other for bit stuffing:");
      scanf("%d",&ch);
      printf("\n enter the data to be transmitted \n");
      fflush(stdin);
      gets(txt);
      if(ch==1)
       charstuff(txt,sdat);
      else
       bitstuff(txt,sdat);
      printf("\n after framing the data to be transmitted is \n");
      puts(sdat);
     }
    Last edited by harrypotter7; October 14th, 2013 at 12:04 PM. Reason: wanted to add tags

  2. #2
    Join Date
    Sep 2010
    Beans
    898

    Re: C program not working

    It would be helpful if you could edit your post and use [ code ] tags, so that the C source code has proper indentation.

  3. #3
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,083
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: C program not working

    Instead of
    Code:
      scanf("%d", &ch);
    Try
    Code:
      gets(txt);
      sscanf(txt, "%d", &ch);

  4. #4
    Join Date
    Feb 2013
    Beans
    9

    Re: C program not working

    working perfectly do u mind telling me what is the reason........

  5. #5
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,083
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: C program not working

    fflush(stdin) does not necessarily do what you might expect. In this case, it does not cause the '\n' at the end of the first line of input to be consumed. So that's what gets read by your gets(txt), and it doesn't wait for a second line.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •