Im having a hard time on how to display the total price when i choose roundtrip, on how to display the price and the total price.. someone please help me pls pls pls

Here are the requirements:

A. Log in by Username and password
B. User can choose either one way or roundtrip with selection of dates
C. Flight options with different PRICES, SEATING CAPACITY and AVAILABLE SEATS.
D. The user can book up to TEN TICKETS per transaction (even for different flights)
E. Proper notifications or error messages must be observed like in the case where the user wants to book a flight with ZERO available seats, etc.
F. Once all the tickets were booked, display a summary of the transaction including the TOTAL PRICE and TOTAL NUMBER OF TICKETS

Your software must have the following: Basic I/O statements of C-language, Conditional Statements, Looping Statements, functions, arrays, strings, structures, pointers and memory allocation. You may include logical assumptions as part of your program.

And here's my code so far

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void header()
{
    printf("\nFlight                Price   Seating Capacity    Available Seats\n");
}
void option1()
{
    printf("\nFlight                Price   Seating Capacity    Available Seats\n");
    printf("1. Manila To Hong Kong      1,500   100         16\n");
    printf("2. Manila To Caticlan       1,000   75          0\n");
    printf("3. Manila To Singapore      3,000   100         24\n");

}
void option2()
{
    printf("\nFlight                Price   Seating Capacity    Available Seats\n");
    printf("4. Manila To Hong Kong      1,700   100         18\n");
    printf("5. Manila To Caticlan       2,000   75          24\n");
    printf("6. Manila To Singapore      4,000   100         65\n");

}

void option3()
{
    printf("\nFlight                Price   Seating Capacity    Available Seats\n");
    printf("7. Manila To Hong Kong      2,000   100         46\n");
    printf("8. Manila To Caticlan       1,500   75          16\n");
    printf("9. Manila To Singapore      4,000   100         85\n");

}
int choice()
{

}

struct flight{
    int ticket;
    int price;
};

int main() {
    struct flight book[10];
    char user[50], pw[50], adminu[50]="jon", adminpw[50]="snow";
    int trip, date, date2, a, x, y;
    float total;
    char repeat;
    printf("\nEnter username: ");
    scanf("%s", &user);
    printf("\nEnter password: ");
    scanf("%s", &pw);
    if (strcmp(user, adminu)==0 && strcmp(pw, adminpw)==0)
    {
        printf("\nWelcome");

        printf("\nChoose if one way trip or roundtrip: ");
        printf("\n1. One way trip \n2. Roundtrip\n");
        scanf("\n%d", &trip);
        printf("\nDate: ");
        printf("\n1. August 10, 2017 \n2. August 11, 2017\n3. August 12, 2017(not applicable for round trip)\n");
        scanf("%d", &date);

        switch (trip)
        {

        case 1: 
        {
            if (date==1)
            {
                printf("\nAugust 10, 2017\n");
                option1();
                printf("\nHow many tickets will you get?:");
                scanf("%d", &x);
                for (a=1; a<=x; a++)
                {
                    printf("\nPlease select which flight you will book: ");
                    scanf("%d", &book[a].ticket);
                }
            }

            else if (date==2)
            {

            printf("\nAugust 11, 2017\n");
            option2();
            printf("\nHow many tickets will you get?:  ");
            scanf("%d", &x);
            for (a=1; a<=x; a++)
                {
                    printf("\nPlease select which flight you will book: ");
                    scanf("%d", &book[a].ticket);
                }

            }
            else if (date==3)
            {
                printf("\nAugust 12, 2017\n");
                option3();
                printf("\nHow many tickets will you get?: ");
                scanf("%d", &x);
                for (a=1; a<=x; a++)
                {
                    printf("\nPlease select which flight you will book: ");
                    scanf("%d", &book[a].ticket);
                }
            }

            else printf("\nInvalid");
            break;
        }

        case 2:
            {
            if (date==1)
            {
                printf("\nAugust 10, 2017\n");
                option1();
                printf("\nChoose date of return: \n1. August 11, 2017\n2. August 12, 2017\n");
                scanf("%d", &date2);
                printf("\nHow many tickets will you get (excluding the return trip)?: ");
                scanf("%d", &x);
                for (a=1; a<=x; a++)
                {
                    printf("\nPlease select which flight you will book: ");
                    scanf("%d", &book[a].ticket);
                    if (date2==1)
                    {

                    switch (book[a].ticket)
                    {
                        case 1: 
                        {
                            printf("\nDetails for return trip");
                            header();
                            printf("Hong Kong to Manila     2,000   100         45\n"); break;
                        }
                        case 2: 
                        {
                            printf("\nDetails for return trip");
                            header();
                            printf("Caticlan to Manila      1,500   75          67\n"); break;
                        }
                        case 3: 
                        {
                            printf("\nDetails for return trip");
                            header();
                            printf("Singapore to Manila     4,000   100         46\n"); break;
                        }
                        default: break;
                    }
                    }
                    else if (date2==2)
                    {
                        switch (book[a].ticket)
                    {
                        case 1: 
                        {
                            header();
                            printf("\nHong Kong to Manila       2,000   100         45"); break;
                        }
                        case 2: 
                        {
                            header();
                            printf("\nCaticlan to Manila        1,500   75          67"); break;
                        }
                        case 3: 
                        {
                            header();
                            printf("\nSingapore to Manila       4,000   100         46"); break;
                        }
                        default: break;
                    }
                    }
                    else 
                    {
                    printf("\nInvalid"); 
                    return 0;
                    }
                }

            }

            else if (date==2)
            {

            printf("\nAugust 11, 2017\n");
            option2();
            printf("\nHow many tickets will you get?: ");
            scanf("%d", &x);
            for (a=1; a<=x; a++)
                {
                    printf("\nPlease select which flight you will book: ");
                    scanf("%d", &book[a].ticket);
                }

            }
            else if (date==3)
            {
                printf("\nAugust 12, 2017\n");
                option3();
                printf("\nHow many tickets will you get?: ");
                scanf("%d", &x);
                for (a=1; a<=x; a++)
                {
                    printf("\nPlease select which flight you will book: ");
                    scanf("%d", &book[a].ticket);
                }
            }

            else printf("\nInvalid");
            break;
                break;
            }
            default:
             break;
        }

        printf("\n");

    printf("\nSummary: ");
    for (a=1; a<=x; a++)
    {
    printf("\nDetails of Ticket no. [%d]", a);
    printf("\nFlight no. [%d]\n", book[a].ticket);

    }

    total=book[a].price;
    printf("\nTotal number of tickets: %d\n", x);
    printf("\nTotal Price: %0.2f", total);
}

    else 
    {
    printf("\nInvalid login");
    }   

    return 0;
}

