Thread: Structure Array Does Not Save The Input From User

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    3

    Structure Array Does Not Save The Input From User

    Hi guys. I just joined cboard programming for a few days. I'm just a beginner of C language. It will be great if you help me with these code My professor gave me this assignment:

    The Colossus Airlines fleet consists of one plane with a seating capacity of 12. It makes one flightdaily. Write a seating reservation program with the following features:
    The program uses an array of 12 structures. Each structure should hold a seat identificationnumber, a marker that indicates whether the seat is assigned, the last name of the seat holder,and the first name of the seat holder.

    The program displays the following menu:
    To choose a function, enter its letter label:
    a. Show total number of empty seats:
    b. Show list of empty seats:
    c. List the assigned seats with name of person:
    d. Assign the customer to an empty seat.
    e. Delete seat assignment.
    q. Quit program.

    Write a function for each menu item.

    I wrote down the code. Everything works nearly perfect until I reached option "d". I input the customer to an empty seat, then choose the option "c". It showed that "Currently there are no seats have been assigned" even though I just input from the previous one The data seems not to be saved to structure array. I really need your helps. Thank you
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #define size 12
    typedef struct B{
        char lastname[20];
        char firstname[20];
    }name;
    
    
    typedef struct A{
        char seat_id[3];
        char status;
        name person;
    }seat;
    
    
    int number_empty_seats(seat a[]);
    void list_empty_seats(seat a[]);
    void list__assigned_seats(seat a[]);
    void assign_seat(seat *Seats);
    void delete_seat(seat *Seats);
    void quit(void);
    
    
    
    
    int main(void)
    { 
        seat seats[size] = {{"A1" ,'E'},{"A2", 'E'} ,{"A3",'E'}, {"A4",'E'},{"A5", 'E'},{"A6", 'E'},{"B1", 'E'},{"B2", 'E'},{"B3", 'E'}, {"B4",'E' },{"B5", 'E'},{"B6", 'E'}};
        seat *p = &seats[0];
        int choice;
        do
        {
            printf("\n******Menu******\n");
            printf("1. Show total number of empty seats: \n");
            printf("2.Show lists of empty seats: \n");
            printf("3.List of assigned seats with name of person: \n");
            printf("4.Assign the custormer to an empty seat: \n");
            printf("5.Delete assigned seat: \n");
            printf("6. Quit program\n");
            printf("\n------Enter Selection => ");
            scanf("%d", &choice);
            printf("\n");
                switch (choice)
                {
                case 1:
                    number_empty_seats(seats);
                    break;
    
    
                case 2:
                    list_empty_seats(seats);
                    break;
    
    
                case 3:
                    list__assigned_seats(seats);
                    break;
    
    
                case 4:
                    assign_seat(p);
                    break;
    
    
                case 5:
                    delete_seat(p);
                    break;
    
    
                case 6:
                    quit();
                    break;
                default:
                    printf("Invalid choice\n");
                }
        } while (choice != 6);
    }
    
    
    int number_empty_seats(seat a[])
    {
        int x;
        int count = 0;
        for (x = 0; x < size; x++)
        {
            if (a[x].status == 'E')
            {
                count = count++;
            }
        }
        printf("Number of empty seat is => %d \n", count);
        _getch();
        return count;
    }
    
    
    void list_empty_seats(seat a[])
    {
        int y, j, k;
        for (y = 0; y < size; y++)
        {
            if (a[y].status == 'E')
            {
                printf("%s is empty seat\n", a[y].seat_id);
            }
        }
        _getch();
    }
    void list__assigned_seats(seat a[])
    {
        int z;
        for (z = 0; z < size; z++)
        {
            if (a[z].status == 'F')
            {
                printf("Seat %s has been assigned to Custormer %s\n", a[z].seat_id, a[z].person);
            }
        }
        printf("\nCurrently there are no seats have been assigned");
        _getch();
    }
    void assign_seat(seat *Seats)
    {
        int g;
        int nums;
        for (g = 0; g < size; g++)
        {
            if (Seats[g].status == 'E')
            {
                printf("Enter custormer's First Name=> ");
                scanf("%s", &Seats[g].person.firstname);
                printf("Enter customer's Last Name=> ");
                scanf("%s", &Seats[g].person.lastname);
                printf("Do you want to assign seat %s to custormer %s %s\n", Seats[g].seat_id, Seats[g].person.lastname, Seats[g].person.firstname);
                printf("1=Yes/ 2=No? => ");
                scanf("%d", &nums);
                if (nums == 1)
                {
                    Seats[g].status == 'F';
                    printf("Seat %s has been assigned successfully to custormer %s %s\n\n", Seats[g].seat_id, Seats[g].person.lastname, Seats[g].person.firstname);
                    return;
                }
                else
                    printf("You have declined to assign the seat for custormer\n\n");
            }
        }
        _getch();
    }
    
    
    void delete_seat(seat *Seats)
    {
        int h, k;
        int nums;
        int choice;
        char PassengerLastName[20];
        printf("Seat ID\t\tCUstormer Name\n");
        for (h = 0; h < size; h++)
        {
            if (Seats[h].status == 'F')
            {
                printf("%s\t\t\t%s %s\n", Seats[h].seat_id, Seats[h].person.lastname, Seats[h].person.firstname);
            }
        }
        printf("\n");
        printf("Enter passenger Last Name you want to cancel seat => ");
        scanf("%s", &PassengerLastName);
        for (k = 0; k < size; k++)
        {
            if (Seats[k].status == 'F' && (Seats[k].person.lastname, PassengerLastName)==0)
            {
                printf("Are you sure you want to cancel seat of Custormer %s?\n", PassengerLastName);
                printf("1-Yes / 2-No\n");
                scanf("%d", &choice);
                if (choice == 1)
                {
                    printf("you are successful cancel the seat!!!\n");
                    return;
                }
                if (choice == 2)
                {
                    printf("You have declined\n");
                    return;
                }
            }
            _getch();
        }
    }
    
    
    void quit()
    {
        printf("You are now QUIT the program\n");
        printf("Thank you");
        _getch();
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    Seats[g].status == 'F';
    Remind yourself about the difference between "==" and "=" and then look at the above line from your code.

    I suggest turning up warnings on your compiler; most would have gave a warning on this line.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    3
    Ok I will Thank you

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void assign_seat(seat *Seats);
    > void delete_seat(seat *Seats);
    Why didn't you declare these as
    void assign_seat(seat a[]);
    void delete_seat(seat a[]);

    like all the others?

    They mean the same thing, but the change of style makes for odd reading.

    Similarly, your use of
    seat *p = &seats[0];
    is completely redundant, because
    assign_seat(p);
    can simply be replaced by
    assign_seat(seats);
    to match other usage.

    > count = count++;
    This doesn't do what you think it should.

    Just say count++;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Apr 2018
    Posts
    3
    My professor wants us to pass the pointer to the function assign_seat and delete_seat. So I have to use those

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by ethantruong1
    My professor wants us to pass the pointer to the function assign_seat and delete_seat. So I have to use those
    No, you don't. You're declaring a pointer parameter either way.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Ask your professor what the fundamental difference is between
    assign_seat(p);
    and
    assign_seat(seats);


    Because seats (in this context) is turned into &seats[0] by the compiler, so you're not achieving anything except a load of pointless typing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting user input for structure variables
    By themolejoel in forum C Programming
    Replies: 7
    Last Post: 10-17-2011, 10:49 PM
  2. Structure, user input and printing out results
    By alvarito in forum C Programming
    Replies: 3
    Last Post: 10-04-2011, 11:04 AM
  3. Replies: 5
    Last Post: 08-31-2011, 07:23 AM
  4. Setting a user's input as the structure name
    By Ste_Mulv in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 05:23 AM
  5. Replies: 3
    Last Post: 11-05-2001, 05:00 PM

Tags for this Thread