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