I'm a relative beginner, taking a c++ class, and I'm trying to output this stream of numbers to a .txt file. I'm having no problem actually getting them into the file, but I can't figure out how to get the numbers to line up in columns or rows instead of just being one really long row (or column, if I put an endl after each one).

To put it in context, I'm using a for loop to read each of these numbers from an array one at a time, and the output to the .txt file happens inside the loop as well, so each time it iterates, it reads the next number and outputs it. How can I get this in a more printer-friendly format?

Thanks

Use nested loops. One for rows and one for columns:

for (int rows = 0; rows < .......; ++rows) {
    for (int cols = 0; cols < .......; ++cols ) {
         out_file << some_value << ' ';
    }
    out_file << '\n';
}

You could try using an if statement with the loop's counter and the mod operator (%) to break it up into n rows of x columns.

const int ROW_SIZE = 5;

for (int i = 0; 1 <= 20; i++) {
  /* read input */

  /* write output */

 if ((i%ROW_SIZE) == (ROW_SIZE-1))
  /*your ofstream object */ << "\n";
} // end for

This should create a file laid out as rows of 5 numbers each.

[edit]rats, ninja niek_e strikes again[/edit]

Thanks guys, this seems to have worked!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.