Help With An Alphabetizing Program

This is a discussion on Help With An Alphabetizing Program within the C Programming forums, part of the General Programming Boards category; I have to make a program that takes words input by the user, prints them out, alphabetizes them, and then ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    9

    Help With An Alphabetizing Program

    I have to make a program that takes words input by the user, prints them out, alphabetizes them, and then prints out the alphabetized words. It MUST use functions. I had the code working without functions but now when I try to build it, I get the following errors:

    error LNK2019: unresolved external symbol "char __cdecl sortWords(char)" (?sortWords@@YADD@Z) referenced in function _wmain word list.obj


    error LNK2019: unresolved external symbol "void __cdecl printWords(char,int)" (?printWords@@YAXDH@Z) referenced in function _wmain word list.obj


    error LNK2019: unresolved external symbol "char __cdecl getWords(char)" (?getWords@@YADD@Z) referenced in function _wmain word list.obj


    fatal error LNK1120: 3 unresolved externals F:\word list\Debug\word list.exe 1


    Here is my code:


    Code:
    #include "stdafx.h"
    #include "string.h"
    
    
    char getWords(char);
    void printWords(char, int);
    char sortWords(char);
    
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        #define LETSIZE 15
        #define WORSIZE 25
    
    
        char wordArray[WORSIZE][LETSIZE];
        int z, alphaWords = 0;
    
    
        for(z=0; z<WORSIZE; z++)
        {
            strcpy(wordArray[z], "\0");
        }    
        
        getWords(wordArray[WORSIZE][LETSIZE]);
        printWords(wordArray[WORSIZE][LETSIZE], alphaWords);
        sortWords(wordArray[WORSIZE][LETSIZE]);
    
    
        alphaWords=1;
    
    
        printWords(wordArray[WORSIZE][LETSIZE], alphaWords);
    
    
    
    
        return 0;
    }
    
    
    
    
    
    
    
    
    
    
    char getWords(char gWordArray[WORSIZE][LETSIZE])
    {
         char yesNo[2] = "y";
         int wordCounter = 0;
        
        printf( "Enter a word: ");
        scanf( "%s", gWordArray[wordCounter]);
        printf("\n");
    
    
        if( strlen(gWordArray[wordCounter]) > LETSIZE)
        {
            wordCounter = 0;
        }
    
    
        while( strcmp(yesNo, "y") == 0 && wordCounter < WORSIZE)
        {
            wordCounter++;
            printf( "Do you have more words? yes or no: ");
            scanf( "%s", yesNo);
    
    
            if(strcmp(yesNo, "y") == 0)
            {
                printf( "Enter a word: ");
                scanf( "%s", gWordArray[wordCounter]);
                printf("\n");
    
    
                if( strlen(gWordArray[wordCounter]) > LETSIZE)
                {
                    wordCounter--;
                }
            }
        }
    
    
        return gWordArray[WORSIZE][LETSIZE];
    
    
    }
    
    
    
    
    
    
    
    
    
    
    void printWords(char prWordArray[WORSIZE][LETSIZE], int alWord)
    {
        int y;
    
    
        printf("\n");
        
        for(y = 0; y <= WORSIZE && strcmp(prWordArray[y], "\0")!=0; y++)
        {
            printf( "%s \n", prWordArray[y+alWord]);
        }
    
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    char sortWords(char sortWordArray[WORSIZE][LETSIZE])
    {
        char hold[WORSIZE][LETSIZE];
        int x, swap, pass;
    
    
    
    
        for(pass = 1; pass <= WORSIZE && strcmp(sortWordArray[pass], "\0")!=0; pass++)
        {
            swap = 0;
            
            for(x = 0; x <= WORSIZE && strcmp(sortWordArray[pass], "\0")!=0; x++)
            {
                
                if(strcmp(sortWordArray[x], sortWordArray[x+1]) > 0)
                {
                    strcpy(hold[x], sortWordArray[x]);
                    strcpy(sortWordArray[x], sortWordArray[x+1]);
                    strcpy(sortWordArray[x+1], hold[x]);
                    swap = 1;
                }
            }
            if(!swap)
                break;
        }
    
    
    
    
    
    
        return sortWordArray[WORSIZE][LETSIZE];
    }
    And yes, I'm a novice programmer.

    Any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    2,587
    Why this instead of normal main() function?
    Code:
    int _tmain(int argc, _TCHAR* argv[])
    If not on purpose; use normal main() function.

    If on purpose, I have no idea why or how to do it right.

    These prototypes are promises you made to the compiler; you broke them that is why your are getting the link errors.
    Your promised you will be pass just a single character to each function; but, it looks like you need to pass an array.
    Code:
    char getWords(char);
    void printWords(char, int);
    char sortWords(char);
    What you gave it was NOT what you promised.
    Code:
    char getWords(char gWordArray[WORSIZE][LETSIZE])
    Tim S.
    Last edited by stahta01; 11-02-2011 at 05:40 PM.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    I just used my compilers default for main.

    So my errors are because I told the compiler I would be passing in a single variable and not array?

    So I should do something like:
    Code:
    char getwords(char [][]);
    Last edited by emanly; 11-02-2011 at 05:44 PM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    2,587
    Quote Originally Posted by emanly View Post
    I just used my compilers default for main.

    So my errors are because I told the compiler I would be passing in a single variable and not array?

    So I should do something like:
    Code:
    char getwords(char [][]);

    This will work

    Code:
    char getWords(char gWordArray[WORSIZE][LETSIZE]);
    One of these will work; I can not remember whether first or last dimension can be missing; the memory is not to good tonight.

    Code:
    char getWords(char gWordArray[][LETSIZE]);
    or maybe this
    Code:
    char getWords(char gWordArray[WORSIZE][]);

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,498
    Quote Originally Posted by stahta01 View Post
    Code:
    char getWords(char gWordArray[][LETSIZE]);
    This is the one that works. It's only the leftmost set of [ ] that can be empty. Everything else must contain the right number of elements.

    @emanly:
    When you use pass or return arrayName[WORSIZE][LETSIZE], you are not passing the entire array. You're passing one element. But you're not even really doing that, since arrays in C go from 0 to size-1, you're passing an element that is out of bounds for the array. The last element in the array is arrayName[WORSIZE-1][LETSIZE-1].

    To pass the entire array to your functions, which is what the definitions suggest you want to do, just use the name of the array without any [ ]:
    Code:
    getWords(wordArray);
    You can't return an entire array from a function, and you definitely shouldn't return a local array. Since you don't actually use the return value from getWords or sortWords, you should declare them to return void, and remove the return statements inside.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    Thanks for the help so far guys!

    I've fixed the functions and now it builds but now it crashes when it gets to the sortWords function. While debugging it says its because of a buffer overrun, I think. Is this because I'm passing arrays to functions again?

    Updated code:

    Code:
    
    
    
    
    #include "stdafx.h"
    #include "string.h"
    
    
    #define LETSIZE 15
    #define WORSIZE 25
    
    
    char getWords(char gWordArray[][LETSIZE]);
    void printWords(char prWordArray[][LETSIZE], int);
    char sortWords(char sortWordArray[][LETSIZE]);
    
    
    int main()
    {
    
    
    
    
        char wordArray[WORSIZE][LETSIZE];
        int z, alphaWords = 0;
    
    
        for(z=0; z<WORSIZE; z++)
        {
            strcpy(wordArray[z], "\0");
        }    
        
        getWords(wordArray);
        printWords(wordArray, alphaWords);
        sortWords(wordArray);
    
    
        alphaWords=1;
    
    
    
    
        printWords(wordArray, alphaWords);
    
    
    
    
        return 0;
    }
    
    
    
    
    
    
    
    
    
    
    char getWords(char gWordArray[WORSIZE][LETSIZE])
    {
         char yesNo[2] = "y";
         int wordCounter = 0;
        
        printf( "Enter a word: ");
        scanf( "%s", gWordArray[wordCounter]);
        printf("\n");
    
    
        if( strlen(gWordArray[wordCounter]) > LETSIZE)
        {
            wordCounter = 0;
        }
    
    
        while( strcmp(yesNo, "y") == 0 && wordCounter < WORSIZE)
        {
            wordCounter++;
            printf( "Do you have more words? yes or no: ");
            scanf( "%s", yesNo);
    
    
            if(strcmp(yesNo, "y") == 0)
            {
                printf( "Enter a word: ");
                scanf( "%s", gWordArray[wordCounter]);
                printf("\n");
    
    
                if( strlen(gWordArray[wordCounter]) > LETSIZE)
                {
                    wordCounter--;
                }
            }
        }
    
    
        return gWordArray[WORSIZE][LETSIZE];
    
    
    }
    
    
    
    
    
    
    
    
    
    
    void printWords(char prWordArray[WORSIZE][LETSIZE], int alWord)
    {
        int y;
    
    
        printf("\n");
        
        for(y = 0; y <= WORSIZE && strcmp(prWordArray[y], "\0")!=0; y++)
        {
            printf( "%s \n", prWordArray[y+alWord]);
        }
    
    
    
    
    }
    
    
    
    
    
    
    
    
    
    
    char sortWords(char sortWordArray[WORSIZE][LETSIZE])
    {
        char hold[WORSIZE][LETSIZE];
        int x, swap, pass;
    
    
    
    
        for(pass = 1; pass <= WORSIZE && strcmp(sortWordArray[pass], "\0")!=0; pass++)
        {
            swap = 0;
            
            for(x = 0; x <= WORSIZE && strcmp(sortWordArray[pass], "\0")!=0; x++)
            {
                
                if(strcmp(sortWordArray[x], sortWordArray[x+1]) > 0)
                {
                    strcpy(hold[x], sortWordArray[x]);
                    strcpy(sortWordArray[x], sortWordArray[x+1]);
                    strcpy(sortWordArray[x+1], hold[x]);
                    swap = 1;
                }
            }
            if(!swap)
                break;
        }
    
    
    
    
    
    
        return sortWordArray;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    One problem I see in your sortWords function is you have told the compiler you are going to return a single character but when you return you try to return your char array.

    Jim

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    I need to pass the array into getWords, return it, pass it into printWords, return it, pass it into sortWords, return it, and finally back into printWords. Either my logic is wrong or my code. Where did I tell the compiler I'll be passing in a single character? I thought I fixed that.

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    In your function definition you have char sortWords(...) that char means a single character. You do know that when you pass an array into a function in the parameters it will change the variable in the calling program as well? In other words after your function returns the variable you passed into the function will contain all the changes you made inside your function, so you really do not need to return the array with the return statement.

    Also you should be returning the number of words entered from your getWords() function. And you should be using this value in both your sortWords and printWords function to stop processing when you have processed all of the words entered.


    Jim

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    That is pretty much how my code worked before putting it in functions. I used a word counter for all my conditions to stop the loops before they hit the nulls. The problem is that you can only return one value from a function and since my array is used throughout, I did away with the word counter in the other functions.

    Hence why I use
    Code:
    strcmp(sortWordArray[pass], "\0")!=0
    as a condition instead of a counter. For me to use the counter I would have to use pointers. I did't know that about the arrays being modified. So when sortWordArray is modified, wordArray is as well? So everytime I call or modify one, I do the same to the other?

  11. #11
    Registered User
    Join Date
    Nov 2011
    Posts
    9
    Alright! I got it! Thanks guys! Without the help I wouldn't be able to figure it out. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alphabetizing Names
    By thestrapman in forum C Programming
    Replies: 6
    Last Post: 08-21-2008, 12:36 AM
  2. Alphabetizing n words.
    By eurus in forum C Programming
    Replies: 2
    Last Post: 01-19-2006, 10:57 PM
  3. alphabetizing?
    By Cudarich in forum C Programming
    Replies: 1
    Last Post: 12-09-2004, 02:20 AM
  4. alphabetizing
    By mouse163 in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2002, 02:59 PM
  5. Need help alphabetizing a Struct
    By Sapphire19 in forum C Programming
    Replies: 4
    Last Post: 11-28-2001, 11:47 AM

Tags for this Thread


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