Inputing integers from file: Last number is repeating itself

Below is my input. Everything works how I want it to, except the last number (369), gets repeated on the last sequence, as shown below. Would appreciate if someone could steer me in the right direction...Thanks.

Also, just to add, in the future, I will no doubt employ stringstream, as well as arrays (when needed) when processing lines. It's just that I need to not use them for just this time.

Thanks again.

INPUT:

346 130 982 90 656 117 595
415 948 126 4 558 571 87
42 360 412 721 463 47 119
441 190 985 214 509 2 571
77 81 681 651 995 93 74
310 9 995 561 92 14 288
466 664 892 8 766 34 639
151 64 98 813 67 834 369


OUTPUT:

346 130 982 90 656 117 595 

Total is 2916.
Average is 416.
Lowest number is 90
Largest number is 982

415 948 126 4 558 571 87 

Total is 2709.
Average is 387.
Lowest number is 4
Largest number is 948

42 360 412 721 463 47 119 

Total is 2164.
Average is 309.
Lowest number is 42
Largest number is 721

441 190 985 214 509 2 571 

Total is 2912.
Average is 416.
Lowest number is 2
Largest number is 985

77 81 681 651 995 93 74 

Total is 2652.
Average is 378.
Lowest number is 74
Largest number is 995

310 9 995 561 92 14 288 

Total is 2269.
Average is 324.
Lowest number is 9
Largest number is 995

466 664 892 8 766 34 639 

Total is 3469.
Average is 495.
Lowest number is 8
Largest number is 892

151 64 98 813 67 834 369 

Total is 2396.
Average is 342.
Lowest number is 64
Largest number is 834

369 369 369 369 369 369 369 

Total is 2583.
Average is 369.
Lowest number is 369
Largest number is 369


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

#include <iostream>
#include <fstream>

using namespace std;
const int NumPerLine=7;

int main()
{
    int num = 0;
    int total = 0;
    float average = 0;
    int min = 0;
    int max = 0;

    ifstream fileIn;

    fileIn.open("File2.txt");
    ofstream fileOut("Output.txt");

    //Ensure that the file opens. If it does not, display error message.

    if (!fileIn) {

	cout << "\nError opening file...Closing program. \n";
	exit(1);
    }

    else {

    while (fileIn) {

    cout << endl;

	int total = 0;

	if(fileIn){
	for (int k = 0; k < NumPerLine; k++) {

	fileIn >> num;
	if (k == 0)
	{
	max = num;
        min = num;
	}

	total += num;
        fileOut << num << " ";

        if (num>max)
        max=num;

        if (num<min)
        min=num;
     		}

	average = total / NumPerLine;

        fileOut << "\n\nTotal is " << total << "." << endl;
        fileOut << "Average is " << average << "." << endl;
        fileOut << "Lowest number is " << min << endl;
        fileOut << "Largest number is " << max << endl << endl;
		}
	}
	fileIn.close();
	fileOut.close();

    cout << "\nData has been processed, and copied to the file, \"Output.txt\"." << endl << endl;
    }
    return 0;
}
Last edited on
Also, for what it's worth, on line 37, if I do something like, "while(fileIn >> num),"
I don't get the extraneous code, but my output skips the first number.
The problem here, as is so often the case, is that the status of the file is checked before reading from it, rather than afterwards. Thus the check if(fileIn){ at line 37 is pretty much useless, in part because the while condition at line 31 has already ensured that condition is true.

What you need to do is to check each time an attempt is made to read a number from the, whether or not it succeeded.
You might for example do something like this, though I'm pretty sure there are neater ways, this is more of a quick fix:
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
        int total = 0;
        int count = 0;

        for (int k = 0; k < NumPerLine; k++)
        {
            fileIn >> num;
            if (!fileIn) // was the number read from  the file successfully?
                break;   // no - then exit from the for loop
            count++;

            if (k == 0)
            {
                max = num;
                min = num;
            }

            total += num;
            fileOut << num << " ";

            if (num > max)
                max = num;

            if (num < min)
                min = num;
        }

        if (count > 0)
        {
            average = total / count;

            fileOut << "\n\nTotal is " << total << "." << endl;
            fileOut << "Average is " << average << "." << endl;
            fileOut << "Lowest number is " << min << endl;
            fileOut << "Largest number is " << max << endl << endl;
        }
Thanks, man. I used your code and still got 369, although I only got it one time (as shown below). You were definitely right about line 37. I figured out as you did, that it is useless in this code.

369 

Total is 369.
Average is 52.
Lowest number is 369
Largest number is 369
Well, I tested the code before I posted it, not sure went wrong for you. Did you include the new count variable and associated code?

I guess you didn't as you could not have got a value of 52 for average if you did.
Last edited on
Ok, got it...For some reason, my "fileIn >> num;" in the loop was below the break/count++/etc. Thanks!!

Going to be glad when this is done, so I can use stringstream when I'm processing lines next time.
Topic archived. No new replies allowed.