Recommended Answers

All 4 Replies

Just an observation. You have 240 lines here and if you could be nice you would highlight or point out which lines you are having trouble with rather than have everyone find where the relevant lines are.

To troubleshoot this you could print values where you suspect an issue. I don't see any lines that are there to debug the code.

Let me pick on say Line 68. No comment? Just let those that are to debug your code try to figure out why 1 here? Think about the future and code it with meaningful variables and comments. As it stands I have to remember why trip is 1 or 0.

The spec seems to give you a lot of latitude on how to implement this. That's a good thing and a bad thing. It's good because it lets you get your creative juices flowing and try different things. It's bad because... well, hmmm, is it a bad thing? If you're looking to just hand something in and pass the course with as little effort as possible, it's a bad thing. But if you're looking to be a programmer and experiment and fail and experiment another way and fail and experiment and get it half right, then knock your head against the wall, then try and fail twelve times, then try and succeed, well when you tally up the hours and find it took you 100 hours, is that 100 hours wasted or 100 hours well spent? If it's 100 hours well spent, you're a programmer at heart.

Enough philosophy. Right off the bat, I see a whole bunch of hard-coded values in the print statements. Like the number of available seats. Suppose I buy one ticket from Manila to Hong Kong. I see there are 16 available seats. Then I buy another ticket. Are there still 16 available seats?

Comment two: It's hard to follow the code due to the inconsistent indentation / bracket style. I see some structs and functions with the starting bracket on the same line as the name...

