I am trying to read the data (integers) from a file to a larger unsorted array and then move specific elements from that array to specific sub arrays. I can write the data from the file to the unsorted array fine but I get the wrong integers when trying to move the items to the sub arrays (lines 14 - 29). Is there a way to just write the specific integers directly from the file to the correct array instead of larger unsorted array? Example, if the number is between 0 and 9 then write it to array b1. Here is what I have so far.

//writes data from file to the array
 while( (i < ARRAYSIZE) && (fin >> input_array[i]))
  {
    i++;
  }
  //close the input file 
  fin.close();
  
  //this prints the array to screen so the user can visually verify it has the correct elements  
  for(int i =0; i < ARRAYSIZE; i++)
    cout<<input_array[i]<<endl;
    
//places the elements from the input_array into the correct sub array depending on what range of integers it falls between
  for(int i = 0; i < ARRAYSIZE; i++)
  {
    for(int j = 0; j < 4; j++)
    {
    if(input_array[i] < 10)
      input_array[i] >> b1[j];
     else if((input_array[i] >= 10) && (input_array[i] < 20))
       input_array[i] >> b2[j];
      else if((input_array[i] >= 20) && (input_array[i] < 30))
       input_array[i] >> b3[j];
       else if((input_array[i] >= 30) && (input_array[i] < 40))
       input_array[i] >> b4[j];
        else
         cout <<"please check the data as element "<< i <<" will not fit in a sub array" <<endl;
     }
  }
Member Avatar for jencas

Why are you using arrays when std::vectors are more appropriate?
Nevertheless, you need different j's for indexing your b1, b2, b3, b4 sub arrays
and

input_array[i] >> b1[j];

should be something like:

b1[j1++] = input_array[i];

Hey I donot understand Why you actually need the second loop.

I prefer to do this

1)initialise all elements of your subarrays to '0'
2)Remove the second for loop
3)After finding the limit in which the value can be assigned in ,
Search for a zero in the sub array and then give it the value

Hope this makes sense.

Thanks everyone. I am going to try some of your suggestions and see if I can get it to work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.