Mar 13, 2015 at 1:35am Mar 13, 2015 at 1:35am UTC
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 Mar 13, 2015 at 1:59am Mar 13, 2015 at 1:59am UTC
Mar 13, 2015 at 2:03am Mar 13, 2015 at 2:03am UTC
@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 Mar 13, 2015 at 2:13am Mar 13, 2015 at 2:13am UTC
Mar 13, 2015 at 3:48am Mar 13, 2015 at 3:48am UTC
Works perfect. Thank you for your help.