I am having issues with this program. I am confused on how and what functions to use, how to get my program to get the information from a file, and I am extremely confused on arrays. I've been reading this chapter relevant to the problem through and through and I am stilling coming up with nothing.

Here is my problem: Write a program that reads a file consisting of students test scores in the range 0-200, 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, 175-200. Output the score ranges and the number of students. (Run your program with the following input data : 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.

Here what I have so far.. it's not much, and it doesn't even work anyway. Can anyone help me on what to add to make this work correctly? and explain what does what? I have restarted this program a handful of times and each time I hit a brick wall and don't know what to do. I am also getting blasted with error messages such as:

invalid types `int[int]' for array subscript

initializing argument 1 of `int getStuff(int)'
`int getStuff(int)':
invalid conversion from `int*' to `int'

I've sat here for hours upon hours trying to make sense of this, but I cannot do it, I can't even begin to comprehend where to start, and with what to start with.

Please someone help!

#include <iostream>
#include <fstream>

using namespace std;

int getStuff(int score);

int main()
{
    int score[7];
    
    ifstream inFile;
    
    inFile.open("scores.txt");
    
    if (!inFile) 
    {
       cout << "Can't open input file " << endl;
       return 1;
               } 
    
    getStuff(score);
    
    inFile.close();
    
    cin.get();
    return 0;
}

int getStuff (int score)
{
     int rangescore;
     
     if (score >= 0 && score <= 24)
       rangescore[0]++;
       
       else if (score >= 25 && score <= 49)
       rangescore[1]++;
       
       else if (score >= 50 && score <= 74)
       rangescore[2]++;
       
       else if (score >= 75 && score <= 99)
       rangescore[3]++;
       
       else if (score >= 100 && score <= 124)
       rangescore[4]++;
       
       else if (score >= 125 && score <= 149)
       rangescore[5]++;
       
       else if (score >= 150 && score <= 174)
       rangescore[6]++;
       
       else if (score >= 175 && score <= 200)
       rangescore[7]++;
       
       else
       cout << "That was an invalid entry." << endl;   
    
    return rangescore;
 }

I just solved this problem like a couple weeks ago. Check out this thread. I'm sure it will answer pretty much all your questions.

Give this a shot on your own and see how you do. Then, if you have anymore questions let us know.

I would do something like that:

(Note this code might not work.. Didn't test it and don't really have time to.. But you should get the idea)

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
	ifstream file;
	file.open("scores.txt");

	if(file.fail())
	{
		cout << "Problem opening the file" << endl;
	}
	else
	{
		int score;
		int rangescore[7] = {0}; //rangescore 0 to 7 all to 0 
		file >> score;  //Initiale read. If the file is empty it skips the while loop below
		while(!file.eof()) //Read items until program reaches EOF (End of File)
		{
			if (score >= 0 && score <= 24)
				rangescore[0]++;

			else if (score >= 25 && score <= 49)
				rangescore[1]++;

			else if (score >= 50 && score <= 74)
				rangescore[2]++;

			else if (score >= 75 && score <= 99)
				rangescore[3]++;

			else if (score >= 100 && score <= 124)
				rangescore[4]++;

			else if (score >= 125 && score <= 149)
				rangescore[5]++;

			else if (score >= 150 && score <= 174)
				rangescore[6]++;

			else if (score >= 175 && score <= 200)
				rangescore[7]++;
			else
				cout << "Error occured" << endl;

			file >> score;

		}

		for(int i = 0; i <= 7; i++)
		    cout << rangescore[i] << endl;
		
		file.close();

	}
	
}
commented: good code and very well formatted. muy bueno. +6
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.