Thread: Can someone help mewith this program?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    4

    Can someone help mewith this program?

    Iam not getting the desired output, I dont know what is wrong.

    /**************************************
    ** This program allows the user to enter **
    ** words up to 20 characters long and stops **
    ** at the first 4 charcter word. It then **
    ** places the words in ascending order **
    ** printing the smallest and largest word. **
    **************************************/

    Code:
      #include <stdio.h>
      #include <string.h>
    
      #define N 20
    
      void smallest_word(char[][], int );
      void largest_word(char[][], int);
      int read_line(char[][], int);
    
      int i = 0;
    
      main()
    
       { int len=0;
        char str[N][N];
    
        while (len != 4)
        { printf("Enter word: ");
          len = read_line(str, N);
        }
         smallest_word(str, N);
         largest_word(str, N);
    
    
        return 0;
      }
    
    
                /* read_line */
    
        int read_line(char str[][N], int n)
        { int l=0;
    
          scanf("%s", str[i]);
          l = strlen(str[i]);
          i++;
          if (l >= 20)
          {printf("Word too long.\n ");
           exit(0);
          }
          else
          {printf("Word is: %s\n", str[i-1]);
          return l;
          }
        }
    
              /* smallest_word */
    
       void smallest_word(char str[][N], int n)
    
       { int j;
         char smallest;
    
         strcpy(&smallest, str[0]);
         for(j=1; j<=i; j++)
         { printf("str[%d] = %s\n", j, str[j]);
           if (strcmp(str[j], &smallest) < 0)
              strcpy(&smallest, str[j]);
         }
         printf("Smallest word is: %s\n", &smallest);
       }
    
    
               /*  largest_word  */
    
       void largest_word(char str[][N], int n)
    
       { int j;
         char largest;
    
         strcpy(&largest, str[0]);
         for(j=1; j<=i; j++)
         { if (strcmp(str[j], &largest) > 0)
             strcpy(&largest, str[j]);
         }
         printf("Largest word is: %s\n", &largest);
       }

    Errors being obtained:

    Enter word: twelve
    Enter word: six
    Enter word: fifty
    Enter word: four
    str[1] = six
    Smallest word is: six
    Largest word is: t
    Last edited by Grifftt; 10-19-2002 at 05:30 PM.

  2. #2
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    What do your errors say. Post them.
    Also, why are you using a 2D array.
    Mr. C: Author and Instructor

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Here are some errors:

    In function smallest_word()

    >>char smallest;
    >>strcpy(&smallest, str[0]);
    smallest is defined as a single char, not an array, so you can't copy to it. Try:
    >>char smallest[N];
    >>strcpy(smallest, str[0]);

    >>for (j = 1; j <= i; j++)
    The array elements start at 0, not 1. So try
    >>for (j = 0; j < i; j++)

    All occurences of &smallest should be changed to smallest.

    The same errors are in the largest_word() function too.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM