Reading a file and defining an array

My program is suppose to read integer values from a .txt document of scores. It then is suppose to determine how many students have scores within ranges of
0-24
25-49
50-74
75-99
100-124
125-149
150-174
175-200

and then take that number ans store it in a 8 element array. i beleive i am set but cant figure out why it wont read the file in a while loop. If i just use cout its fine.

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
  #include <iostream>
#include <fstream>
using namespace std;

int const SIZE = 8;

int main()
{
	ifstream infile;
	int scores[SIZE];
	int score;

	infile.open("Lab08ScoresData.txt");
	
	if (!infile)
	{
		cout << "\nInput file can't be opened";
	}

	while (infile >> score)
	{

		if (score >= 0 && score <= 24)
			scores[0] = scores[0] +1;
		else if (score >= 25 && score <= 49)
			scores[1] = scores[1] +1;
		else if (score >= 50 && score <= 74)
			scores[2] = scores[2] +1;
		else if (score >= 75 && score <= 99)
			scores[3] = scores[3] +1;
		else if (score >= 100 && score <= 124)
			scores[4] = scores[4] +1;
		else if (score >= 125 && score <= 149)
			scores[5] = scores[5] +1;
		else if (score >= 150 && score <= 174)
			scores[6] = scores[6] +1;
		else if (score >= 175 && score <= 200)
			scores[7] = scores[7] +1;
	} 

	cout << "There are " << scores[0] << " score(s) between 0-24" << endl;
	cout << "There are " << scores[1] << " score(s) between 25-49" << endl;
	cout << "There are " << scores[2] << " score(s) between 50-74" << endl;
	cout << "There are " << scores[3] << " score(s) between 75-99" << endl;
	cout << "There are " << scores[4] << " score(s) between 100-124" << endl;
	cout << "There are " << scores[5] << " score(s) between 125-149" << endl;
	cout << "There are " << scores[6] << " score(s) between 150-174" << endl;
	cout << "There are " << scores[7] << " score(s) between 175-200" << endl;
	
	return 0;
}
this is txt file.

76 89 150 135 200 76 12 100 150 28 178 189 167 200 175 150 87 99 129 149 176 200 87 35 157 189
Why do you think that the program is not reading the file? Did you get the "Input file can't be opened" message? By the way it would probably be wise to stop the program if the file doesn't open.

By the way where are you initializing your score[] array?

Topic archived. No new replies allowed.