How can I make this program less wordy for eg. with structs and classes and function?

This is a discussion on How can I make this program less wordy for eg. with structs and classes and function? within the C++ Programming forums, part of the General Programming Boards category; I find that my program is a bit too wordy and im not sure as to how i can make ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    10

    How can I make this program less wordy for eg. with structs and classes and function?

    I find that my program is a bit too wordy and im not sure as to how i can make it into less line. Can someone help me please?

    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    void details( string fNum,int dHour,int dMin,int aHour,int aMin, string aircraft);
    
    int main()
    {
     //Flights tB:Trinidad-Barbados,tN:Trinidad to New York,tL:Trinidad to London.
            int dHour;
            int dMin;
            string fNum;
            int aHour;
            int aMin;
            int durHour;
            int durMin;
            string aircraft;
            int rows,columns;
            int dOption;
            bool availabe=true;
            int tbSeats[15][4]={0};
            int tnSeats[32][6]={0};
            int tlSeats[30][10]={0};
            int resAmt;
            int empty=0;
    
    //Recording Reservations for Passengers
            cout<<"\nWhat is your destination?\n"<<endl<<"1) Trinidad to Barbados"<<endl
                                                   <<"2) Trinidad to New York"<<endl
                                                   <<"3) Trinidad to London\n"<<endl;
            cout<<"Please enter either 1,2 or 3."<<endl;
            cin>>dOption;
    
            switch (dOption)
            {
                    case 1:
                            dHour=7;
                            dMin=0;
                            fNum="USC 215";
                            aHour=7;
                            aMin=45;
                            durHour=0;
                            durMin=45;
                            aircraft="MD 82";
                            rows=15;
    		columns=4;
    
    
                    break;
    
                    case 2:
                            dHour=6;
                            dMin=0;
                            fNum="USC 400";
                            aHour=3;
                            aMin=0;
                            durHour=6;
                            durMin=0;
                            aircraft="Boeing 737";
                            rows=32;
                            columns=6;
                    break;
    
                    case 3:
                            dHour=7;
                            dMin=30;
                            fNum="USC 900";
                            aHour=9;
                            aMin=30;
                            durHour=9;
                            durMin=0;
                            aircraft="Boeing 777";
                            rows=30;
                            columns=10;
                    break;
            }
                    
    
    	details(fNum,dHour,dMin,aHour,aMin, aircraft);
    
    //This section declares everything in all of the arrays to 0;
           
     if (dOption==1)
            {
                    int tbSeats[15][4]={0};
    
                     for(int a=0;a<rows;a++)
                     {
                            for(int b=0;b<columns;b++)
                            {
                                    cout<<tbSeats[a][b]<<"\t";
    
                            }
                            cout<<endl;
    
           }         }
                    else if(dOption==2)
                    {
                            int tnSeats[32][6]={0};
    
    
                                    for(int a=0;a<rows;a++)
                                    {
                                            for(int b=0;b<columns;b++)
                                            {
                                                    cout<<tnSeats[a][b]<<"\t";
    
                                            }
                            cout<<endl;
                                    }
                    }
    
                            else if (dOption==3)
                            {
                                    int tlSeats[30][10]={0};
    
    
                                            for(int a=0;a<rows;a++)
                                            {
                                                    for(int b=0;b<columns;b++)
                                                    {
                                                            cout<<tlSeats[a][b]<<"\t";
    
                                                    }
    
                                            }
                            }
                                            cout<<endl;
    
    // This section determines how many empty seats there are
    
            if (dOption==1)
            {
    
                     for(int c=0;c<rows;c++)
                     {
                            for(int d=0;d<columns;d++)
                            {
                                    if (tbSeats[c][d]==0)
                                    empty++;
    
                            }
                     }
    
    
             }
                    else if(dOption==2)
                    {
    
    
                                    for(int c=0;c<rows;c++)
                                    {
                                             for(int d=0;d<columns;d++)
    
                                            {
                                                    if (tnSeats[c][d]==0)
                                                    empty++;
    
                                            }
                                    }
    
    
    
    
                    }
                            else if (dOption==3)
                            {
    
    
                                            for(int c=0;c<rows;c++)
                                            {
                                                    for(int d=0;d<columns;d++)
                                                    {
    
                                                    if (tlSeats[c][d]==0)
                                                    empty++;
                                                    }
    
                                            }
                            }
                            cout<<"There are "<<empty<<" empty seats"<<endl;
    
    //This section takes the amount of seats that the user wishes to reserve and makes them unavailable
    
    
                            cout<<"How many seats would you like to reserve?"<<endl;
                            cin>>resAmt;
    
                            for (int e=0;e>resAmt;e++)
                            {
                                     if (dOption==1)
                                     {
    
                                            for(int c=0;c<rows;c++)
                                            {
                                                    for(int d=0;d<columns;d++)
                                                    {
                                                            if (tbSeats[c][d]==0)
                                                            {
                                                            tbSeats[c][d]=1;
                                                            }
                                                            else
                                                            {
                                                            cout<<"This seat is already taken"<<endl;
                                                            }
    
                                                    }
                                            }
    
    
                                    }
                                    else if(dOption==2)
                                            {
    
    
                                                    for(int c=0;c<rows;c++)
                                                    {
                                                            for(int d=0;d<columns;d++)
    
                                                            {
    
                                                                    if (tnSeats[c][d]==0)
                                                                    {
                                                                    tnSeats[c][d]=1;
                                                                    }
                                                                    else
                                                                    {
                                                                    cout<<"This seat is already taken"<<endl;
                                                                    }
    
                                                            }
                                                    }
    
    
                                            }
                                    else if (dOption==3)
                                            {
                                                    for(int c=0;c<rows;c++)
                                                    {
                                                            for(int d=0;d<columns;d++)
                                                            {
    
                                                                    if (tlSeats[c][d]==0)
                                                                    {
                                                                            tlSeats[c][d]=1;
                                                                    }
                                                                    else
                                                                    {
                                                                    cout<<"This seat is already taken"<<endl;
                                                                    }
                                                            }
    
                                                    }
                                            }
                                            }
    }
    
    return 0;
    
    
    }
    
    
                   void details( string fNum,int dHour,int dMin,int aHour,int aMin, string aircraft)
    {
                    cout<<"\n\nYou Have Chosen to Fly on Flight Number "<<fNum<<endl;
                    cout<<"The Flight will Depart at "<<dHour<<":"<<dMin<<endl;
                    cout<<"The Flight will Arrive at its Destination in "<<aHour<<" Hours and "<<aMin<<" Minutes"<<endl;
                    cout<<"Aircraft is the "<<aircraft<<endl;
    
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    1)I would do is make a Flight class that contains all the details of a flight. The details function would be a member of Flight

    2)You have a lot of code that is Identical except the array which represents the list of seats. Instead of using 3 separate arrays, use one. Either make this array big enough for the biggest plane, or dynamically allocate the correct size array for each flight and use that array.

    3)Several times you write a comment like "//This section does such and such". Instead of what you have, for each of those sections make a separate method of Flight with a descriptive name, and remove the comments.

    These suggestions may not in all cases decrease the amount of code, but they will make your code clearer, which is generally more important.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21