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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  #include <iostream>
#include <fstream>
#include <iomanip>
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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];
}



Also instead of

int range[] = {0, 0, 0, 0, 0, 0, 0, 0};

you could write

int range[MAX_VALUE / RANGE_SIZE] = {};
Last edited on
Topic archived. No new replies allowed.