-
Help with Sorting
I am having trouble on my sort. I am trying to sort First and Last Name (in ABC order), and for every name there is the age, city, state, etc.. Obviously when I sort the Names, I have to get the age, city, state, etc.. to go along with it. I have managed to get age and zipcode to switch along with the names, but I dont know how to change the city to go along with the name because it is a char. How would I do this?
I know my code is really rough, and there are better ways to do the whole code, but what is the most simplest way possible to get the city to change along with the First and Last name? Thanks! :)
This is the WHOLE Code:
http://rafb.net/p/1uDyjo27.html
This is the section I am working on now:
Code:
void searchfull()
{
int j;
char tempf[25][25],templ[25][25],tempr[25][25],tempq[25][25];
int tempn[25],tempx[25];
for (j=1; j<=u-1; j++)
{
for (I=1; I<=u-1; I++)
{ namecmp=strcmpi(namel[I], namel[I+1]);
if(namecmp > 0)
{
strcpy(tempf[I],namef[I]);
strcpy(templ[I],namel[I]);
strcpy(tempr[I],city[I]);
tempn[I]=zipcode[I];
tempx[I]=age[I];
strcpy(namef[I], namef[I+1]);
strcpy(namel[I], namel[I+1]);
strcpy(city[I], city[I+1]);
zipcode[I]=zipcode[I+1];
age[I]=age[I+1];
strcpy(namef[I+1],tempf[I]);
strcpy(namel[I+1],templ[I]);
strcpy(city[I], tempr[I+1]);
zipcode[I+1]=tempn[I];
age[I+1]=tempx[I];
}
else if(namecmp==0)
{
namecmp=strcmpi(namef[I], namef[I+1]);
if(namecmp> 0)
{
strcpy(tempf[I],namef[I]);
strcpy(tempr[I],city[I]);
strcpy(templ[I],namel[I]);
tempn[I]=zipcode[I];
tempx[I]=age[I];
strcpy(namef[I], namef[I+1]);
strcpy(namel[I], namel[I+1]);
strcpy(city[I],city[I]);
zipcode[I]=zipcode[I+1];
age[I]=age[I+1];
strcpy(namef[I+1],tempf[I]);
strcpy(namel[I+1],templ[I]);
strcpy(city[I],tempr[I]);
zipcode[I+1]=tempn[I];
age[I+1]=tempx[I];
}
}
}
}
If you want to talk to me one on one, I am available on AIM at Chronotrigga05 right now :D. Or you can reply down here, Ill reply back.
-
What you need is 1) to learn about classes and (more generally) OOP and 2) stop using C strings and start using std::string string.
-
Create a struct for all your data, not a bunch of parallel arrays.
Code:
struct info {
char namel[20];
char namef[20];
char city[10];
int zipcode;
int age;
};
info myArr[25]; // or however many you have
Keeping records which are supposed to be together actually together becomes so much easier.
Then you'll be able to write rather more compact
Code:
if(namecmp > 0)
{
info temp = myArr[i+1];
myArr[i+1] = myArr[i];
myArr[i] = temp;
}
But as Desolation says, std::string fname; would be much preferred.
> namecmp=strcmpi(namel[i], namel[I+1]);
Two global variables, both called 'i', one in lower case and one in upper case - UGH!
Anyway, to use the array, it would be
Code:
namecmp=strcmpi(myArr[i].namel, myArr[I+1].namel);
-
For:
Code:
if(namecmp > 0)
{
info temp = myArr[i+1];
myArr[i+1] = myArr[i];
myArr[i] = temp;
}
Do I have to make a new struct for temp?
-
Forget about those ugly C-style strings and use std::string.
-
What's wrong with the struct you've got?
It's the same type as elements of your array, so assignment does exactly what you would want it to do (in this case).
-
Well, my project is due soon like tommorow, but I am willing to give std::string a shot to learn it; can you give me an example of how to use it or lead me to a site?
But even if C-style strings are ugly, I at least know how to 'kind of' do it. Anyway, I was talking about info temp; if I have to make a new struct info for it or not.
-
No you don't.
It it were an array of int, and int temp; would there be an issue?
-
Yes, there would be if it was an int of temp. Anyway; it runs but it doesn't sort ABC.. Not sure:
Code:
void searchfull()
{
int j;
char tempf[25][25],templ[25][25],tempr[25][25],tempq[25][25];
int tempn[25],tempx[25];
for (j=1; j<=u-1; j++)
{
for (I=1; I<=u-1; I++)
{ namecmp=strcmpi(MyArr[I].namel[I], MyArr[I+1].namel[I]);
if(namecmp > 0)
{
info temp=MyArr[I+1];
MyArr[I+1]=MyArr[I];
MyArr[I]=temp;
}
else if(namecmp==0)
{
namecmp=strcmpi(MyArr[I].namef[I], MyArr[I+1].namef[I+1]);
if(namecmp> 0)
{
info tempi=MyArr[I+1];
MyArr[I+1]=MyArr[I];
MyArr[I]=tempi;
}
}
}
}
}