How do I make selection sort work with strings in a structure?? My compiler doesn't seem to like my variable assignments under the show_alpha() function. :cry:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SMAX 12
#define LEN 20
#define EMPTY 0

void show_empty_num(struct data *);
void show_empty_list(struct data *);
void show_alpha(struct data *, FILE *, int);
void assign_seat(struct data *, FILE *, int);
void del_seat(struct data *, FILE *, int);
void quit();
void prn_menu();

struct data {
	int seatid[1];
	int seatm[1];
	char first[LEN];
	char last[LEN];
};

int main(void)
{
	struct data flight[SMAX];
	FILE *fp;
	char input;
	int size = sizeof (struct data);
	int i = 0, cnt = 0;

	while (i < SMAX)
	{
		flight[i].seatm[0] = 0;
		i++;
	}

	if ((fp = fopen("file1.txt", "a+b")) == NULL)
	{
		puts("Error opening file");
		exit(0);
	}

	rewind(fp);

	while (cnt < SMAX && fread(&flight[cnt], size, 1, fp) == 1)
		cnt++;

	prn_menu();

	while ((input = getchar()) != EOF)
	{
		switch (input)
		{
			case 'a':
				show_empty_num(flight);
				prn_menu();
				break;
			case 'b':
				show_empty_list(flight);
				prn_menu();
				break;
			case 'c':
				show_alpha(flight, fp, size);
				prn_menu();
				break;
			case 'd':
				assign_seat(flight, fp, size);
				prn_menu();
				break;
			case 'e':
				del_seat(flight, fp, size);
				prn_menu();
				break;
			case 'f':
				fclose(fp);
				quit();
				break;
		}
	}
	return 0;
}

void show_empty_num(struct data *flight)
{
	int cnt = 0, i = 0;
	while (cnt < SMAX)
	{
		if (flight[cnt].seatm[0] == EMPTY)
			i++;
		cnt++;
	}
	printf("Number of empty seats = %d\n", i);
}

void show_empty_list(struct data *flight)
{
	int cnt = 0;
	while (cnt < SMAX)
	{
		if (flight[cnt].seatm[0] == EMPTY)
			printf("Seat %d is empty\n", cnt);
		cnt++;
	}
}

void show_alpha(struct data *flight, FILE *fp, int size)
{
	int i = 0, j = 0, cnt = 0, tmp = 0;
	char *temp;

	for (i = 0; i < SMAX - 1; i++)
	{
		for (j = i + 1; j < SMAX; j++)
			if (strcmp(flight[i].first, flight[j].first) < 0)
			{
				temp = flight[i].first;
				flight[i].first = flight[j].first;
				flight[j].first = temp;

				temp = flight[i].last;
				flight[i].last = flight[j].last;
				flight[j].last = temp;

				tmp = flight[i].seatid[0];
				flight[i].seatid[0] = flight[j].seatid[0];
				flight[j].seatid[0] = tmp;

			}
	}

	while (cnt < SMAX)
	{
		printf("%s %s ID No: %d\n", flight[cnt].first, flight[cnt].last, flight[cnt].seatid[0]);
		cnt++;
	}
	
	cnt = 0;
	while (cnt < SMAX)
		fread(&flight[cnt], size, 1, fp);
}

void assign_seat(struct data *flight, FILE *fp, int size)
{
	char ans;
	int num;
	puts("Enter the seat number. 99 to stop:");

	while (scanf("%d", &num) == 1)
	{
		if (num == 99) 
			break;
		if (num > SMAX || num < 0 || flight[num].seatm[0] == 1)
		{
			puts("Not a valid number or seat is occupied!");
			puts("Enter the seat number. 99 to stop:");
			continue;
		}
		else
		{
			flight[num].seatm[0] = 1;
			puts("Enter a seat ID:");
			scanf("%d", &flight[num].seatid[0]);
			
			fflush(stdin);
			
			puts("Enter a first name:");
			gets(flight[num].first);
			puts("Enter a last name:");
			gets(flight[num].last);
			fwrite(&flight[num], size, 1, fp);			
		}
		puts("Would you like to assign another seat? (Y)es or (N)o");
		scanf("%c", &ans);
		if (ans == 'Y')
		{
			puts("Enter the seat number. 99 to stop:");
			continue;
		}
		else
			break;
	}
	
}

