Display scores in rows

I have to write a program that read the DataFile.txt and display scores in rows of three scores. But, I do not know how to display in rows of three.

[code]
//This is my code
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

void main()
{
string dataFile = "DataFile.txt";
ifstream fin;

int num, ns;

fin.open(dataFile.c_str());
ns = 0;
fin >> num; // Read first data value

while (!fin.eof())
{
cout << num << " "; // ?.. This displays all data in a single rows. How to display this in rows of three scores?
++ns;
fin >> num; // Read next data value
}
cout << endl << endl;

fin.close();

}
Last edited on
@XyzAbc

You could first initialize the variable, ns to be 1, instead of 0. Then, after your ++ns, (which probably should be ns++;), have,
1
2
if(ns %3 == 0)
cout << endl;


That should do it.
Last edited on
Works perfect. Thank you for your help.
My pleasure, XyzAbc
Topic archived. No new replies allowed.