954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

file to array question

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;
     }
  }
demroth
Light Poster
44 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

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];
jencas
Posting Whiz
366 posts since Dec 2007
Reputation Points: 395
Solved Threads: 71
 

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.

Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
 

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

demroth
Light Poster
44 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You