Sorting C-string arrays

Hey guys, I'm just making a general program that I make for every subject to practice my skills, substituting for given chapter, pointers, arrays, etc.. Well I'm on C-string's and I'm trying to use a selection sort but I'm getting a run time error so I can't figure it out. Any help is appreciated! Thanks..

#include <iostream>
#include <cctype>
#include <cstring>
char *getId(const int);
const int SIZE = 100;
int searchId(char[][SIZE], int, char*);
void selec(char[][SIZE], int);


using namespace std;
int main()
{

const int size1 = 100;
const int size2 = 100;
const int numProd = 5;

char iNum[numProd][size1] = {"917", "918", "919", "920", "921"};
char iPrice[numProd][size1] = {"980", "660", "450", "520", "730"};
char iName[numProd][size1] = {"Glock", "Ruger", "Smith&Wesson", "SigSauer", "Taurus"};


char* ptr = nullptr;

ptr = getId(size1);

int index;
index = searchId(iNum, SIZE, ptr);

}

char* getId(const int size)
{
char id[size];

cout << "Enter Item Number: ";
cin.getline(id, size);

return id;
}

int searchId(char search[][SIZE], int size, char* id)
{
int index;
int count = 0;

for(int index = 0; index < size; index++)
{

if(strcmp(search[index], id) == 0)
{
break;
}

count++;
}

if(strcmp(search[count], id) != 0)
return -1;

return count;

}

void selec(char sort[][SIZE], int size)
{
int start, minIndex;
char* minValue = nullptr;
int index;

for(start = 0; start < size - 1; start++)
{
minIndex = start;
minValue = sort[start];
for(index = start + 1; index < strlen(*sort); index++)
{
if(strcmp(sort[index], sort[index+1]) < 0)
{
minIndex = index;
minValue = sort[index];
}
}
cout << minValue << sort[index+1] << endl;
}
}
Last edited on
getId(...) is invalid. Never return a pointer to a local variable. Instead provide the buffer to be fillef as a parameter:

void getId(char* p, const int size)

You use the second parameter for searchId(...) wrong. It is not SIZE but numProd.


Please use code tags: [code]Your code[/code]
Read this: http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.