I am having a very hard time formatting the data values from a program correctly. The point of the program is to display values from a data file in batch processing in a format like 5 columns with 5 integers each and creating another row of 5 integers if there is more than 5 integers in the data file.
However my code is not working in this manner even though it seems it should
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int data;// the data which will be inputted from the file
int count = 0;
int amount; // number of data integers in a column
cout << "Values read from file" << endl;
while (cin >> data)
{
for (int i = 0; i < 5; i++)
{
amount++;
cin >> data;
cout << setw(7) << data;
if (amount == 5)
{
i = 0;
cout << endl;
}
}
}
return 0;
}
This is what I have so far and the code is compiling but the output I'm getting is very ugly and the integers at the end repeat.
This is what the format I'm getting looks like
Values read from file 32 -4 707 22 33 -70000 370000 -43 17 0 96 96 96 96 96
Use modular arithmetic!
The 'mod' operator,
%
will do division and return the remainder. You can use this to figure out when to print your new lines:as a side note, the reason your code isn't working now is probably becuase your
i=0
was supposed to beamount = 0
, but modular arithmetic will let you keep amount as a running total.