Home > Blockchain >  How do I pass a airplane row and seat to a txt file together with its passenger name etc.?
How do I pass a airplane row and seat to a txt file together with its passenger name etc.?

Time:06-19

We need to make a program for airplane seat reservation. Like we need a program which can reserve, change, delete, and display seats. But our program needs also to be written in a txt file as the user inputs data (name, age, city, seat-e.g., 1F). And I have made a program myself. It runs but exits halfway. And I am not sure if what I'm doing is right. I kind of get the gist on how I can pass informations using structs. But I don't know how I will the seats also in the txt file.

I have these on my first lines:

#include<stdio.h>
#include<malloc.h>

struct Passenger{
    int pnum;
    int age;
    char city[50];
    char name[50];
    };

struct Seats{
    int row;
    char seats[10];
};

struct Airplane{
    struct Seats seats[6];
};

void intializeDefault(struct Airplane *airplane){
    for(int i=0;i<10;i  ){
        char ch='A';
        airplane->seats[i].row=i 1;
        for(int j=0;j<6;j  ){
            airplane->seats[i].seats[j]=ch;
            ch  ;
        }
    }
}

And well we kind of have commands, so I put it like:

int main(){
    int ch;
    struct Airplane airplane;
    intializeDefault(&airplane);
    do{
        printf("Welcome to our Airlines!\n");
        
        printf("\nOptions:");
        printf("\n[1] Book a seat.");
        printf("\n[2] Display seats.");
        printf("\n[3] Change seat.");
        printf("\n[4] Delete reservation.");
        printf("\n[5] Display reservation records.");
        printf("\n[0] Exit.");
        printf("\nWhat can we do for you? (Enter number.):  ");
        scanf("%d", &ch);
    
        switch(ch){
            case 1:book();
                break;
            case 2:display(&airplane);
                break;
            case 3:change();
                break;
            case 4:deleteSeat();
                break;
            case 5:record();
            break;
            default:    
                break;  
        }
    }while (ch!=0);
    {
        printf("\nThank you for booking !");
    };

    return 0;
}

What I need help on is how should I do on this part:

void book(){
    struct Passenger *p;
    FILE *fp;
    
    struct Airplane airplane;
    intializeDefault(&airplane);
    int totalSeats = 60;
    int seatsBooked = 0;
    int n, i, j, row;
    char seat;
    printf("How many passengers/seats you want to reserve?: ");
    scanf("%d", &n);
    
    p = (struct Passenger*) malloc(n * sizeof(struct Passenger));
    
    fp=fopen("Passengers.txt", "w");
    
    if(seatsBooked==60){
            printf("\nAll seats are booked now.");
        }
        
    for(i=0;i<n;i  ){
        printf("Enter Roll Number: ");
        scanf("%d", p[i].pnum);
        
        fflush(stdin);
        printf("Enter #%d passenger's name: ", i 1);
        scanf("%[^\n]s", p[i].name);
        
        fflush(stdin);
        printf("What's the passenger's age?: ");
        scanf("%d", p[i].age);
    
        fflush(stdin);
        printf("Please enter the city where the passenger resides: ");
        scanf("%d",p[i].city);
                
        fflush(stdin);
        printf("\nEnter Seat row: (Type 1-10) ");
        scanf("%d", &row);
                
        fflush(stdin);
        printf("Enter Seat column: (Type A-F) ");
        scanf("%c", &seat);
        
            
        if(airplane.seats[row-1].seats[seat-'A']!='X'){
        airplane.seats[row-1].seats[seat-'A']='X';
        seatsBooked  ;
        printf("\nSeat %d%c is booked. \n\n", row, seat);
                    }
        else{
            printf("\nSeat %d%c is already booked.\n", row, seat);
            }
        
    }
    fclose(fp);
}

I just need to know on how I can also pass every passenger's seats within on them, and I think I will figure out the other parts. It's because we need to show the records of passengers and their seat reservations when we command "5", and I need to show "X" on every reserved seats when we command "2". And I am not quite sure how I will do that. Should I change the functions on above? The structs? Or some lines?

CodePudding user response:

I gave a quick look and I think that you should keep track of the booked seat by the single user... In your "Seats" struct, why don't you add an array of "Passenger" pointers in order to link every seat to the respective user? As soon a user books a seat, you should have the index of the booked seat in a specific row. You take the char array and you put an 'X' in that position. Then you take the Passenger* array and in the same position you assign the address of that client's Passenger object.

CodePudding user response:

The one suggestion I would make is to pass along your airplane structure to your various functions. For example:

    switch(ch){
        case 1:book(&airplane);
        break;

Then, down in your "book" function you would use the airplane structure in your code.

void book(Airplane *airplane){
    struct Passenger *p;
    FILE *fp;

    //struct Airplane airplane; /* Or just remove this line of code */
    

You might want to repeat the referencing of the airplane structure in your other functions as well.

Hope that helps.

Regards.

  • Related