-1

I'm trying to make a C program, but I keep getting an error from a function I added:

undefined reference to `genName'collect2.exe: error: ld returned 1 exit status

The function I added is made to generate a random name. The rest of the code worked before, but when I try to test this new function the above error occurs.

The new function is defined as (defined after all the rest of my code):

char* genName()
{
    srand(time(0));
    int r = rand() % (sizeof(firstNames)/sizeof(firstNames[0]));
    strcpy(first, firstNames[r]);
    int v = rand() % (sizeof(lastNames)/sizeof(lastNames[0]));
    srand(time(0));
    strcpy(last, lastNames[v]);
    strcat(first, last);
    return first;
}

The rest of my code is:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

// If 0, organize by all. if 1, organize by grade. if 3, organize by speed.
int method = 3;

char first[40];
char last[25];
char* genName();

typedef struct
{
    char name[60];
    int score;
    double time;
} student;

char firstNames[][15] =
{
    {"John"},
    {"Jim"},
    {"Tim"},
    {"Sam"},
    {"Jack"},
    {"Larry"},
    {"Bob"},
    {"Mack"},
    {"Kyle"},
    {"Tom"},
    {"Joe"},
    {"Dan"}
};

char lastNames[][25] =
{
    {" Howards"},
    {" Nyles"},
    {" Jones"},
    {" White"},
    {" Myles"},
    {" Simones"},
    {" Smith"},
    {" Johnson"},
    {" Williams"},
    {" Brown"},
    {" Jackson"},
    {" Martin"},
    {" Davis"},
    {" Thompson"},
    {" Moore"}
};


int compare(const void*, const void*);

student s1 = {"Jim Howards", 89, 4.90};
student s2 = {"Tim Nyles", 76, 6.12};
student s3 = {"John Jones", 97, 7.56};
student s4 = {"Sam White", 50, 1.12};
student s5 = {"Jack Myles", 88, 3.90};

int main()
{
    student array[] = {s1, s2, s3, s4, s5};
    qsort(array, (sizeof(array)/sizeof(array[0])), sizeof(array[0]), compare);

    if (method == 1)
    {
        for (int i = 0; i < (sizeof(array)/sizeof(array[0])); i++)
        {
            printf("%d.%s, with a score of %d%%\n", i+1, array[i].name, array[i].score);
        }    
    }
    else
    {
        if (method == 2)
        {
            for (int i = 0; i < (sizeof(array)/sizeof(array[0])); i++)
            {
                printf("%d.%s, with a time of %1.2f minutes\n", i+1, array[i].name, array[i].time);
            }    
        }
        else
        {
            if (method == 3)
            {
                for (int i = 0; i < (sizeof(array)/sizeof(array[0])); i++)
                {
                    printf("%d.%s, with a score of %d%%, and a time of %1.2f\n", i+1, array[i].name, array[i].score, array[i].time);
                }
            }
        } 
    }

    char* hi = genName();
    printf("%s", hi);

    return 0;
}

int compare(const void * num1, const void * num2)
{
    student *st1 = (student *)num1;
    student *st2 = (student *)num2;
    if (method == 1)
    {
        if (st1->score < st2->score)
        {
            return 1;
        }
        if (st1->score == st2->score)
        {
            return 0;
        }
        if (st1->score > st2->score)
        {
            return -1;
        }
    }
    else
    {
        if (method == 2)
        {
            if (st1->time > st2->time)
            {
                return 1;
            }
            if (st1->time == st2->time)
            {
                return 0;
            }
            if (st1->time < st2->time)
            {
                return -1;
            }   
        } 
        else
        {
            if (method == 3)
            {
                if (st1->score < st2->score)
                {
                    return 1;
                }

                if (st1->score == st2->score)
                {
                    if (st1->time > st2->time)
                    {
                        return 1;
                    }
                    if (st1->time == st2->time)
                    {
                        return 0;
                    }
                    if (st1->time < st2->time)
                    {
                        return -1;
                    }  

                }

                if (st1->score > st2->score)
                {
                    return -1;
                }   
            } 
        }
    }
}
Das_Geek
  • 2,775
  • 7
  • 20
  • 26
Deathtraptaco
  • 21
  • 1
  • 9

1 Answers1

1

As written, your implementation of char *genName() is inside the brackets of your implementation of int compare(const void*, const void*). Consistent indentation can help avoid errors like these.

Nick Reed
  • 4,989
  • 4
  • 17
  • 37