why no output?

This program is supposed to read 10 integers from an input file, save the largest and smallest integer to an outfile, and compute the even and odd integers and save the sums to an outfile. (The contents of the file named input.dat are: 45 23 12 8 -67 56 87 -33 50 15) I've created the two files input.dat and result.dat correctly, but the program is not reading the numbers from the file.

It outputs all 0s for total and nothing at all for totalOdd and totalEven. It also doesn't output anything for largest and smallest. Why? Please help!

The code is:
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void findLargeSmall (int, int&, int&);
void findTotals (int, int&, int&, int&);

int main () {
	
	int count = 0, current = 0, large = 0, small = 0, total = 0, totalOdd = 0, totalEven = 0;

	ifstream inFile;
	ofstream outFile;
	inFile.open ("myInput.dat");
	outFile.open ("result.dat");

		while (count <= 10) {
		
	inFile >> current;

	findLargeSmall (current, large, small);
	findTotals (current, total, totalOdd, totalEven);
	
	count++;
	
	outFile << total << totalOdd << totalEven << large << small;
	}
	return 0;
}

void findLargeSmall (int current, int& large, int& small ) {

		if (large < current)
			large = current;
		else;
		if (small > current) 
			small = current;
		else;
}

void findTotals (int current, int& total, int& totalOdd, int& totalEven) {

	total += current;
		if (current %2 == 0)
			totalEven += current;
		else totalOdd += current;
}
I run your program with some simple numbers, and what I found that is the main problem is the:

1. That while statement, sometimes it's running more, or less than the number of numbers. so I change it to while(!inFile.eof())
2. Your totalOdd and totalEven shouldn't be like that, simply make it as an increment: totalOdd++; totalEven++;
3. I moved the output out of the while loop. So that the output is the end result.

This is what I use to test, and the number tested was just 1 2 3 4 5. But I think it looks good from here.
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void findLargeSmall (int, int&, int&);
void findTotals (int, int&, int&, int&);

int main () {
	
	int count = 0, current = 0, large = 0, small = 0, total = 0, totalOdd = 0, totalEven = 0;

	ifstream inFile;
	ofstream outFile;
	inFile.open ("Input.dat");
	outFile.open ("result.dat");

	while (!inFile.eof()) {
	inFile >> current;
	cout << current << "\t"; 
	findLargeSmall (current, large, small);
	findTotals (current, total, totalOdd, totalEven);
	
	count++;
	//outFile << total << totalOdd << totalEven << large << small;
	}
		
	cout << endl << "Total is: " << total << endl; 
	cout << "totalOdd is: " << totalOdd << endl; 
	cout << "totalEven is: " << totalEven << endl; 
	cout << "Large is: " << large << endl; 
	cout << "small is: " << small << endl; 
		
	return 0;
}

void findLargeSmall (int current, int& large, int& small ){
		if (large < current)
			large = current;
		else if(small > current) 
			small = current;
}

void findTotals (int current, int& total, int& totalOdd, int& totalEven) {

	total += current;
		if (current %2 == 0)
			totalEven++;
		else totalOdd++;
}
Thank you for your help! I changed some things around and this is what I have. It works now.
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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

void findLargeSmall (int, int&, int&);
void findTotals (int, int&, int&, int&);

int main () {
	
	int count = 1, current = 0, large = 0, small = 0, total = 0, totalOdd = 0, totalEven = 0;

	ifstream inFile;
	ofstream outFile;
	inFile.open ("myInput.dat");
	outFile.open ("result.dat");

		while (count <= 10) {
		
	inFile >> current;

	findLargeSmall (current, large, small);
	findTotals (current, total, totalOdd, totalEven);
	
	if (current %2 == 0)
		outFile << "Even: " << current << endl;
	else
		outFile << "Odd: " << current << endl;

	count++;
	
	}
	
	outFile << "Total\t" << "Total Odd\t" << "TotalEven\t" << "Largest\t\t" << "Smallest\t" << endl << endl;
	outFile << total <<"\t"<< totalOdd <<"\t\t"<< totalEven <<"\t\t"<< large <<"\t\t"<< small <<"\t"<< endl;

	return 0;
}

void findLargeSmall (int c, int& l, int& s) {

		if (l < c)
			l = c;
		else;
		if (s > c) 
			s = c;
		else;
}

void findTotals (int c, int& t, int& tOdd, int& tEven) {

	t += c;
		if (c %2 == 0)
			tEven += c;
		else tOdd += c;
}
Topic archived. No new replies allowed.