Finding minimum and maximum line by line (Fstream)

Pretty straightforward question here. I'm used to setting an upper and lower value for variables, and then seeing if a bunch of numbers are lower than that (like I did below). I want to know how I could do the same thing, but by assigning the min and max to the first value of the row. When I was playing around with it, I did something like min = num, and then seeing if min was greater than or less than num, but it ended up being 5 > 5, 55 > 55, etc. and I wasn't going anywhere.

My code definitely isn't perfect - For some reason, I get the output that I want for all of the rows of data that I need (I have 8 rows of numbers, with each of them having 7 numbers in that line) but I also get two additional outputs where I get 7 zeros, average = 0, etc.

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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    int num = 0;
    int count = 0;
    int total = 0;
    int average = 0;

    ifstream fileIn;
    fileIn.open("File2.txt");

    //ofstream fileOut("Lab5A_Output.txt");

    if (!fileIn) {

	cout << "\nError opening file...Closing program. \n";
	fileIn.close();

    }

    else {

	while (fileIn >> num) {

	    for (int i = 0; i < '\n'; i++) {

        cout << endl;

		int num = 0;
		int count = 0;
		int total = 0;
		int average = 0;
		int min = 1000;
		int max = 0;

		for (int k = 0; k < 7; k++) {

		    fileIn >> num;
		    total += num;
		    cout << num << " ";
		    count++;

		    if (num < min)
		    min = num;

		    if (num > max)
		    max = num;
		}

		average = total / count;

		cout << "\n\nTotal is " << total << "." << endl;
		cout << "Total amount of numbers is " << count << "." << endl;
		cout << "Average is " << average << "." << endl;
        cout << "Lowest number is " << min << endl;
        cout << "Largest number is " << max << endl;

	    }

	    fileIn.close();

	}
    }

//fileOut.close();

    return 0;
}
for (int i = 0; i < '\n'; i++) {


What is the output for the following code?
1
2
3
4
5
6
7
8
9
#include <iostream>

int main()
{
     for (int i=0; i<'\n'; ++i)
         std::cout << i << '\n' ;

     std::cout << (int)'\n' << '\n' ;
}


http://ideone.com/sSmbi1
Topic archived. No new replies allowed.