How..Complicated reading problem

My data.txt contents is like this:

3 6
8 12 60
74 57 94 13

---------------------------------

This is my code to read it and store it:

int x;
count=0;
while (inFile >> x)
{
inFile >> x;
store_array[count]=x;
cout << store_array[count] << endl;
count++;
}

int n1, n2, n3, n4;
n1=store_array[0];
n2=store_array[1];
n3=store_array[2];
n4=store_array[3];

if(count==2)
{
gcd(n1,n2);
}

if(count==3)
{
gcd(gcd(n1,n2),n3);
}

if(count==4)
{
gcd(gcd(n1,n2),gcd(n3,n4));
}

Problem is:
How to read each line of the data and store them into array.

For example:
read first line data that is: 3 6
assign array[0]=3 and array[1]=6
compute gcd(array[0], array[1])

then read next data after new line that is: 8 12 60
assign array[0]=8, array[1]=12 and array[2]=60

then read next data that is: 74 57 94 13
assign array[0]=74, array[1]=57, array[2]=94 and array[3]=13

Any idea how to read and store this data into array after each new line.
Read each line with getline() -- the one declared in <string>, then use an istringstream (#include <sstream>) to get each integer out of the line. For each integer, add it into your array. When there are no more integers in the stringstream, compute the GCD and continue on with the next line.

Hope this helps.
Thanks for the idea..I found the solutions

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

const int ROWS = 5;
const int COLS = 4;
const int BUFFSIZE = 80;

int main() {
int array[ROWS][COLS];
char buff[BUFFSIZE]; // a buffer to temporarily park the data
ifstream infile("data2.txt");
stringstream ss;
for( int row = 0; row < ROWS; ++row ) {
// read a full line of input into the buffer (newline is
// automatically discarded)
infile.getline( buff, BUFFSIZE );
// copy the entire line into the stringstream
ss << buff;
for( int col = 0; col < COLS; ++col ) {
// Read from ss back into the buffer. Here, ',' is
// specified as the delimiter so it reads only until
// it reaches a comma (which is automatically
// discarded) or reaches the 'eof', but of course
// this 'eof' is really just the end of a line of the
// original input. The "6" is because I figured
// the input numbers would be 5 digits or less.
ss.getline( buff, 15, ' ' );
// Next, use the stdlib atoi function to convert the
// input value that was just read from ss to an int,
// and copy it into the array.
array[row][col] = atoi( buff );
}
// This copies an empty string into ss, erasing the
// previous contents.
ss << "";
// This clears the 'eof' flag. Otherwise, even after
// writing new data to ss we wouldn't be able to
// read from it.
ss.clear();
}
// Now print the array to see the result
for( int row = 0; row < ROWS; ++row ) {
for( int col = 0; col < COLS; ++col ) {
cout << array[row][col] << " ";
}
cout << endl;
}
infile.close();
system("pause");
}
Topic archived. No new replies allowed.