Need a little help

New to C++ and first only in the sixth week of class so I apologize for my bad coding up front. Ok so I am trying to write an array that will read numbers from a .txt file and then do 4 things. Output lowest, output highest, output sum and output average. I can get the file to open and I know that part is going fine because I can get it to print the numbers from the .txt file. But, when I input the rest of the code, I am unsure where they numbers are coming from. The .txt file looks like this
47
89
65
36
12
25
17
8
62
10
87
62

Code I've written is this.
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
  #include <iostream>
#include<fstream>

using namespace std;




int main()
{
	const int SIZE = 12;
	int numbers[SIZE];
	int highest, lowest, total =0, count = 0;
	double average;

	ifstream in;

	in.open("numbers.txt");

	while (count < SIZE && in >> numbers[count])
		count++;

		highest = numbers[SIZE];

		for (count = 1; count < SIZE; count++)
			highest = numbers[count];
		{
			if (numbers[count] > highest)
				highest = numbers [count];
				cout<<"The highest number in the array is " <<highest<<endl;
		}
	
		lowest = numbers[SIZE];

		for (count = 1; count < SIZE; count++)
			lowest = numbers[count];
		{
			if (numbers[count] < lowest)
				lowest = numbers [count];
			cout<<"The lowhest number in the array is " <<lowest<<endl;
		}


		for (int count=0; count < SIZE; count++)
			total+= numbers[SIZE];
			cout<<"The sum of the numbers in the array is " <<total<<endl;


		for (int count=0; count < SIZE; count++)
			total += numbers[SIZE];
			average = total / SIZE;
			cout<<"The average of the numbers in the array is " <<average<<endl;
			
	in.close();




	system("pause");
	return 0;
}
closed account (48T7M4Gy)
You aren't reading the values in from the file.

You're opening the file in line 18 and closing it in line 54. You need to read the data from the file and put it in the array and then process the array. The junk numbers are just your blank array that hasn't been initiated or supplied with data from your file.

You'll need a loop to read in each data element. Use a while or for loop even better because you know how many numbers are on file.

Looks good otherwise :)

http://www.cplusplus.com/doc/tutorial/files/
would it be something like SIZE = numbers[count] ?
closed account (48T7M4Gy)
I'm not sure what you mean but:

You need to count the items as they are read into the array ... along the lines of:

1
2
3
4
for (int i = 0; i < SIZE]; i++)
{
 in >> numbers[i];
}
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
#include <iostream>
#include<fstream>

// using namespace std; // *** avoid, just qualify names with std::

int main()
{
    const int SIZE = 12;
    int numbers[SIZE] = {0}; // *** initialise to all zeroes; just being safe

    // *** postpone defining variables to as late as possible
    //int highest, lowest, total =0, count = 0;
    //double average;
    int count = 0 ;

    // ifstream in;

    // in.open("numbers.txt");
    std::ifstream in("numbers.txt"); // *** constructor opens the file


    while ( count < SIZE && in >> numbers[count] )  ++count ; //count++;
    // at this point, count holds the count of numbers that were read

    if( count == 0 ) return 1 ; // we haven't read even one number

    // the positions of the count numbers are 0, 1, ..., count-1
    // last number is at count-1
    // highest = numbers[SIZE];

    // set the highest and lowest to the first number, at position 0
    int highest = numbers[0] ;
    int lowest = numbers[0] ;
    int total = numbers[0] ; // and the total too

    // for (count = 1; count < SIZE; count++)
    for( int i = 1 ; i < count ; ++i ) // for each number from position 1 up to count-1
    {
        // highest = numbers[count];
        // the number we are looking at now is the one at position 'i'
        if ( numbers[i] > highest ) highest = numbers [i];
        // cout<<"The highest number in the array is " <<highest<<endl;

        // lowest = numbers[SIZE];
        if ( numbers[i] < lowest ) lowest = numbers [i];

        // for (count = 1; count < SIZE; count++)
        //	lowest = numbers[count];
        // {
        //	if (numbers[count] < lowest)
        //		lowest = numbers [count];
        //	cout<<"The lowhest number in the array is " <<lowest<<endl;

        total += numbers[i] ;
    }

    // compute average and print results *after* we have processed all the numbers
    const double average = total / double(count) ; // *** avoid integer division

    std::cout << "  count: " << count << '\n'
              << " lowest: " << lowest << '\n'
              << "highest: " << highest << '\n'
              << "  total: " << total << '\n'
              << "average: " << average << '\n' ;

    // return 0; // not really necessary. there is an implicit return 0 at the end of main()
    // the file is automagically closed when the variable 'in' goes out of scope
}
Would

for (int count = 0; count < SIZE]; count++)
{
in >> numbers[count];
}

be best since I am using numbers[count] everywhere else?
An array isn't necessary here. I would get rid if it completely.

1
2
3
4
5
6
7
8
9
10
11
set highest = minimum possible value
set lowest = maximum possible value
set count = zero
set total = zero
Read in each number from the file,
    add it to the total
    add 1 to count
    store new highest or lowest if appropriate

calculate average
print results
closed account (48T7M4Gy)
nubers[count] is quite ok but Chervil is right you can eliminate the array altogether and you only need to make small changes to the nature and extent of your existing code and while loop.
Thank you to everyone that responded and helped. I was able to get it running with a little tweaking here and there from all of your recommendations. As far as getting rid of the array, the assignment was about arrays so, I could not just get rid of them. I understand what you were trying to say tho and TY.
Topic archived. No new replies allowed.