void del_seat(struct data *flight, FILE *fp, int size)
{
	int num;
	char ans;
	puts("Enter a seat number 0-11. 99 to stop:");
	
	while (scanf("%d", &num) == 1)
	{
		if (num == 99) 
			break;
		if (num > SMAX || num < 0 || flight[num].seatm[0] == 0)
		{
			puts("Not a valid number or seat is empty!");
			puts("Enter a seat number beteen 0-11. 99 to stop:");
			continue;
		}
		else
		{
			flight[num].seatid[0] = EMPTY;
			flight[num].seatm[0] = EMPTY;
			flight[num].first[0] = '\0';
			flight[num].last[0] = '\0';
			fwrite(&flight[num], size, 1, fp);
		}
		fflush(stdin);
		puts("Would you like to delete another seat? (Y)es or (N)o");
		scanf("%c", &ans);
		if (ans == 'Y')
		{
			puts("Enter a seat number 0-11. 99 to stop:");
			continue;
		}
		else
			break;
	}
}

void prn_menu()
{
	puts("To choose a function, enter its label:");
	puts("a) Show number of empty seats");
	puts("b) Show list of empty seats");
	puts("c) Show alphabetical list of seat assignments");
	puts("d) Assign a customer to a seat");
	puts("e) Delete a seat assignment");
	puts("f) Quit");
}

void quit()
{
	puts("Have a nice day !");
	exit(0);
}

Recommended Answers

All 4 Replies

you are attempting to use c++ techniques on C strings. use strcpy() to copy one string to another

char tmp[LEN];
    strcpy(tmp, flight[i].first);

However, that is not really what you want to do anyway -- it takes to much time to swap individual members of a structure. You want to swap the entire structure in one shot.

struct data temp;
...
...
  // now swap the structures
   memcpy(&temp,&flight[i], sizeof(struct data));
   memcpy(&flight[i], &flight[j], sizeof(struct data));
   memcpy(&flight[j], &temp, sizeof(struct data));

well nevermind i figured out the first part of the question that i had.

my next question has to do with changing the contents of the array of structures. How do I keep the original items from being altered?
Thanks in advance for any replies you can give.

my next question has to do with changing the contents of the array of structures. How do I keep the original items from being altered?

make a duplicate copy of the array (or save it in a data file) before it is sorted.

Here, see this, maybe you'll find it useful

#include <stdio.h>
#include <string.h>

#define MAX_NUM 20
#define NUM 5

/* It's sometimes clever to create an array of pointers to structures, because
swapping structures may be very expensive since structures can be large. 
With array of pointers you actually sort pointers (4 bytes on mosth 32 bit machines)
but not the actual data. This is just example, may be you'll find it useful. */

typedef struct Example
{
	int data;
	char name[MAX_NUM];
	/* other data */
}example;

void swap_pointers (example** pex1, example** pex2)
{
	example* tmp;

	tmp = *pex1;
	*pex1 = *pex2;
	*pex2 = tmp;
}

int main (void)
{
	example array_struct[NUM];
	example* array_pointers[NUM];/* for easier and more efficient sorting */
	int i, j;
	int min;

	/* Just for example, I'll fill structures with string names */
	strcpy (array_struct[0].name, "Asim");
	strcpy (array_struct[1].name, "Asmir");
	strcpy (array_struct[2].name, "Emir");
	strcpy (array_struct[3].name, "Nermin");
	strcpy (array_struct[4].name, "Jasmin");
	
	for (i = 0; i < NUM; i++)
	{
		array_pointers[i] = &array_struct[i];
	}

	/* now, here goes implementation of selection sort algorithm on structures*/

	for (i = 0; i < NUM; i++)
	{
		min = i;
		for (j = i + 1; j < NUM; j++)
		{
			if (strcmp (array_pointers[min]->name, array_pointers[j]->name) > 0)
			{
				min = j;
			}
		}
		swap_pointers (&array_pointers[i], &array_pointers[min]);
	}
	/*	As you can see, you're not actually sorting the array of structures, but 
		array of pointers to structures. Now you can display data in sort order or 
		write them to file. */
	for (i = 0; i < NUM; i++)
	{
		printf("%s ", array_pointers[i]->name);
	}

	return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.