Thread: Please help me (C Programming)

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    1

    Thumbs up Please help me (C Programming)

    here's the code of the program, the problem is stated in the bottom.
    Code:
    #include <stdio.h>
    #define SIZE 10
    int SeatDis(int[], int);
    main() {
        static int Ar[SIZE] = { 0 };
        int a, ok;
            puts("Enter 1 for "First class"");
            puts("Enter 2 for "Econom class"");
            puts("Enter 3 to Quit");
                scanf_s("%d", &a);
    
    
                if (a == 1 || a == 2) {
                    ok = SeatDis(Ar, (a - 1) * 5);
                    if (ok != 0){
                        printf("Your seat num is %d\n", ok);
                        main();
                }
                        if (a == 1 && SeatDis(Ar, 5)) {
                            printf("No seats found in First Class\n");
                            printf("Wish to buy a seat in Econom class?(1 or Other = Yes : 0 = No)\n");
                            scanf_s("%d", &a);
                            if (a) {
                                printf("Your seat num is %d\n", SeatDis(Ar, 5));
                                main();
                            }
                        }
                        else {
                            if (a == 2 && SeatDis(Ar, 0)) {
                                printf("No seats found in Econom Class\n");
                                printf("Wish to buy a seat in First class?(1 = Yes : 2 = No)\n");
                                scanf_s("%d", &a);
                                if (a) {
                                    printf("Your seat num is %d\n", SeatDis(Ar, 0));
                                    main();
                                }
                            }
    
    
                            else printf("Next flight in three hours time from now...\n");
                        }
                    }
            else {
                if (a != 3) {
                    printf("Something went wrong...\n");
                    main();
                }
            }
        system("pause");
        return 0;
        }
    
    
    int SeatDis(int Ar[], int c) {
        for (int b = c + 5; c < b; c++) {
            if (Ar[c] == 0) {
                Ar[c] = 1;
                return c + 1;
            }
        }
        return 0;
    }


    The program asks you If you would like to buy a seat in the plane. Depending on your decision and amount of spare seats it reacts..
    In the plane first (1-5) seats are First class, 6-10 are Econom.
    The program works fine except of one bug that I can't figure out how to solve.
    When all of the seats in one class are taken It asks me if I want to change my class - I answer: 1 meaning ok, but instead of giving me the seat from start (1-5 or 6-10) It gives me 2-10 or 7-10 seats, meaning that one seat had been lost, because no one had bought it.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Some thoughts.
    1. Don't call main recursively. If you want a loop, use a loop.
    2. Use better variable names. Say
    int firstSeats[5]
    int economySeats[5]

    3. Your seatdis function appears to dispense seats. You need another function to check if a seat is available.
    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. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM

Tags for this Thread