infinite loop

This is a discussion on infinite loop within the C Programming forums, part of the General Programming Boards category; Hello: i have a problem about the loop this problem is fine when the number of name is less than ...

  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
    Katy, Texas
    Posts
    2,309
    This may have something to do with it:
    Code:
     char buffer[2];
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    fgets ( buffer, 10, stdin );
    do not lie to fgets - your buffer is only 2 bytes long, not 10

    use fgets ( buffer, sizeof buffer, stdin );
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21