sorting a structure array help

i need to print the names as they appear in the original file, print the info of the person with the highest distance, print the info sorted by ascending ID number, and print sorted by name alphabetically. the first two parts work fine but sorting by ID and Name dont work. i was wondering if anyone could help?

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include <math.h>

using namespace std;

struct Node
{
char name[30];
int ID;
int distance;
};

Node person[10];
void maxdis(int num,Node *person);
void IDsort(int num,Node *person);
void abcsort(int num,Node *person);

int main()
{
char temp[50];
int count = 0;
ifstream In;
In.open("a1.txt");
if (In.good())
{
cout << "* * * a1.txt is FOUND! * * *" << endl;
cout <<"==============================" << endl;
}
else
{
cout << "* * * a1.txt is NOT FOUND! * * *" << endl;
cout << "Restart this program and make sure you place a1.txt\nin the same directory this source file is in" << endl;
cout <<"=======================================================" << endl;
system("PAUSE > null");
return 0;
}
while ( (In.peek()) != EOF )
{
In.getline(person[count].name, 30);
In.getline(temp, 50);
person[count].ID = atoi(temp);
In.getline(temp, 50);
person[count].distance = atol(temp);
In.getline(temp, 50);
cout << person[count].name << endl << person[count].ID << endl << person[count].distance << endl <<temp <<endl;
count++;
}
maxdis(9,person);
IDsort(9,person);
abcsort(9,person);
return 0;
}

void maxdis(int num, Node *person)
{
int max = 0;
for (int i=0; i<num-1; i++)
{
if (person[i].distance<person[i+1].distance)
max=i+1;
}
cout<<"The person with the max distance is: \n\n"<<person[max].name<<endl<<person[max].ID<<endl<<person[max].distance<<endl;
return;
}

void IDsort(int num, Node *person)
{
int count=0;
cout<<"\nSorted by ID:"<<endl;
for (int i=0; i < num-1; i++)
{
for (int j=0; j<num-1-i; j++)
{
if (person[j].ID>person[j+1].ID)
{
int count=person[j].ID;
person[j].ID=person[j+1].ID;
person[j+1].ID=count;
}
}
cout<<"\n"<<person[count].name<<endl<<person[count].ID<<endl<<person[count].distance<<endl;
count++;
}
return;
}

void abcsort(int num, Node *person)
{
cout<<"\nSorted by name:"<<endl;
char temp[50];
for (int i=0; i<num; i++)
{
if (strcmp(person[i].name,person[i+1].name)>0)
{
strcpy(temp,person[i].name);
strcpy(person[i].name,person[i+1].name);
strcpy(person[i+1].name,temp);
}
}
for (int i=0;i<num;i++)
cout<<"\n"<<person[i].name<<endl<<person[i].ID<<endl<<person[i].distance<<endl;
return;
}


this is the print out:

* * * a1.txt is FOUND! * * *
==============================
April,Joe
3120
90

Matthews,Jocob
4592
88

Garfield,Kitty
8917
79

Lake,Bill
2233
85

Johnson,Jim
5672
67

Zumistan,Kim
6750
76

Larson,Mike
6718
99

Anderson,Eva
5672
80

The person with the max distance is:

Larson,Mike
6718
99

Sorted by ID:

April,Joe
3120
90

Matthews,Jocob
2233
88

Garfield,Kitty
4592
79

Lake,Bill
5672
85

Johnson,Jim
5672
67

Zumistan,Kim
5672
76

Larson,Mike
6718
99

Anderson,Eva
6750
80

Sorted by name:

April,Joe
0
90

Garfield,Kitty
2233
88

Lake,Bill
3120
79

Johnson,Jim
4592
85

Matthews,Jocob
5672
67

Larson,Mike
5672
76

Anderson,Eva
6718
99


6750
80


8917
0
In your ID sort:

1
2
3
4
5
6
7
8
9
10
11
for (int i=0; i < num-1; i++)
{
for (int j=0; j<num-1-i; j++)
{
if (person[j].ID>person[j+1].ID)
{
int count=person[j].ID;
person[j].ID=person[j+1].ID;
person[j+1].ID=count;
}
}


Should be:

1
2
3
4
5
6
7
8
9
10
11
12
13
for (int i=0; i < num; i++)
{
  for (int j=i; j<num; j++)
  {
    if (person[i].ID<person[j+1].ID)//Move all Smaller ID's to the back
    {
      int count=person[i].ID;
      person[i].ID=person[j+1].ID;
      person[j+1].ID=count;
    }
  }
  cout<<"\n"<<person[i].name<<endl<<person[i].ID<<endl<<person[i].distance<<endl;
}


Also because you have been actually moving around the variables in the array, you have actually corrupted your code because as you can see from the output in abcsort(), some names have the wrong distance or ID associated with them. So you have to come up with a better way of solving this then actually moving the contents of each array. Maybe use a copy of the input from the file to access the functions
Topic archived. No new replies allowed.