### Reading in data into array from a loop

Right now Im trying to write a program that can take an input file that has a bunch of integers, and then show the amount of numbers in certain ranges. For example:

If the input file has 20, 30, 40, 50, 60. And the ranges are 1-20, 21-40, 41-60

The output would be

1-20: 1 21-40: 2 41-60: 3

etc.

Im new to programming, so Im just having a little problem with my code, I know im close. Im using arrays to store them, but I wasnt sure how to make an array that adapts to how many integers there are in a file. So this is what I got so far. (Just for this exercise, Im trying to make the max amount the array will store 100 integers).

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104`` `````` #include #include #include using namespace std; int ranges(int); int main() { ifstream indata; ofstream outdata; indata.open("scores1.txt"); outdata.open("scoresoutput.txt"); if(!indata) { cout << "Unable to open file" << endl; return 1; } int range[] = {0, 0, 0, 0, 0, 0, 0, 0}; int score[50]; while(indata) { for(int i=0; i < 50 ; i++) { indata >> score[i]; if(0 <= score[i] <= 24) { range[0]++; } else if(25 <= score[i] <= 49) { range[1]++; } else if(50 <= score[i] <=74) { range[2]++; } else if(75<= score[i] <= 99) { range[3]++; } else if(100 <= score[i] <= 124) { range[4]++; } else if(125 <= score[i] <= 149) { range[5]++; } else if(150 <= score[i] <= 174) { range[6]++; } else if(175 <= score[i]<= 200) { range[7]++; } indata.ignore(1); } } outdata << "Range" << setw(10) << "Number of Students" << endl << "0-24" << setw(20) << range[0] << endl<< "25-49" << setw(20) << range[1] << endl << "50-74" << setw(20) << range[2] << endl << "75-99" << setw(20) << range[3] << endl << "100-124" << setw(20) << range[4] << endl << "125-149" << setw(20) << range[5] << endl << "150-174" << setw(20) << range[6] << endl << "175-200" << setw(20) << range[7] << endl; return 0; } ``````

Right now the my output is just showing 0-24 50. And the other ranges arent holding any values. Can someone tell me what Im doing wrong?
you write the if condition the wrong way.

This:
`if(0 <= score[i] <= 24) // wrong! `

Has to be:
`if((0 <= score[i]) && (score[i] <= 24))`

and the other as well.

There's a problem with line 38. You have an (unmodifed) value even if you pass the end of file (one value too much). To solve that you should write

`if(indata >> score[i])`

Currently in your program there's no reason that `score` has to be an array (hence the loop on line 35 is unnecessary, ie `while(indata >> score)`).

A simple `int score` would suffice
 I wasnt sure how to make an array that adapts to how many integers there are in a file

I strongly recommend you look into using STL vectors, instead of C-style arrays. You should use them in preference to arrays in almost all circumstances, and it's never too early to get into the habit of using them :)

For one thing, they will resize themselves as necessary, to adjust to the number of elements you want to store in them.
For the exercise you are doing there is no any need to have an array to store inputted values. You need to have only one array that corresponds to the number of ranges.
Also instead of the numerous if statements you could write cery simply

 ``1234567891011121314`` ``````const int MAX_VALUE = 200; conts int RANGE_SIZE = 25; while ( indata ) { int value; indata >> value; size_t n = value / 25; n = n >= MAX_VALUE / RANGE_SIZE ? MAX_VALUE / RANGE_SIZE - 1 : n; ++range[n]; }``````