Thread: infinite loop

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    9

    infinite loop

    Hello: i have a problem about the loop
    this problem is fine when the number of name is less than 3
    after the num >=4 , after showing the unsorted output, it doesnt show the sorted one. i dunno what wrong with my program . Plz help.. thank you

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define merror()   {printf("memory allocation problem\n");exit(1);}
    
    int main(void)
    {
         char buffer[2];
         char **buf;
         char *sto;
         int num;
         int i, j;
    
    
         A: printf(" how many names will be entered : \n");
         fflush(stdout);
         fgets ( buffer, 10,  stdin );
        num = atoi (buffer);
    
        if (num <=0)
        {
                   printf("empty input \n");
         }
         else if (num >100)
         {
                   printf("input too long \n");
                   goto A;
         }
         else if (num >0 && num <= 100)
         {
                   printf("you will enter %d names \n", num);
         }
    
         for(i=0; i<num; i++)
         {
              buf[i] = malloc(sizeof(char*));
         }
    
         for(i=0; i < num; i++)
         {
              printf(" enter a name \n");
              for(j=0; j<21; j++)
              {
                    buf[i][j]=fgetc(stdin);
                    if (buf[i][j]=='\n') {
                      buf[i][j]='\0';
                      break;   /* break j-loop */
                    }
                  }/* end j-loop */
    
                  if (j==21)
                  {
                    printf("input too long\n");
                    while(fgetc(stdin)!='\n');
                    continue;   /* continue i-loop */
                }
       }
    
        printf("\nunsorted input:\n");
        for(i=0; i< num; i++)
        printf("%s\n", buf[i]);
    
    sto = malloc(sizeof(char*));
    
    for(i=num; i>0; i--)
    {
        for(j=0; j<i; j++)
    		if (strcmp(buf[j],buf[j+1])>0)
    		{
     			  sto=buf[j];
     			  buf[j]=buf[j+1];
       				buf[j+1]=sto;
    		}
    }
    
      printf("sorted input:\n");
      for(i=0; i<num; i++)
      {
        printf("%s\n",buf[i]);
      }
    
     free(buf);
      free(sto);
    
    return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    This may have something to do with it:
    Code:
     char buffer[2];
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Registered User t3chn0n3rd's Avatar
    Join Date
    Dec 2007
    Location
    kansas city
    Posts
    25

    c code

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    #define merror()   {printf("memory allocation problem\n");exit(1);}
    
    int main(void)
    {
         char buffer[2];
         char **buf;
         char *sto;
         int num;
         int i, j;
    
    
         A: printf(" how many names will be entered : \n");
         fflush(stdout);
         fgets ( buffer, 10,  stdin );
        num = atoi (buffer);
    
        if (num <=0)
        {
                   printf("empty input \n");
         }
         else if (num >100)
         {
                   printf("input too long \n");
                   goto A;
         }
         else if (num >0 && num <= 100)
         {
                   printf("you will enter %d names \n", num);
         }
    
         for(i=0; i<num; i++)
         {
              buf[i] = malloc(sizeof(char*));
         }
    
         for(i=0; i < num; i++)
         {
              printf(" enter a name \n");
              for(j=0; j<21; j++)
              {
                    buf[i][j]=fgetc(stdin);
                    if (buf[i][j]=='\n') {
                      buf[i][j]='\0';
                      break;   /* break j-loop */
                    }
                  }/* end j-loop */
    
                  if (j==21)
                  {
                    printf("input too long\n");
                    while(fgetc(stdin)!='\n');
                    continue;   /* continue i-loop */
                }
       }
    
        printf("\nunsorted input:\n");
        for(i=0; i< num; i++)
        printf("%s\n", buf[i]);
    
    sto = malloc(sizeof(char*));
    
    for(i=num; i>0; i--)
    {
        for(j=0; j<i; j++)
    		if (strcmp(buf[j],buf[j+1])>0)
    		{
     			  sto=buf[j];
     			  buf[j]=buf[j+1];
       				buf[j+1]=sto;
    		}
    }
    
      printf("sorted input:\n");
      for(i=0; i<num; i++)
      {
        printf("%s\n",buf[i]);
      }
    
     free(buf);
      free(sto);
    
    return 0;
    }

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgets ( buffer, 10, stdin );
    do not lie to fgets - your buffer is only 2 bytes long, not 10

    use fgets ( buffer, sizeof buffer, stdin );
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-14-2009, 11:24 PM
  2. Cosine fucntion and infinite loop.
    By youareafever in forum C Programming
    Replies: 2
    Last Post: 11-07-2008, 04:45 AM
  3. Infinite Loop with GetAsyncKeyState
    By guitarist809 in forum Windows Programming
    Replies: 1
    Last Post: 04-18-2008, 12:09 PM
  4. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 12:46 AM
  5. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM