Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

If I could help doing this in C programming (not C++) that would help a lot. Tha

ID: 3804506 • Letter: I

Question

If I could help doing this in C programming (not C++) that would help a lot. Thank you.

The Colossus Airlines fleet consists of one plane with a seating capacity of 12. It makes one flight daily. Write a seating reservation program with the following features:


a. The program uses an array of 12 structures. Each structure should hold a seat identification number, a marker that indicates whether the seat is assigned, the last name of the seat holder, and the first name of the seat holder.

b. The program displays the following menu:

To choose a function, enter its letter label:

a) Show number of empty seats

b) Show list of empty seats

c) Show list of seats reserved (alphabetical by last name)

d) Assign a customer to a seat assignment

e) Delete a seat assignment

f) Quit

c. The program successfully executes the promises of its menu. Choices d) and e) require additional input, and each should enable the user to abort an entry.

d. After executing a particular function, the program shows the menu again (except for choice f).

e. Data is saved in a file between runs. When the program is restarted, it first loads in the data, if any, from the file.

Explanation / Answer


// C code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SEAT 12
#define MAXSEAT 30
#define INPUTFILE "input.txt"
struct seat
{
int passengerID;
char passengerMarker;
char passengerFrstName [MAXSEAT];
char passengerLastName [MAXSEAT];
};
struct seat aeroplane [SEAT] =
{
{1, 'N', "null", "null"},
{2, 'N', "null", "null"},
{3, 'N', "null", "null"},
{4, 'N', "null", "null"},
{5, 'N', "null", "null"},
{6, 'N', "null", "null"},
{7, 'N', "null", "null"},
{8, 'N', "null", "null"},
{9, 'N', "null", "null"},
{10, 'N', "null", "null"},
{11, 'N', "null", "null"},
{12, 'N', "null", "null"},
};

void printmenu (void);
void flush (void);
void initial (void);
void choicemethod (char);
void countempty (void);
void listempty (void);
void addSeat (void);
void deleteSeat (void);
void showalphabetical (void);
int main (void)
{
char choice;
while (printmenu (), scanf ( "%c", & choice) == 1 && choice != 'f')
{
flush ();
initial ();
choicemethod (choice);
}
}
void printmenu (void)
{
puts ( "=============") ;
puts ( "To choose a function, enter its letter label:");
puts ( "a) Show number of empty seats");
puts ( "b) Show list of empty seats");
puts ( "c) Show alphabetical list of seats");
puts ( "d) Assign a customer to a seat assignment");
puts ( "e) Delete a seat assignment");
puts ( "f) Quit");
puts ( "==============") ;
}
void flush (void)
{
while (getchar () != ' ')
continue;
}
void choicemethod (char ch)
{
switch (ch)
{
case 'a': countempty ();
break;
case 'b': listempty ();
break;
case 'c': showalphabetical ();
break;
case 'd': addSeat ();
break;
case 'e': deleteSeat ();
break;
case 'f': exit (1);
break;
default: break;
}
}
void initial (void)
{
FILE * fp;
struct seat temp;
int i;
if ((fp = fopen (INPUTFILE, "ab +")) == NULL)
{
fprintf (stderr, ". Can not open file%s ", INPUTFILE);
exit (2);
}
rewind (fp);
if (fread (& temp, sizeof (struct seat), 1, fp) != 1)
fwrite (aeroplane, sizeof (struct seat), SEAT, fp);
fclose (fp);
}
void countempty (void)
{
int i;
int ct;
FILE * fp;
struct seat temp;
if ((fp = fopen (INPUTFILE, "rb")) == NULL)
{
fprintf (stderr, ". Can not open file%s ", INPUTFILE);
exit (2);
}
rewind (fp);
for (ct = 0, i = 0; i <SEAT; i ++)
{
fread (& temp, sizeof (struct seat), 1, fp);
if (temp.passengerMarker == 'N')
ct ++;
}
puts ( " ********** ");
if (ct> 0)
printf ( " Now have%d seats have not ordered .", ct);
else
printf ( ". I'm so sorry, all seats has been ordered ");
puts ( "************* ");
fclose (fp);
}
void listempty (void)
{
int i, j;
FILE * fp;
struct seat temp;
if ((fp = fopen (INPUTFILE, "r + b")) == NULL)
{
fprintf (stderr, ". Can not open file%s ", INPUTFILE);
exit (3);
}
rewind (fp);
puts ( " **************** ");
for (j = 0, i = 0; i <SEAT; i ++)
{
fread (& temp, sizeof (struct seat), 1, fp);
if (temp.passengerMarker == 'N')
{
printf ( "SEAT% -5d ======> N ", temp.passengerID);
j ++;
}
}
if (j == 0)
{
printf ( "I'm so sorry, there is not seat ");
}
puts ( " ************* ");
fclose (fp);
}
void addSeat (void)
{
int i;
int ticket;
char ch;
FILE * fp;
struct seat temp;
if ((fp = fopen (INPUTFILE, "rw + b")) == NULL)
{
fprintf (stderr, ". Can not open file%s ", INPUTFILE);
exit (4);
}
rewind (fp);
printf ( "Input the seat number you want to order (1 to%d) ", SEAT);
while (scanf ( "%d", & ticket) == 1)
{
if (ticket <1 || ticket> 12)
{
printf ( "Invalid input (1-%d)", SEAT);
continue;
}
for (i = 0; i <SEAT; i ++)
{
fread (& temp, sizeof (struct seat), 1, fp);
if (ticket == temp.passengerID && temp.passengerMarker == 'N')
{
printf ( "? SEAT%d have not been ordered, are you sure (y / n) ", temp.passengerID);
flush ();
scanf ( "%c", & ch);
flush ();
if (strchr ( "Yy", ch))
{
temp.passengerMarker = 'Y';
puts ( "Enter your first name.");
gets (temp.passengerLastName);
puts ( "Enter your last name.");
gets (temp.passengerFrstName);
fseek (fp, -sizeof (struct seat), SEEK_CUR);
fwrite (& temp, sizeof (struct seat), 1, fp);
printf ( " SEAT%d order successfull%s%s ", ticket, temp.passengerLastName, temp.passengerFrstName);
fclose (fp);
return;
}
else
{
fclose (fp);
flush ();
return;
}
}

}
rewind (fp);
printf ( "SEAT%d has already been ordered, choice others .", ticket);
}

}
void deleteSeat (void)
{
int i;
char ch;
FILE * fp;
struct seat temp;
char passengerLastName [14];
char passengerFrstName [14];
if ((fp = fopen (INPUTFILE, "rw + b")) == NULL)
{
fprintf (stderr, ". Can not open file%s ", INPUTFILE);
exit (5);
}
rewind (fp);
puts ( "Input your firstname.");
gets (passengerLastName);
puts ( "Input your last name.");
gets (passengerFrstName);
for (i = 0; i <SEAT; i ++)
{
fread (& temp, sizeof (struct seat), 1, fp);
if (strcmp (temp.passengerLastName, passengerLastName) == 0 && strcmp (temp.passengerFrstName, passengerFrstName) == 0)
{
printf ( "%s%s you have ordered seat%d, are you sure unordered (y / n)?", passengerLastName, passengerFrstName, temp.passengerID);
scanf ( "%c", & ch);
if (strchr ( "Yy", ch))
{
temp.passengerMarker = 'N';
strcpy (temp.passengerLastName, "null");
strcpy (temp.passengerFrstName, "null");
fseek (fp, -sizeof (struct seat), SEEK_CUR);
fwrite (& temp, sizeof (struct seat), 1, fp);
printf ( " SEAT%d unorder successfully%s%s ", temp.passengerID, passengerLastName, passengerFrstName);
fclose (fp);
flush ();
return;
}
else
{
flush ();
fclose (fp);
return;
}
}
}
printf ( " %s%s you have not ordered any seat .", passengerLastName, passengerFrstName);
}
void showalphabetical (void)
{
int i, j, k;
FILE * fp;
char fulpassengerFrstName [2 * MAXSEAT];
struct seat temp;
struct seat passenger [SEAT];
if ((fp = fopen (INPUTFILE, "rb")) == NULL)
{
fprintf (stderr, ". Can not open file%s ", INPUTFILE);
exit (6);
}
for (j = 0, i = 0; i <SEAT; i ++)
{
fread (& temp, sizeof (struct seat), 1, fp);
if (strcmp (temp.passengerLastName, "null") != 0 && strcmp (temp.passengerFrstName, "null") != 0)
passenger [j ++] = temp;
}
fclose (fp);
puts ( " ************ ");
if (j == 0)
{
printf ( " There has nobody ordered any seat .");
puts ( " ********* ");
return;
}
for (i = 0; i <j; i ++)
for (k = i; k <j; k ++)
{
if (strcmp (passenger [i] .passengerLastName, passenger [k] .passengerLastName)> 0)
{
temp = passenger [i];
passenger [i] = passenger [k];
passenger [k] = temp;
}
if (strcmp (passenger [i] .passengerLastName, passenger [k] .passengerLastName) == 0)
if (strcmp (passenger [i] .passengerFrstName, passenger [k] .passengerFrstName)> 0)
{
temp = passenger [i];
passenger [i] = passenger [k];
passenger [k] = temp;
}
}
for (i = 0; i <j; i ++)
{
strcpy (fulpassengerFrstName, passenger [i] .passengerLastName);
strcat (fulpassengerFrstName, passenger [i] .passengerFrstName);
printf ( "%-30s ordered SEAT%d ", fulpassengerFrstName, passenger [i] .passengerID);
}
puts ( " ********** ");
}


/*
output:

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
a

**********

Now have12 seats have not ordered

.*************

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
b

****************

SEAT 1 ======>    N
SEAT 2 ======>    N
SEAT 3 ======>    N
SEAT 4 ======>    N
SEAT 5 ======>    N
SEAT 6 ======>    N
SEAT 7 ======>    N
SEAT 8 ======>    N
SEAT 9 ======>    N
SEAT 10 ======>    N
SEAT 11 ======>    N
SEAT 12 ======>    N

*************

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
c

************

There has nobody ordered any seat

.
*********

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
d
Input the seat number you want to order (1 to12)
3
? SEAT3 have not been ordered, are you sure (y / n)
y
Enter your first name.
jason
Enter your last name.
roy

SEAT3 order successfulljasonroy

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
d
Input the seat number you want to order (1 to12)
3
SEAT3 has already been ordered, choice others
.1
? SEAT1 have not been ordered, are you sure (y / n)
y
Enter your first name.
alex
Enter your last name.
hales

SEAT1 order successfullalexhales

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
b

****************

SEAT 2 ======>    N
SEAT 4 ======>    N
SEAT 5 ======>    N
SEAT 6 ======>    N
SEAT 7 ======>    N
SEAT 8 ======>    N
SEAT 9 ======>    N
SEAT 10 ======>    N
SEAT 11 ======>    N
SEAT 12 ======>    N

*************

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
c

************

alexhales ordered SEAT1
jasonroy ordered SEAT3

**********

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
d
Input the seat number you want to order (1 to12)
2
? SEAT2 have not been ordered, are you sure (y / n)
y
Enter your first name.
jason
Enter your last name.
roy

SEAT2 order successfulljasonroy

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
c

************

alexhales ordered SEAT1
jasonroy ordered SEAT2
jasonroy ordered SEAT3

**********

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
e
Input your firstname.
jason
Input your last name.
roy
jasonroy you have ordered seat2, are you sure unordered (y / n)?y

SEAT2 unorder successfullyjasonroy

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
c

************

alexhales ordered SEAT1
jasonroy ordered SEAT3

**********

=============
To choose a function, enter its letter label:
a) Show number of empty seats
b) Show list of empty seats
c) Show alphabetical list of seats
d) Assign a customer to a seat assignment
e) Delete a seat assignment
f) Quit
==============
f

*/

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote