In C++ How can I make integers from a data file be spaced evenly into 5 colums?

373 views Asked by At

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

4

There are 4 answers

1
scohe001 On

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:

while(stuff) {
    if(amount % 5 == 0) {
        cout << endl;
    }
    amount++;

    //more stuff
}

as a side note, the reason your code isn't working now is probably becuase your i=0 was supposed to be amount = 0, but modular arithmetic will let you keep amount as a running total.

1
Svampuz On

You have not inizialized amount variable:

int data;
int count = 0;
int amount = 0;

should solve your problem.

1
user4581301 On
#include<iostream>
#include<fstream>
#include <iomanip>

using namespace std;

int read(istream & in)
{
    int data; // the data which will be inputted from the file
    int amount = 5; // number of data integers in a column

    cout << "Values read from file" << endl;
    while (in >> data) //only need one cin. This will catch most of the errors you'll
                        // care about.
    {
        cout << setw(7) << data;
        if (--amount == 0) // % 5 will screw up at max int. 
                           // This won't and is probably quicker.
        {
            amount = 5;
            cout << endl;
        }
    }
    if (amount != 5)
    { // spit out EOL if there are any left-overs.
        cout << endl;
    }

    return 0;
}

int main(int argsc, char ** argsv)
{
    // ifstream in("name of file");
    // read(in);
    read(cin);
}

EDIT: Generalized to make it a bit more usable. OP seems to want to read from file at some point.

0
Chris Dodd On
int main()
{
    int data; // the data which will be inputted from the file
    int count = 0; // number of values written so far
    cout << "Values read from file" << endl;
    while (cin >> data) {
        cout << setw(7) << data;
        if (++count % 5 == 0)
            cout << endl;
    }
}
  • you only need one loop
  • always check the result of reading something to check if it worked.