I'm working on a program that reads a file of scores and then outputs the number of scores in certain ranges. I've got it to read the input file (scores.txt) which is set up as follows; 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

there are 8 different score ranges and I need to output how many scores are in each range. So far I'm using one range to make sure I figure this thing out. I'm very new to programming. It works if the first number is within the range, but that's as far as it gets. if the first number in the file is not within the range, it outputs nothing. I am totally lost, I'm not sure I'm even going about this the right way. Any ideas?

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX_CODE_SIZE = 50;

void readCode(ifstream& infile, int list[], int& length, bool& lenCodeOk);

int main()
{
	int codeArray[MAX_CODE_SIZE];

	int codeLength;

	bool lengthCodeOk;

	ifstream incode;
	
	incode.open("scores.txt");
	if (!incode)
	{
		cout << "Can't open the input file" << endl;
		return 1;
	}

	 readCode (incode, codeArray, codeLength, lengthCodeOk);

	 incode.close();

	 return 0;
}

void readCode(ifstream& infile, int list[], int& length, bool& lenCodeOk)
{
	int count;
	int value;
	int rangeA;

	lenCodeOk = true;

	infile >> length;
    
	if (length > MAX_CODE_SIZE)
	{
		lenCodeOk = false;
		return;
	}
	
	rangeA = 0;
	value = 0;

	for (count = 0; count < length; count++)
		infile >> list[count];
	{
		value = list[count];

		if (value >= 0 || value <= 24)
		{
			rangeA = rangeA + 1;
		}
		else
		{
			rangeA = rangeA;
		}
	}

	cout << "The number of students with scores 0 - 24 are: " << rangeA << endl;

	cout << endl;
}

Recommended Answers

All 6 Replies

line 65: That's a do nothing line, so you might as well delete it.

you need 8 ranges so create an int array with 8 elements so that array[0] represents the quantity of numbers within the first range, array[1] is the quantity of numbers in the second range, etc. etc.

int array[8] = {0}
...
...
if( value >= 0 && value <= 24)
    array[0]++;
else if( value > 24 && value < 50)
   array[1]++;
// etc

Thanks, it makes a lot of sense now. This is what I have now, but I'm not getting any output. Obviously I'm overlooking something (most likely simple), but I can't for the life of me find it.

CplusplusSyntax

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

const int MAX_CODE_SIZE = 50;

void readCode(ifstream& infile, int list[], int& length, bool& lenCodeOk);

int main()
{
	int codeArray[MAX_CODE_SIZE];

	int codeLength;

	bool lengthCodeOk;

	ifstream incode;
	
	incode.open("scores.txt");
	if (!incode)
	{
		cout << "Can't open the input file" << endl;
		return 1;
	}

	 readCode (incode, codeArray, codeLength, lengthCodeOk);

	 incode.close();

	 return 0;
}

void readCode(ifstream& infile, int list[], int& length, bool& lenCodeOk)
{
	int count;
	int value;
	int range[8] = {0};


	lenCodeOk = true;

	infile >> length;
    
	if (length > MAX_CODE_SIZE)
	{
		lenCodeOk = false;
		return;
	}
	
	value = 0;

	for (count = 0; count < length; count++)
		infile >> list[count];
	{
		value = list[count];

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

	cout << "The number of students with scores 0 - 24 are: " << range[0] << endl;
    cout << "The number of students with scores 25 - 49 are: " << range[1] << endl;
    cout << "The number of students with scores 50 - 74 are: " << range[2] << endl;
    cout << "The number of students with scores 75 - 99 are: " << range[3] << endl;
    cout << "The number of students with scores 100 - 124 are: " << range[4] << endl;
    cout << "The number of students with scores 125 - 149 are: " << range[5] << endl;
    cout << "The number of students with scores 150 - 174 are: " << range[6] << endl;
    cout << "The number of students with scores 175 - 200 are: " << range[7] << endl;
	cout << endl;
	
}

line 56: That brace is in the wrong place, move it up one line. As coded now only line 55 is part of that loop on line 54.

I corrected that, should have seen that! I still do not get any output though.

It doesn't work because the first number in scores.txt (76) is greater than MAX_CODE_SIZE (50). Just delete lines 46-50 and you will get this:

The number of students with scores 0 - 24 are: 9
The number of students with scores 25 - 49 are: 2
The number of students with scores 50 - 74 are: 0
The number of students with scores 75 - 99 are: 5
The number of students with scores 100 - 124 are: 1
The number of students with scores 125 - 149 are: 3
The number of students with scores 150 - 174 are: 5
The number of students with scores 175 - 200 are: 51

Press any key to continue . . .

Any reason to use that list array ? It isn't use for anything

while (infile >> value)
	{
		if (value >= 0 && value < 25)
		    range[0]++;
        else if(value > 24 && value < 50)
            range[1]++;
		else if (value > 49 && value < 75)
			range[2]++;
		else if (value > 74 && value < 100)
			range[3]++;
		else if (value > 99 && value < 125)
			range[4]++;
		else if (value > 124 && value < 150)
			range[5]++;
		else if (value > 149 && value < 175)
			range[6]++;
		else 
			range[7]++;
	}

Thank you very much, I totally forgot about changing that when I re-did the text file. You rock!

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.