Displaying a Well-Formatted Table

This is a discussion on Displaying a Well-Formatted Table within the C++ Programming forums, part of the General Programming Boards category; I have posted recently regarding this program but seeking additional assistance for a different matter. The program should display a ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    13

    Displaying a Well-Formatted Table

    I have posted recently regarding this program but seeking additional assistance for a different matter. The program should display a formatted table of Flights and their information. I am able to format my first line of output, but I cannot format the subsequent lines of the flight like the first one.

    Some of the formatting is getting screwy in my post but I think my questions are still clear.

    So my two questions:
    1) How to format the additional lines to line up with the original?
    2) The Destination section does not seem to start at the same place on each row. Such as in my example, Destination Atlanta is a whitespace past Albuquerque. What methods are available to fix this?

    Here's an example of my output:
    Origin Destination Flight Price =================================================
    From Chicago to: Albuquerque 380 $400


    Atlanta 290 $175


    Miami 2843 $500


    Orlando 3940 $430


    From Nashville to: Atlanta 339 $83


    Denver 1203 $560


    Las-Vegas 920 $59


    Miami 89 $490


    New-York-City 983 $260


    Philadelphia 8831 $410


    Tampa 665 $320


    Tampa 428 $79



    My code in "type.cpp":

    Code:
    ostream& operator << (ostream& os, const flightStruct rhs) //Overloaded operator for '<<'{                                                            //for struct output
                                                                //for origin
        os << left << setw(10) << rhs.destination                                    //for destination
           << '\t' << rhs.flightNum << '\t'                        //for flight number
           << "  $" << rhs.price << endl;                        //for flight price
        
        return os;
        
    }
    My code in "main.cc"
    Code:
    cout << "   " << "Origin" << '\t' << " Destination" << '\t' << "Flight" << "      " << "Price" << '\t' << endl         << "=================================================" << endl;
        
        map.displayFlightInfo();        //Display the flight information

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    Instead of the tab character use the setw() function, you'll have better control of where you place the data. Don't forget to include the proper include file for this class.

    Jim

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    Thanks, that helped. The code looks cleaner now and was easier to format the output. However, the subsequent lines are still not formatting according to the first line. They tend to start underneath "From [city] to:". The line itself, such as the flight number and price are set according to the lines before, but in terms of where the subsequent city actually starts...it's not lining up. Any ideas?

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    2,674
    Show your code. Do you understand that you can use the setw() manipulator with numbers as well? You may need to change the justification.

    Jim

  5. #5
    Registered User
    Join Date
    Feb 2013
    Posts
    13
    It might help if I specify I am attempting to output information stored in a struct, which is also stored in a pointer-based array.

    I'll post the sections of code where I imagine the problems are occurring along with my output (hopefully the formatting does not get messed up in the post)

    Here's the first portion of the output to get an idea:

    Code:
       Origin     Destination    Flight      Price    
    =================================================
    From Chicago to: Albuquerque    380       $400
    
    
       Atlanta    290       $175
    
    
         Miami   2843       $500
    
    
       Orlando   3940       $430
    
    
    From Nashville to:    Atlanta    339       $83
    
    
        Denver   1203       $560
    
    
     Las-Vegas    920       $59
    
    
         Miami     89       $490
    
    
    New-York-City    983       $260
    
    
    Philadelphia   8831       $410
    
    
         Tampa    665       $320
    
    
         Tampa    428       $79

    In a file named type.cpp

    Code:
    ostream& operator << (ostream& os, const flightStruct rhs) //Overloaded operator for '<<'{                                                            //for struct output
                                                                //for origin
        os << fixed << setw(10) << rhs.destination                                    //for destination
           << setw(7) << rhs.flightNum << setw(10)                        //for flight number
           << setw(8) << "$" << rhs.price << endl;                        //for flight price
        
        return os;
        
    }
    In a file name flightMap.cpp:

    Code:
    void flightMap::displayFlightInfo() const                    //Displays the origin, destination, flight number, and price of each flight{
        
        for(int index = 0; index < size; index++)                //Set index to zero, check to see if it is less than the amount of cities read in, increment index
        {
            if(flights[index].length() > 0)                        //If the flight contains information
            {
                cout << "From " << cities[index] << " to: ";    //Output the flight
                flights[index].Display();
            }
                
        }
    }

    In the main.cc file:

    Code:
    cout << "   " << "Origin" << '\t' << " Destination" << '\t' << "Flight" << "      " << "Price" << '\t' << endl         << "=================================================" << endl;
        
        map.displayFlightInfo();        //Display the flight information

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Displaying Scores in Table
    By jappy512 in forum C Programming
    Replies: 11
    Last Post: 12-15-2009, 08:59 PM
  2. Displaying a table using nested loops
    By leviterande in forum C Programming
    Replies: 13
    Last Post: 09-29-2009, 04:42 PM
  3. Displaying a table of arbitrary values?
    By cbmurdock in forum C Programming
    Replies: 1
    Last Post: 02-28-2008, 11:59 PM
  4. Displaying a table
    By hkguy in forum C++ Programming
    Replies: 2
    Last Post: 03-29-2004, 06:49 AM
  5. displaying the contents of an html file in a table
    By confuted in forum Tech Board
    Replies: 3
    Last Post: 08-02-2003, 07:50 PM

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