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);
}