Reputation: 13
How to make a program that read digits form text file into array while there is numbers in current line then i want to print that line on screen and repeat everything while there is lines. How to do this? Once i heard about dynamic arrays maybe i should use them?
int k = 0;
int paz[14];
int sk;
file >> n; // n - number of lines
for (int i = 0; i < n; i++)
{
while ( file >> sk )
{
paz[k] = sk;
cout << paz[k] << " ";
k++;
}
cout << endl;
}
Text file: In each line can be form 0 to 10 numbers. First line shows number of lines and lines separated with comma
2,
9 5 10 7 8 9 7 12 7 17,
0 1 7 0 14 4 ,
Upvotes: 1
Views: 360
Reputation:
Yes, you should use dynamic arrays. They are called vectors. They are very easy to use and a very good replacement for arrays.
The code with vector, would look like this:
#include <vector>
int k = 0;
vector<int> paz;
int sk;
file >> n; // n - number of lines
for (int i = 0; i < n; i++)
{
while ( file >> sk )
{
paz.push_back(sk);
cout << paz[k] << " ";
k++;
}
cout << endl;
}
Upvotes: 2