1,105,310 Community Members

Need help sorting an array of structs

Member Avatar
A&M_guy
Newbie Poster
3 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I am new to structs and I am trying to sort an array of structs but I can't seem to get it to work, I can't figure out what I am doing wrong and any assistance would be very much appreciated!

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int SIZE = 30;

struct people
{
       string last;
       string first;
       string telephone;
}info[SIZE];

void input_data(ifstream& in_data, people info[]);
//void sortValues(people info[]);
void bubbleSort(people info[]);

int main(){
    
    int x;
    
    ifstream in_data("directory.txt");

    if(in_data.fail()){
        cout << "Can't open the file\n";
        cin.get();
        return(0);
        }
          
    input_data(in_data, info);
    //sortValues(info);
    bubbleSort(info);
    
    cout << info[0].last << ", " << info[0].first;
    
cin.get();
return 0;    
}

void input_data(ifstream& in_data, people info[]){
    int i;
    for(i = 0; i < SIZE; i++)
        while(in_data.peek() != EOF)
        {
            getline(in_data, info[i].last, '\n');
            getline(in_data, info[i].first, '\n');
            getline(in_data, info[i].telephone, '\n');
            i++;
        }
}
/*
void sortValues(people info[], people p)
{
     int i = 0,
         j = 0,
         t = 0;
     for(i = 1; i < SIZE; i++)
     {
           for(j = 0; j < SIZE - i; j++)
           {
             if(info[j].last > info[j+1].last)
             {
                        t = info[j];
                        info[j] = info[j+1];
                        info[j+1] = t;
             }
           }
     }
}
   */  
void bubbleSort(people info[]) 
{
    bool doMore;
    do 
    {
        doMore = false;  // assume this is last pass over array
        for (int i=0; i<SIZE-1; i++) 
        {
            if (info[i].last > info[i+1].last) 
            {
                // exchange elements
                people temp = info[i]; 
                info[i] = info[i+1]; 
                info[i+1] = temp;
                doMore = true;  // after exchange, must look again
            }
        }
    } while (doMore);
}
Member Avatar
VernonDozier
Posting Expert
5,632 posts since Jan 2008
Reputation Points: 2,218 [?]
Q&As Helped to Solve: 768 [?]
Skill Endorsements: 26 [?]
Featured
 
0
 

The sort looks OK to me. What's wrong with the output? And have you verified that everything reads into the structures properly from the file?

Note : I assume you have precisely 30 elements? If not, all bets are off because your sort code assumes that. I notice a "peek" and an EOF test in the read-from-file stage, so perhaps you do NOT know you have exactly 30 elements. Since the array is completely uninitialized, you could be sorting garbage at the end of the list which makes it up to the front?

Member Avatar
A&M_guy
Newbie Poster
3 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The sort looks OK to me. What's wrong with the output? And have you verified that everything reads into the structures properly from the file?

Note : I assume you have precisely 30 elements? If not, all bets are off because your sort code assumes that. I notice a "peek" and an EOF test in the read-from-file stage, so perhaps you do NOT know you have exactly 30 elements. Since the array is completely uninitialized, you could be sorting garbage at the end of the list which makes it up to the front?

I think you are right about the garbage at the end, I tried to initialize the array when I declared it by using people info = {0}; but it gave me a syntax error. I am not sure how to get the sort function to only read filled elements. I did check that the information was being read in to the array fine...and when I run the sort instead of "last, first" being output I get " , " it is blank.

Member Avatar
VernonDozier
Posting Expert
5,632 posts since Jan 2008
Reputation Points: 2,218 [?]
Q&As Helped to Solve: 768 [?]
Skill Endorsements: 26 [?]
Featured
 
0
 

When in doubt, initialize with something printable, easily identifiable which no real record would contain, and which will almost certainly be at the end alphabetically...

const string INVALID_RECORD = "zzzzz";

// now before reading into the file, fill it all in with z's.  The real records will overwrite this.
for(int i = 0; i < SIZE; i++)
{
    info[i].last = INVALID_RECORD;
    info[i].first = INVALID_RECORD;
    info[i].telephone = INVALID_RECORD;
}

Stick this at the very top of main and see what happens.

Member Avatar
A&M_guy
Newbie Poster
3 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

When in doubt, initialize with something printable, easily identifiable which no real record would contain, and which will almost certainly be at the end alphabetically...

const string INVALID_RECORD = "zzzzz";

// now before reading into the file, fill it all in with z's.  The real records will overwrite this.
for(int i = 0; i < SIZE; i++)
{
    info[i].last = INVALID_RECORD;
    info[i].first = INVALID_RECORD;
    info[i].telephone = INVALID_RECORD;
}

Stick this at the very top of main and see what happens.

I just had one of the "ah ha" moments when you put this up! Thank you!

Question Answered as of 2 Years Ago by VernonDozier
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: