6 Replies - 1087 Views - Last Post: 13 October 2011 - 07:49 PM Rate Topic: -----

#1 informationJunkie121  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-October 11

sorting struct array from file

Posted 13 October 2011 - 01:42 PM

hello! this is my first time posting and I was wondering if I could get some help. This program that I've written is supposed to get a list of usernames from a file listed like this: lastname firstname middleinitial ... it's then supposed to sort them in alphabetical order using the last name.

I'm not getting any errors. It's not sorting at all. I'm not sure what I'm doing wrong. I think I may need a fresh new pair of eyes as I've been looking at this for too long. I'm pretty sure the answer is super simple. I'm just not seeing it.


Anyone have any suggestions? Thank you.


  
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const int MAXNUMBER = 50;

struct Person
{
	string firstName;
	string lastName;
	char middleInitial;
	int order;
};

void lastNameSort(Person* [], int );


int main()
{
	ifstream inFile;
	inFile.open( "listOfUserID" );

	if ( inFile )
	{
		cout << "File is found." << endl;

		Person* userPointer[ MAXNUMBER ];

		string first,
			   last;
		char initial;
		int order = 0;

		while ( !inFile.eof() )
		{
			inFile >> last >> first >> initial;
			userPointer[ order ]  = new Person;
			userPointer[ order ]->lastName = last;
			(*userPointer[ order ] ).firstName = first;
			userPointer[ order ] ->middleInitial = initial;
			userPointer[ order ] ->order = order+1;
			order++;
		}
		inFile.close();
		--order;

		for ( int i = 0; i< order; i++ )

			cout << userPointer[ i ]->lastName 
				 << "  " << userPointer[i]->firstName
				 << "  " << userPointer[i]->middleInitial
				 << "  " << userPointer[i]->order << endl;

		cin.get();
		lastNameSort(  userPointer, order );

		cin.get();

		for ( int i = 0; i< order; i++ )
			cout << userPointer[ i ]->lastName 
				 << "  " << userPointer[i]->firstName
				 << "  " << userPointer[i]->middleInitial
				 << "  " << userPointer[i]->order << endl;

	}
	else
	{
		cout << "cannot find file" << endl;
		cin.get();
		return 1;
	}

	cin.get();
	return 0;
}

void swap( Person*&, Person*& );

void lastNameSort( Person* p[], int howMany )
{
	int indexOfSmallest;
	string smallest;

	for ( int topOfList=0; topOfList < howMany-1; topOfList++ )
	{
		indexOfSmallest = topOfList;
		smallest = p[indexOfSmallest]->lastName;

		for ( int current = topOfList+1; current<howMany; current++ )
		{
			if ( p[current]->lastName.compare( smallest ) < 0 )
			{
				indexOfSmallest = current;
				smallest = p[indexOfSmallest]->lastName;
			}
		}

		if ( indexOfSmallest != topOfList )
		{
			swap( p[indexOfSmallest], p[topOfList] );
		}
	}
}

void swap( Person*& a, Person*& b )
{
	Person* temp = a;
	a = b;
	b = temp;
}



Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: sorting struct array from file

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: sorting struct array from file

Posted 13 October 2011 - 05:14 PM

It worked for me.

Perhaps it is the swap you could try swapping in the loop or using a pointer to a Person pointer for the parameters.
Was This Post Helpful? 0
  • +
  • -

#3 informationJunkie121  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-October 11

Re: sorting struct array from file

Posted 13 October 2011 - 05:54 PM

It's still not working for me. The file reads fine. It's when once it's read, it doesn't sort it and list the new sorted order by last name. It may be in my function or prototype in this part that it's not sorting. i think i did the pointers correctly, but it's still not sorting and listing the new sort.
	for ( int current = topOfList+1; current<howMany; current++ )
		{
			if ( p[current]->lastName.compare( smallest ) < 0 )
			{
				indexOfSmallest = current;
				smallest = p[indexOfSmallest]->lastName;
			}
		}


Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: sorting struct array from file

Posted 13 October 2011 - 06:44 PM

Does it print anything after the sort?

You can try a different swap function.


// prototype
void swap( Person**, Person**);

  ...

  // call
  swap( &p[indexOfSmallest], &p[topOfList] );

  ...

// definition
void swap( Person* *a, Person* *b )
{
	Person* temp = *a;
	*a = *b;
	*b = temp;
}


Was This Post Helpful? 1
  • +
  • -

#5 informationJunkie121  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-October 11

Re: sorting struct array from file

Posted 13 October 2011 - 07:01 PM

That worked! I guess I needed a pointer to a pointer. It started printing after sort after I modified the for else loop in the main function. 2 days of reading this code and it was just a bunch of * and & that I needed in the function. Plus i learned something new.

Thank you so much!
Was This Post Helpful? 0
  • +
  • -

#6 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: sorting struct array from file

Posted 13 October 2011 - 07:10 PM

Unless the sort function is part of some assignment, you are much better off using the built in sort functionality. If you do that, the only thing you need to do is teach it how to compare your objects of your class and your array will be sorted cleanly and efficiently. It would look something like this.

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>

using namespace std;

const int MAXNUMBER = 50;

struct Person
{
	string firstName;
	string lastName;
	char middleInitial;
	int order;
};

bool compare_person(const Person* person1, const Person* person2) {
    return person1->lastName < person2->lastName;
}

int main()
{
	ifstream inFile;
	inFile.open( "listOfUserID.txt" );

	if ( inFile )
	{
		cout << "File is found." << endl;

		Person* userPointer[ MAXNUMBER ];

		string first,
			   last;
		char initial;
		int order = 0;

		while ( !inFile.eof() )
		{
			inFile >> last >> first >> initial;
			userPointer[ order ]  = new Person;
			userPointer[ order ]->lastName = last;
			(*userPointer[ order ] ).firstName = first;
			userPointer[ order ] ->middleInitial = initial;
			userPointer[ order ] ->order = order+1;
			order++;
		}
		inFile.close();
		--order;

		for ( int i = 0; i< order; i++ )

			cout << userPointer[ i ]->lastName 
				 << "  " << userPointer[i]->firstName
				 << "  " << userPointer[i]->middleInitial
				 << "  " << userPointer[i]->order << endl;

		std::sort(userPointer, userPointer+order, compare_person); //i used order here because it seems to contain the number of users read


		for ( int i = 0; i< order; i++ )
			cout << userPointer[ i ]->lastName 
				 << "  " << userPointer[i]->firstName
				 << "  " << userPointer[i]->middleInitial
				 << "  " << userPointer[i]->order << endl;

	}
	else
	{
		cout << "cannot find file" << endl;
		cin.get();
		return 1;
	}

	return 0;
}


This post has been edited by Karel-Lodewijk: 13 October 2011 - 07:11 PM

Was This Post Helpful? 1
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1329
  • View blog
  • Posts: 4,555
  • Joined: 19-February 09

Re: sorting struct array from file

Posted 13 October 2011 - 07:49 PM

View PostinformationJunkie121, on 14 October 2011 - 05:01 AM, said:

That worked! I guess I needed a pointer to a pointer. It started printing after sort after I modified the for else loop in the main function. 2 days of reading this code and it was just a bunch of * and & that I needed in the function. Plus i learned something new.

Thank you so much!


Very good. When data is passed to a function as a parameter the data is copied. If you want to change the data, pointers (or references) are used. If the data is a pointer, then a pointer is still needed to point to the data (which is a pointer itself). :crazy:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1