Can anyone tell me what's wrong with this code?

The code is supposed to read a text file, Scores, with scores integer values and display the highest and lowest score as well as the passing and failing scores, above 60 is passing. However the program only displays the scores but nothing after. I am confused and have been trying to find the mistake to no avail.


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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int read_into_array(ifstream& inf,int scores[],int size);
double print_score_statistics(ifstream& inf, int scores[], int size);
string file;
ifstream inf;
int value;
int scores[100];

int main()
{

	cout << "Please enter the file name." << endl;
	cin >> file;

	int index = read_into_array(inf, scores,100);
	print_score_statistics(inf, scores, 100); 
	inf.close();

}

int read_into_array(ifstream& inf,int scores[],int size)
	{
	    int index = 0;
		inf.open(file);

		if (inf.fail())
		{
			cout << "Error retrieving input file." << endl;
		}
		cout << "The scores follow." << endl;
		while (inf >> scores[index])
		{
			
				cout << scores[index] << endl << endl;
				index++;
			
		}

		return index = index - 1;
	}

double print_score_statistics(ifstream& inf, int scores[], int size)
{

	int next = 0, highest = 0, lowest = 0;
	int pass = 0;
	int fail = 0;

	while (size > next)
	{
		if (next > highest)
		{
			highest = next;
		}
		else
		{
			lowest = next;
		}

		if (next >= 60)
			pass++;
		else if (next < 60)
			fail++;
	}

	cout << "The highest score is: " << highest << endl;
	cout << "The lowest score is: " << lowest << endl;
	cout << "The number of passing scores are: " << pass << endl;
	cout << "The number of failing scores are: " << fail << endl;

	return 0;
}
Last edited on
You're trying to go through the scores[] array, right?
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
double print_score_statistics(int scores[], int size) //you're not using the input file stream in here, so there is no need to put it in the list of arguments
{
    int next = 0, highest = 0, lowest = 0, pass = 0, fail = 0;
    highest = scores[0];
    lowest = scores[0];
    for (int i = 0; i < size; i++)
    {
        if (scores[i] < lowest)
            lowest = scores[i];
        if (scores[i] > highest)
            highest = scores[i];

        if (scores[i] >= 60)
            pass++;
        else
            fail++;
    }
	cout << "The highest score is: " << highest << endl;
	cout << "The lowest score is: " << lowest << endl;
	cout << "The number of passing scores are: " << pass << endl;
	cout << "The number of failing scores are: " << fail << endl;
}


In your original function, next had the value of 0, and size had the value of 100. In that was the case, while (next > size) would have been false and never would have executed.
Thank you, while that did start outputting values, it is counting the non-existent values as "failed" values. My text file has 10 failing scores, and 9 passing scores. However the program is outputting 91 failed scores. Also the lowest score is seen as 0 and should be 5.
Last edited on
For anyone that comes across this I did manage to solve it, the code follows:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int read_into_array(ifstream& inf, int scores[], int size);
void print_score_statistics(ifstream& inf, int scores[], int size);
string file;
ifstream inf;
int value;
int scores[100];

int main()
{

	cout << "Please enter the file name." << endl;
	cin >> file;

	int index = read_into_array(inf, scores, 100);
	print_score_statistics(inf, scores, index);
	inf.close();

}

int read_into_array(ifstream& inf, int scores[], int size)
{
	int index = 0;
	inf.open(file);

	if (inf.fail())
	{
		cout << "Error retrieving input file." << endl;
	}
	cout << "The scores follow." << endl;
	while (inf >> scores[index])
	{

		cout << scores[index] << endl << endl;
		index++;

	}

	return index;
}
void print_score_statistics(ifstream& inf, int scores[], int size)
{
	int next = 0, highest = 0, lowest = 0, pass = 0, fail = 0;
	highest = scores[0];
	lowest = scores[0];
	for (int i = 0; i < size; i++)
	{
		if (scores[i] < lowest)
		{
			lowest == scores[i];
		}
		if (scores[i] > highest)
		{
			highest = scores[i];
		}
		if (scores[i] >= 60)
		{
			pass++;
		}
		else if (scores[i] < 60)
		{
			fail++;
		}
	}
	cout << "The highest score is: " << highest << endl;
	cout << "The lowest score is: " << lowest << endl;
	cout << "The number of passing scores are: " << pass << endl;
	cout << "The number of failing scores are: " << fail << endl;

}
Last edited on
Topic archived. No new replies allowed.