formatting output

Started by BigAl75, May 07, 2004, 02:32:08 PM

Previous topic - Next topic
I'm writing a code that will take numbers from a text file, enter them into an array, and then read the array.  One of the first things we have to do is print the data in the array.  The only problem I'm having is that when outputting the numbers to a text file, all the numbers are being displayed on one line, and they need to be displayed 5 numbers per line, like so:

1 2 3 4 5
6 7 8 9 10
etc.......

Anyone have any suggestions as to how I can do this?

Ok.

Maybe I'm just missing something, but I am one to believe that if you show wha tyou have, one might be able to help you help yourself better.

Would you please provide what you have, so that we [myself or someone else] can get a better idea of what you're really doing ?

But..  just as an example, you could do something like this (clean or otherwise, not taking the text file into account.. just defined an array here in the code itself) :

#include <stdio.h>

#define MAX_ELE 12

int main()
{

int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
int i, j, k;

for (i = 0; i < MAX_ELE; i++)
{
for (j = 0; j < 5; j++, i++)
  {
  printf("%d ", array);
  }
       printf("\n");
}
return 0;
}

which would essentially iterate through the array 'array' with the number of elements equal to MAX_ELE (defined to whatever) ... and print 5 numbers per line.. since after the five times per row it'll print a new line (\n).

Cheers,
Metty
"My Terminal is my Soul"

May 07, 2004, 06:38:38 PM #2 Last Edit: May 07, 2004, 06:39:42 PM by BigAl75
#include <string>
#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include <iomanip.h>

using namespace std;

ifstream infile;
ofstream outfile;

const int maxlist = 30;

int main()
{
   ifstream infile;
   ofstream outfile;

   infile.open("inputnums.txt");
   outfile.open("arrayhomework.txt");

   double list[maxlist];
   int counter, index, numbers, above = 0, below = 0;
   double sum, average;

//array values*********************************
    cout<<"The numbers in the array are: "<<endl;
   outfile<<"The numbers in the array are: "<<endl;

   for(numbers = 0; numbers < maxlist; numbers++)
   {
      infile>>list[numbers];
      cout<<list[numbers]<<" ";
      outfile<<list[numbers]<<" ";
    }



Thanks for the help.  This is the original one I had ( the one that puts them all on one line).  I've had 4 or 5 different versions, but went back to square 1.

SMF spam blocked by CleanTalk