I am working on class problem to read scores from a file and output the number of scores in 8 buckets. My code is below...but when I try to run, it seems to just get stuck in a loop. Can anyone help? Write a C++ program that reads a file named "grades.txt" consisting of students' test scores in the range 0-200 (all integer values). (The first number in the file specifies the number of grades it contains. You may also assume that the number of grades will be less than 100.) It should then determine the number of students having scores in each of the following ranges: 0-24, 25-49, 50-74, 75-99, 100-124, 125-149, 150-174, and 175-200. Output the score ranges and the number of students. Given the following file input:
26 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
//Header file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
/******************************
Function: Reads students scores and displays distribution.
Preconditions:
Input file with student's scores: Score.txt
Postconditions:
Outputs to screen only
Dates of Coding Modifications: October 29, 2014
*******************************/
//Function prototypes
void initialize(int buckets[]);
void getStudent(int index, int buckets[]);
void printArray(int category, int buckets []);
int main()
{
//Declare variables
int index=0; //Category for grades
int score=0; //Individual test score
int buckets[8]; //Array for tracking # of scores in each bucket
//Attempt to Open File....Error if cannot open
ifstream infile;
infile.open("scores.txt");
if (!infile)
{
cout<<"Cannot open input file."<<endl;
return 0;
}
//Initialize Array Values
initialize(buckets);
//Calculate number of grades in each bucket
getStudent(index, buckets);
//Output Results
printArray(index, buckets);
return 1;
}
void initialize(int buckets[])
{
int j=0;
for (j=0; j<8; j++)
{
buckets[j]=0;
}
}
void getStudent(int index, int buckets[])
{
int score;
ifstream infile;
infile.open("scores.txt");
while(!infile.eof())
{
cin>>score;
index=score/25;
if(index>=0 && index<8) ++buckets[index];
}
return;
}
void printArray(int index, int buckets[])
{
string scoreRange;
cout << "Score Range" << " ";
cout << "Number of Student" << endl;
for(index = 0; index < 7; index++)
{
switch (index)
{
case 0:
scoreRange= "0-24";
break;
case 1:
scoreRange= "25-49";
break;
case 2:
scoreRange= "50-74";
break;
case 3:
scoreRange= "75-99";
break;
case 4:
scoreRange= "100-124";
break;
case 5:
scoreRange= "125-149";
break;
case 6:
scoreRange= "150-174";
break;
case 7:
scoreRange= "175-200";
break;
}
cout << scoreRange << " ";
cout << buckets[index] << endl;
}
return;
}