struct flight{
// code
int main() {

and some with their own lines...

void header()
{
// code
void option1()
{

Either way is fine, but try to stick to one or the other. Use tabs or spaces, but not a mix. Here is your code quickly formatted using CodeBlocks. Note the consistency of the spacing, brackets, and indentation. See how fast I can see which block of code is inside which other block of code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void header()
{
    printf("\nFlight                Price   Seating Capacity    Available Seats\n");
}

void option1()
{
    printf("\nFlight                Price   Seating Capacity    Available Seats\n");
    printf("1. Manila To Hong Kong      1,500   100         16\n");
    printf("2. Manila To Caticlan       1,000   75          0\n");
    printf("3. Manila To Singapore      3,000   100         24\n");
}

void option2()
{
    printf("\nFlight                Price   Seating Capacity    Available Seats\n");
    printf("4. Manila To Hong Kong      1,700   100         18\n");
    printf("5. Manila To Caticlan       2,000   75          24\n");
    printf("6. Manila To Singapore      4,000   100         65\n");
}

void option3()
{
    printf("\nFlight                Price   Seating Capacity    Available Seats\n");
    printf("7. Manila To Hong Kong      2,000   100         46\n");
    printf("8. Manila To Caticlan       1,500   75          16\n");
    printf("9. Manila To Singapore      4,000   100         85\n");
}

int choice()
{

}

struct flight
{
    int ticket;
    int price;
};

int main()
{
    struct flight book[10];
    char user[50], pw[50], adminu[50]="jon", adminpw[50]="snow";
    int trip, date, date2, a, x, y;
    float total;
    char repeat;
    printf("\nEnter username: ");
    scanf("%s", &user);
    printf("\nEnter password: ");
    scanf("%s", &pw);
    if (strcmp(user, adminu)==0 && strcmp(pw, adminpw)==0)
    {
        printf("\nWelcome");
        printf("\nChoose if one way trip or roundtrip: ");
        printf("\n1. One way trip \n2. Roundtrip\n");
        scanf("\n%d", &trip);
        printf("\nDate: ");
        printf("\n1. August 10, 2017 \n2. August 11, 2017\n3. August 12, 2017(not applicable for round trip)\n");
        scanf("%d", &date);

        switch (trip)
        {
            case 1:
            {
                if (date==1)
                {
                    printf("\nAugust 10, 2017\n");
                    option1();
                    printf("\nHow many tickets will you get?:");
                    scanf("%d", &x);
                    for (a=1; a<=x; a++)
                    {
                        printf("\nPlease select which flight you will book: ");
                        scanf("%d", &book[a].ticket);
                    }
                }
                else if (date==2)
                {

                    printf("\nAugust 11, 2017\n");
                    option2();
                    printf("\nHow many tickets will you get?:  ");
                    scanf("%d", &x);
                    for (a=1; a<=x; a++)
                    {
                        printf("\nPlease select which flight you will book: ");
                        scanf("%d", &book[a].ticket);
                    }

                }
                else if (date==3)
                {
                    printf("\nAugust 12, 2017\n");
                    option3();
                    printf("\nHow many tickets will you get?: ");
                    scanf("%d", &x);
                    for (a=1; a<=x; a++)
                    {
                        printf("\nPlease select which flight you will book: ");
                        scanf("%d", &book[a].ticket);
                    }
                }
                else
                    printf("\nInvalid");
                break;
            }

            case 2:
            {
                if (date==1)
                {
                    printf("\nAugust 10, 2017\n");
                    option1();
                    printf("\nChoose date of return: \n1. August 11, 2017\n2. August 12, 2017\n");
                    scanf("%d", &date2);
                    printf("\nHow many tickets will you get (excluding the return trip)?: ");
                    scanf("%d", &x);
                    for (a=1; a<=x; a++)
                    {
                        printf("\nPlease select which flight you will book: ");
                        scanf("%d", &book[a].ticket);
                        if (date2==1)
                        {
                            switch (book[a].ticket)
                            {
                                case 1:
                                {
                                    printf("\nDetails for return trip");
                                    header();
                                    printf("Hong Kong to Manila     2,000   100         45\n");
                                    break;
                                }
                                case 2:
                                {
                                    printf("\nDetails for return trip");
                                    header();
                                    printf("Caticlan to Manila      1,500   75          67\n");
                                    break;
                                }
                                case 3:
                                {
                                    printf("\nDetails for return trip");
                                    header();
                                    printf("Singapore to Manila     4,000   100         46\n");
                                    break;
                                }
                                default:
                                    break;
                            }
                        }
                        else if (date2==2)
                        {
                            switch (book[a].ticket)
                            {
                                case 1:
                                {
                                    header();
                                    printf("\nHong Kong to Manila       2,000   100         45");
                                    break;
                                }
                                case 2:
                                {
                                    header();
                                    printf("\nCaticlan to Manila        1,500   75          67");
                                    break;
                                }
                                case 3:
                                {
                                    header();
                                    printf("\nSingapore to Manila       4,000   100         46");
                                    break;
                                }
                                default:
                                    break;
                            }
                        }
                        else
                        {
                            printf("\nInvalid");
                            return 0;
                        }
                    }
                }

                else if (date==2)
                {
                    printf("\nAugust 11, 2017\n");
                    option2();
                    printf("\nHow many tickets will you get?: ");
                    scanf("%d", &x);
                    for (a=1; a<=x; a++)
                    {
                        printf("\nPlease select which flight you will book: ");
                        scanf("%d", &book[a].ticket);
                    }

                }
                else if (date==3)
                {
                    printf("\nAugust 12, 2017\n");
                    option3();
                    printf("\nHow many tickets will you get?: ");
                    scanf("%d", &x);
                    for (a=1; a<=x; a++)
                    {
                        printf("\nPlease select which flight you will book: ");
                        scanf("%d", &book[a].ticket);
                    }
                }
                else
                    printf("\nInvalid");
                break;
                break;
            }
            default:
                break;
        }

        printf("\n");

        printf("\nSummary: ");
        for (a=1; a<=x; a++)
        {
            printf("\nDetails of Ticket no. [%d]", a);
            printf("\nFlight no. [%d]\n", book[a].ticket);

        }

        total=book[a].price;
        printf("\nTotal number of tickets: %d\n", x);
        printf("\nTotal Price: %0.2f", total);
    }
    else
    {
        printf("\nInvalid login");
    }

    return 0;
}

Now for your structure(s). I see a flight structure containing a ticket and a price. There's a bunch of information like source and destination cities that should be stored somewhere in a structure, yes? How about inside the flight structure? How about the number of seats in the airplane (seating capacity) too? Maybe the number of available seats too?

Perhaps a single function called option() that takes a flight object as a parameter and you use that flight object to print the details that you print in option1, option2, and option3?

There are LOTS of ways to go in this project.

Im having a hard time on how to display the total price when i choose roundtrip, on how to display the price and the total price.. someone please help me pls pls pls

First things first. You have problems before you get here. I ran your program. I tried a good login and a bad login. The good login lets me through, the bad one doesn't, so good job there. But then I am asked whether I want a one way ticket or a round trip ticket. I decide that Hong Kong is fun to visit, but I wouldn't want to live there, so round trip it is. I am then given an option of three dates (August 10th, August 11th, August 12th) and I see that I cannot buy a round trip ticket on August 12th. Naturally since I'm testing the program, I pick the illegal combination (round trip, August 12th), expecting to be chided for doing that (or gently reminded that I shouldn't do that, depending on the programmer's mood), then bounced back and told to try again.

I was NOT told to try again. I was asked how many tickets I wanted to buy. I said two, because who wants to go to Hong Kong alone, promptly bought two tickets for 4.1 million dollars, then discovered that I am now stuck for eternity in Hong Kong.

The 4.1 million dollars was the reason you posted this thread, but I suggest fixing the other stuff first. Why? Because if you fix the other stuff first, very often the price bug solution will stand out in a way that it does not now.

Here is the general format for these kinds of programs...

  1. Main menu with the main options. Very often one of these options will be the "quit buying tickets" option. You'll either ask how many tickets they are buying up front or ask each time whether they want to buy a ticket or quit. There will be a main loop. Each time through the loop, the person will buy a ticket. See #2. When they quit or have bought 10 out of 10 (or 5 out of 5 or whatever) tickets, you exit the main loop
  2. Within the main loop, you present the options (one way or round trip) and let them choose. If they choose "wrong" (round trip on August 12th, try to buy a ticket on a flight that doesn't exist or is full, you give them an error message and make them try again). By the end of step 2, they've picked a flight.
  3. In this step, you adjust the variable values, whatever they may be. You flag a formerly vacant seat occupied, you adjust the number of vacant seats, you adjust the total price, whatever.
  4. Repeat steps 1 to 3.
  5. They are now out of the main loop. Display the summary statistics, whatever they are (ie total price paid, itineraries, number of seats left per flight, etc., etc.)

And break it up into methods, makes everything a lot easier to read and figure out what it all means.
Combined with debug logging and copious comments, it can really help making sense of code.

Ideally, you'd modularise it and write independent tests for each module, but for an obvious school project of this small size that's probably overkill.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.