What is the best way to make a table of sorts.

This is a discussion on What is the best way to make a table of sorts. within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that intakes baseball stats into structs and then displays them line by line with one ...

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

    What is the best way to make a table of sorts.

    I am writing a program that intakes baseball stats into structs and then displays them line by line with one player on each line and the stats being the columns.

    Not sure how to do this well enough to make them even and such though and make it look nice. Any suggestions?

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    look into output routines using either C style printf() or C++ style ostream manipulators and flags.

  3. #3
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    One other quick question. If use this line:

    cout << setprecision(3) << setiosflags(ios::fixed);

    How can I negate that and make it go back to normal?

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    10
    Anyone know how to do that? Set it back to default after using setprecision and setiosflags?

  5. #5
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I do not know any method to clear all flags, but you can use cout.unsetf( flags_here ) to remove some specific flags. The precision I donīt know, you could reset it to the default, I think that it is 5.
    Hope that helps!
    Flags:
    Code:
    enum {
    
       skipws, 	Skip whitespace on input.
       left,	Left-adjust output.
       right,	Right-adjust output.
       internal,	Pad after sign or base indicator.
       dec,	Decimal conversion.
       oct,	Octal conversion.
       hex,	Hexadecimal conversion.
       showbase,	Show base indicator on output.
       showpoint,	Show decimal point for floating-point output.
       uppercase,	Uppercase hex output.
       showpos,	Show '+' with positive integers.
    
       scientific,	Suffix floating-point numbers with exponential (E) notation on output.
       fixed,	Use fixed decimal point for floating-point numbers.
       unitbuf,	Flush all streams after insertion.
       stdio,	Flush stdout, stderr after insertion.
    };
    Nothing more to tell about me...
    Happy day =)

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    The normal way to do this is to get the precision, change it, party, and then set it back. same deal with the flags
    Code:
     std::ios_base::fmtflags f = std::cout.setf(std::ios_base::right);
     std::streamsize p = std::cout.precision(3);
     std::cout << a << ", "  << foo() << std::endl;
     std::cout.precision(p);
     std::cout.flags(f);
    Yep, that's right this is an ugly and stupid way to do things. Plus if foo() throws an exception you will never get cout to work right again. Although in this case there really is no good way to recover from the exception as we have already printed 'a' and a comma, so we're always going to be ugly. However for those who want a classy and safer way to do things boost comes to the rescue with io state savers While you are there you probably will want to go ahead and have a look at format

  7. #7
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Interesting! But what does the statement
    std::ios_base::fmtflags f = std::cout.setf(std::ios_base::right) ?
    Thanks in advance.
    Nothing more to tell about me...
    Happy day =)

  8. #8
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    most of the (member) functions that allow you to set something return what that value was previously. std::ios_base::fmtflags is really a fancy way of saying int, but future-proof(ish).

  9. #9
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    Sorry for insisting, but what is td::ios_base::right?
    Thanks for the answer.
    Nothing more to tell about me...
    Happy day =)

  10. #10
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    What a shame!!!! Sorry for the last post! My first one has the answer and I didnīt see it.
    Nothing more to tell about me...
    Happy day =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dictionary in C# to hash table in C?
    By dinoman in forum C Programming
    Replies: 2
    Last Post: 04-12-2009, 10:23 PM
  2. Duplicate at Hash Table
    By DarrenY in forum C Programming
    Replies: 1
    Last Post: 05-10-2007, 03:31 AM
  3. Win32 Common Controls in C++, how do i make and use them?
    By C+noob in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2006, 11:53 AM
  4. how do i make a mathematical table using loops.
    By gad n' gaz in forum C Programming
    Replies: 13
    Last Post: 10-18-2005, 03:15 PM
  5. How to make a multiplication table up to 10*10=100?
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 01-11-2002, 10:07 AM

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