0

Does anyone have any idea what's wrong with my code? I keep getting an undefined reference error and I don't know what's causing it. Everything looks correct to me. I'm not very good with using pointers and references, but I checked my textbook and everything seems like it's written as it should be.

//System Libraries Here
#include <iostream>//cin,cout
#include <cstring> //strlen(),strcmp(),strcpy()
using namespace std;

//User Libraries Here

//Global Constants Only, No Global Variables
//Allowed like PI, e, Gravity, conversions, array dimensions necessary
const int COLMAX=80;//Only 20 required, and 1 for null terminator

//Function Prototypes Here
int read(char [][COLMAX],int *);//Outputs row and columns detected from input
void sort(char [][COLMAX],int,int);//Sort by row
void print(const char [][COLMAX],int,int);//Print the sorted 2-D array

//Program Execution Begins Here
int main(int argc, char** argv) {
    //Declare all Variables Here
    const int ROW=30;             //Only 20 required
    char array[ROW][COLMAX];      //Bigger than necessary 
    int colIn,colDet,rowIn,rowDet;//Row, Col input and detected
    
    //Input the size of the array you are sorting
    cout<<"Read in a 2 dimensional array of characters and sort by Row"<<endl;
    cout<<"Input the number of rows <= 20"<<endl;
    cin>>rowIn;
    cout<<"Input the maximum number of columns <=20"<<endl;
    cin>>colIn;
    
    //Now read in the array of characters and determine it's size
    rowDet=rowIn;
    cout<<"Now input the array."<<endl;
    colDet=read(array, &rowDet);
    
    //Compare the size input vs. size detected and sort if same
    //Else output different size
    if(rowDet==rowIn&&colDet==colIn){
        sort(array,rowIn,colIn);
        cout<<"The Sorted Array"<<endl;
        print(array,rowIn,colIn);
    }else{
        if(rowDet!=rowIn)
        cout<<(rowDet<rowIn?"Row Input size less than specified.":
            "Row Input size greater than specified.")<<endl;
        if(colDet!=colIn)
        cout<<(colDet<colIn?"Column Input size less than specified.":
            "Column Input size greater than specified.")<<endl;
    }
    
    //Exit
    return 0;
}

int read(char array[][COLMAX], int *rowDet)
{
    int i;
    int rows = 0;
    int cols = 0;
    string input;
    int len;
    
    for(i = 0; i < *rowDet; i++)
    {
        cout << "Enter string: ";
        cin >> input;
        len = input.length();
        //insert into array if and only if len is positive and less than the Max length
        if (len > 0 && len < COLMAX)
        {
            strcpy(array[rows], input.c_str());
            rows++;
            if(len > cols)
            {
                cols = len;
            }
        }
        else
        {
            cout << "Error Occured: Input String length should be > 0 and < " << COLMAX << endl;
        }
    }
    *rowDet = rows;
    return cols;
}

void sort(char array[][COLMAX],int row, int col)
{
    int i;
    int j;
    char temp[COLMAX];
    
    for(i = 0; i < row; i++)
    {
        for(j = 0; j < row - 1; j++)
        {
            //string comparison on two strings
            //if first is greater than the second then swap them
            if(strcmp(array[j], array[j+1]) > 0)
            {
                strcpy(temp, array[j]);
                strcpy(array[j], array[j + 1]);
                strcpy(array[j + 1], temp);
            }
        }
    }
}

void print(char array[][COLMAX], int row, int col)
{
    int i;
    //print the strings array
    for(i = 0; i < row; i++)
    {
        printf("%s ", array[i]);
    }
}
  • Does this answer your question? [What is an undefined reference/unresolved external symbol error and how do I fix it?](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Ken White Sep 12 '20 at 03:22
  • Lesson on creating a [mre]: Your main function should look more like `int main() { const int ROW=30; char array[ROW][COLMAX]; int colIn, rowIn; print(array,rowIn,colIn); }`. Beyond that, keep the declaration and definition of `print()` and the definition of `COLMAX`. Maybe keep the `#include` and `using` lines. Get rid of everything else and confirm you still get the same error. – JaMiT Sep 12 '20 at 03:28

1 Answers1

1

Your prototype for the print function doesn't match the print function definition below; one has a const array parameter and the other doesn't. Just include const in the function defintion and it should work.

neutrino_logic
  • 1,289
  • 1
  • 6
  • 11