Thread: Formatting output

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    7

    Formatting output

    My code is working perfectly, except for one thing. I need to display my output in the following format:

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

    (no more than 5 numbers per line)

    What I'm getting is this:

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

    My instructor told me to use mods, but we never went over that in class, and I can't find it in the book. Any help would be very much appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Look up the % operator then
    mods being modulo
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    7
    Thanks for the tip.

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    7
    OK, only thing I can find on the % is using it for dividing numbers. There's nothing in the book about using it for displaying output. Anyone got any ideas?

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    127
    Run this and you'll see what Salem meant by using modulo to help with formatting output.
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      for (int i = 1; i <= 20; i++) {
        cout << i;
        if (i % 5 == 0) {
          cout << '\n';
        }
        else {
          cout << ' ';
        }
      }
    }

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    7
    Thanks for the help.

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    7
    Thanks. I ended up doing it like this and it works fine. Thanks again for the help.

    Code:
    for(numbers = 0; numbers < maxlist; numbers++)
        {
           infile>>list[numbers];
           if((numbers + 1) % 5 == 0)
           {
              outfile<<list[numbers]<<" "<<endl;
              cout<<list[numbers]<<" "<<endl;
           }
           else
           {
              outfile<<list[numbers]<<" ";
              cout<<list[numbers]<<" ";
           }
        }
    I did it that way in case, in the future, someone wanted to change the display to 6 per line, all they'd have to do is change every instance of 5 to 6.

  8. #8
    Registered User
    Join Date
    May 2004
    Posts
    127
    I did it that way in case, in the future, someone wanted to change the display to 6 per line, all they'd have to do is change every instance of 5 to 6.
    This sounds like a good place to use a constant.
    Code:
    const int ITEMS_PER_LINE = 5;
    
    [...]
    
    for(numbers = 0; numbers < maxlist; numbers++)
    {
      infile>>list[numbers];
      if((numbers + 1) % ITEMS_PER_LINE == 0)
      {
        outfile<<list[numbers]<<" "<<endl;
        cout<<list[numbers]<<" "<<endl;
      }
      else
      {
        outfile<<list[numbers]<<" ";
        cout<<list[numbers]<<" ";
      }
    }
    Now if you want to change the display to 6 per line, you only have to make one change, regardless of how many times you write the display loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 06:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM