-3

i have a string array which keeps data which read from a file. it consists 76 lines.

what i want to do is store those in different arrays. like from 21st line to 31st line in 1 array. and 31st to 41st one array. how can i do it... plz help

i want to split 70 lines into 7 arrays each containing 10 lines of it. and do it without using vectors

but this did not work

  • 5
    [Not this again.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – chris Jan 01 '13 at 07:52
  • 7*10 != 77 .. What do you want to do ? – asheeshr Jan 01 '13 at 07:56
  • Have you considered using `std::vector > >` (or `std::map > >`) as your container? Either would make your life easier. And follow the link that @chris left you, it will save you a world of pain. – Johnsyweb Jan 01 '13 at 09:31
  • That last edits makes this a totally pointless question since you do not show anything useful (and your last sentence has a dangling reference). – Benjamin Bannier Jan 02 '13 at 09:07

3 Answers3

2

You are using the equality operator ==. i will only be equal to one of those values 6 times across the entire loop execution. Only when i is 11, 22, 33, 44, 55 or 66; for any other value of i your loop will do nothing.

You probably meant < instead.

K-ballo
  • 76,488
  • 19
  • 144
  • 164
1

Something like this:

getline(ol, arr[i/11][i%11]);

where arr is a std::vector of std::vector<std::string>s. Or array of array of strings.

Still another way is:

while (1) {
  std::string *ptr;
  if (i < 11) ptr = arr1;
  else if (i < 22) ptr = arr2;
  // long list of arrays

  getline(ol, ptr[i%11]);
  // increment i, break on eof...
}
perreal
  • 85,397
  • 16
  • 134
  • 168
  • 1
    For some unknown reason, vectors are out. – chris Jan 01 '13 at 08:04
  • i solved it my self. first initialize two variables in one loop, eg: for(int i=start; j =0; i < end ; i++,j++) so to start and end you can pass the values from the large array. j will be small array insexes. so it will be easy. arr1[j] = largearray[i]; – Indunil Withana Jan 10 '13 at 13:02
0
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    string arr[8][10];

    int i = 0, j = 0;

    ifstream ol("a.txt");

    while(getline(ol, arr[i][j]))
    {

        ++j;
        if(j == 10)
        {
            ++i;
            j = 0;
        }
    }

}

This is assuming you don't have more than 80 lines.

user93353
  • 12,985
  • 7
  • 50
  • 106