Need help getting loop to work with data from input file.

I have .txt file of 26 grades that looks like:
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

I need my program to read the data from the input file and output how many grades fall into each of the following categories (with totals):
0 - 24: 1
25 - 49: 2
50 - 74: 0
75 - 99: 6
100 - 124: 1
125 - 149: 3
150 - 174: 5
175 - 200: 8

This is what I've put together so far:


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
#include <iostream> 
#include <fstream>   

using namespace std;

int main()
{
    int grades[26]; //I'm trying to declare the data in an array
    int range1, range2, range3, range4, range5, range6, range7, range8;
    int i;
    ifstream inFile;

    range1 = 0;
    range2 = 0;
    range3 = 0;
    range4 = 0;
    range5 = 0;
    range6 = 0;
    range7 = 0;
    range8 = 0;

    inFile.open("Test_Scores.txt");

    for (i = 0; i < 26; i++)
        inFile >> grades[i];
    {
        if (grades[i] <= 24)
            range1 = range1++;
        if (grades[i] >= 25 && grades[i] <= 49)
            range2 = range2++;
        if (grades[i] >= 50 && grades[i] <= 74)
            range3 = range3++;
        if (grades[i] >= 75 && grades[i] <= 99)
            range4 = range4++;
        if (grades[i] >= 100 && grades[i] <= 124)
            range5 = range5++;
        if (grades[i] >= 125 && grades[i] <= 149)
            range6 = range6++;
        if (grades[i] >= 150 && grades[i] <= 174)
            range7 = range7++;
        if (grades[i] >= 175 && grades[i] <= 200)
            range8 = range8++;
    }

    cout << "The number of grades in each category is: " << endl;
    cout << "0 - 24: " << range1 << endl;
    cout << "25 - 49: " << range2 << endl;
    cout << "50 - 74: " << range3 << endl;
    cout << "75 - 99: " << range4 << endl;
    cout << "100 -124: " << range5 << endl;
    cout << "125 - 149: " << range6 << endl;
    cout << "150 - 174: " << range7 << endl;
    cout << "175 - 200: " << range8 << endl;

    inFile.close();

    return 0;
}


Here's the output I get:

The Number of grades in each category is:
0 - 24: 1
25 - 49: 0
50 - 74: 0
75 - 99: 0
100 - 124: 0
125 - 149: 0
150 - 174: 0
175 - 200: 0

Any help that could be offered would be greatly appreciated.
Last edited on
@tmneveu,
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
1
2
3
4
5
6
for (i = 0; i < 26; i++) // line 24
    inFile >> grades[i]; // the loop ends here (line 25)
    {
        if (grades[i] <= 24) 
            range1 = range1++;
        // ... 


Something like this, perhaps:

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
#include <iostream>
#include <fstream>

int main()
{
    if( std::ifstream file{"Test_Scores.txt"} )
    {
        const int MAX_GRADE = 200 ;
        const int DIVISOR = 25 ;
        const int NUM_RANGES = MAX_GRADE / DIVISOR ;
        int range[NUM_RANGES] = {0} ; // count of ranges (initialised to zeroes)

        int grade ;
        while( file >> grade ) // for each grade read from the file
        {
            if( grade >= 0 && grade <= MAX_GRADE ) // if it is a valid grade
                ++range[ grade / DIVISOR ] ; // increment the appropriate count
        }

        // print results
        for( int i = 0 ; i < NUM_RANGES ; ++i )
            std::cout << i*DIVISOR << " - " << i*DIVISOR + DIVISOR-1 << "  " << range[i] << '\n' ;
    }

    else std::cerr << "failed to open input file\n" ;
}
That should give an out-of-bounds error on the given data.
The issue is that 200 is 25 above and not 24 above the low bound - as an exception.

Perhaps using this method then:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>

int main() {
	std::ifstream file("Test_Scores.txt");

	if (!file)
		return std::cout << "Cannot open file\n", 1;

	constexpr size_t MAX_GRADE {200};
	constexpr size_t RANGE {25};
	constexpr size_t NUM_RANGES {MAX_GRADE / RANGE};

	size_t range[NUM_RANGES] {};

	for (int grade {}; file >> grade;)
		if (grade >= 0 && grade <= MAX_GRADE)
			++range[(grade == MAX_GRADE ? grade - 1 : grade) / RANGE];

	for (size_t i {}; i < MAX_GRADE ; i += RANGE)
		std::cout << i << " - " << i + RANGE  - (i != MAX_GRADE - RANGE) << "  " << range[i / RANGE] << '\n';
}



0 - 24  1
25 - 49  2
50 - 74  0
75 - 99  6
100 - 124  1
125 - 149  3
150 - 174  5
175 - 200  8

Last edited on
The original problem is L26. The { should be before L25.
Thank you all for the help. I used seeplus' advice and it worked. Thanks again.
Topic archived. No new replies allowed.