Can I set spacing in an output file??
Feb 10, 2012 at 6:26am UTC
I have been given a block of numbers to read in by a file, add the columns and rows, output to a file, then display the file.
I have it working fine, but the output isn't pretty because the summed totals have more digits than the input numbers. Is there a formatting trick I can use to line the rows up?
This is my input data
1 2 3 4 5
141 567 434 100 269 324
578 458 562 564 305 245
381 427 561 591 595 542
427 536 491 204 502 253
392 482 521 316 318 495
This is the Output data
1 2 3 4 5 6
141 567 434 100 269 324 1835
578 458 562 564 305 245 2712
381 427 561 591 595 542 3097
427 536 491 204 502 253 2413
392 482 521 316 318 495 2524
1919 2470 2569 1775 1989 1859 12581
This is the code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#include <iostream>
#include <iomanip>
#include <fstream>
const int ROW=5, SIZE=6;
const int ROWA=6, SIZEA=7;
using namespace std;
void addcolumn(int arrayb[][SIZEA]);
void addrow(int arrayb[][SIZEA]);
void print (int [][SIZEA]);
void write(int array[][SIZEA]);
void read(int array[][SIZEA]);
void displayOut();
void displayIn();
int main(int argc, char ** argv)
{
int OutputData[ROWA][SIZEA];
displayIn();
cout<<endl;
read(OutputData);
addrow(OutputData);
addcolumn(OutputData);
write(OutputData);
displayOut();
return 0;
}
void print(int array[][SIZEA])
{
for (int i=0;i<ROWA;i++)
{
for (int n=0;n<SIZEA;n++)
{
cout<<" " <<array[i][n];
}
cout<<endl;
}
}
void addcolumn(int arrayb[][SIZEA])
{
for (int n=0;n<SIZEA;n++)
{int total=0;
for (int i=0;i<ROW;i++)
{
total=total+arrayb[i][n];
}
arrayb[ROW][n]=total;
}
}
void write(int array[][SIZEA])
{
ofstream Data2Out("OutputData.txt" );
for (int i=0;i<ROWA;i++)
{
for (int n=0;n<SIZEA;n++)
{
Data2Out<<array[i][n]<<" " ;
}
Data2Out<<endl;
}
Data2Out.close();
}
void read(int array[][SIZEA]){
ifstream Data ("InputData.txt" );
while (!Data.eof()){
for (int i = 0; i < ROW; i++){
for (int n = 0; n < SIZE; n++){
Data >> array[i][n];
}
}
}
Data.close();
}
void addrow(int arrayb[][SIZEA])
{
for (int i=0;i<ROW;i++)
{int total=0;
for (int n=0;n<SIZE;n++)
{
total=total+arrayb[i][n];
}
arrayb[i][SIZE]=total;
}
}
void displayOut(){
ifstream myfile ("OutputData.txt" );
string line;
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
void displayIn(){
ifstream myfile ("InputData.txt" );
string line;
while ( myfile.good() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
Feb 10, 2012 at 7:43am UTC
Don't worry so much for little things.
You can make a guest of the column with and use setw to fix it (you will need to read number by number).
Topic archived. No new replies allowed.