CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2008
    Posts
    6

    Creating a table with cout

    Hello all.

    I have an array called productinfo, with the attributes of name, quantity and price. I am trying to create a invoice of this with all the columns + total amount (price * quantity).

    Below is my code which i currently have used to generate the invoice.


    I am sure most of you more experienced people will be able to see that when displayed, this code does not guarantee that all data/columns come out evenly. Come rows will be one way, while the next is so many pixels to left or right and all.

    I am kinda at a loss as to what code to implement in order to fix it. Truth be told i am rather new to c++, and this iomanip thing is confusin me as to the exact thing i am looking for to do what i want.

    Is there any helpful hints or code additions anyone can make? Is there a good online tutorial or something? I tried looking around here abit but i think most of the results were populating tables - as in a 2 dimensional array or something. It confuses me. I will be more then willing to post more code if asked.

    Thanks,
    Tim
    Last edited by Timmeh041; May 26th, 2008 at 09:53 AM.

  2. #2
    Join Date
    May 2008
    Posts
    96

    Re: Creating a table with cout

    Ah, I see your problem.

    The best way to design these sorts of things is to pull out a piece of graph paper and draw the desired output using realistic values (at least one of each containing the maximum number of characters that each field can output).

    Then, using a different color, mark the ends of each field.

    One thing to remember is that setw() only guarantees a minimum field width. The STL obnoxiously doesn't provide an automatic way to guarantee a maximum field width, so you have to watch that when writing data.

    So, you may want something like:
    Code:
    cout
      << setw(11) << right << "Product ID"
      << setw(16) << right << "Quantity"
      << setw(16) << right << "Price ($)"
      << setw(16) << right << "Cost ($)"
      << endl << endl;
    
    for each item:
      cout
        << setw(11) << right << productinfo[q].name
        << setw(16) << right << productinfo[q].quantity
        << setw(16) << right << ...
    The above assumes that productinfo.name, .quantity, etc. are all printable in one less than their field sizes.

    Hope this helps.

  3. #3
    Join Date
    May 2008
    Posts
    6

    Re: Creating a table with cout

    Well there in lie the problem, because the program as a whole allows users to enter in product names, prices and amounts (it is a really silly idea to code. Essentially you are creating your own database to buy things from). So, i gather from my readings that setw(n) will set the next piece of output n spaces down the line. Is this right?

    So am i to pad out some spaces with certain characters? Or is there a different function that i simply don't know? Or a way to set column widths? I have no ideas.

    Duoas: The numbers i have in there are just numbers which i thought made the output looked spaced out, i wasn't really considering the length of each item when i choose them. I hope i have understood correctly what you have said.

  4. #4
    Join Date
    May 2008
    Posts
    6

    Re: Creating a table with cout

    Never mind guys, I have figured out how to do it that way i was hoping. Nothing special was really done, just a bit of thinking. Mods, please feel free to delete this thread if ye see fit. Nothing of critical content was discussed.

    I am also removing the code from above. It was part of a university project and i don't want class mates borrowing it, however unlikely i may be.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured