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.

Also, I keep getting an error message for line 43 that says "overloaded function differs only by return type" and "redefinition; different basic types".

This is the umpteenth I've rewritten everything. I'm proud of getting this far, but what all is wrong?

The code is:

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int findLargeSmall (int, int&, int&);
int 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;
}

Recommended Answers

All 3 Replies

The return type of line 8 and 43 must be the same. The same with the other function. Change the return type of functions prototypes on line 8 and 9 to void and your problem will be solved.

Oh, thank you. Staring at it all day made it all just blur together.
Now, it will run and compile, but it outputs all 0's for total and nothing for totalOdd, totalEven, largest, or smallest. Why won't it output?

My guess is the input file isn't getting opened. Afte the open statement check to see that it was opened.

if( !Infile.is_open() )
{
   cout << "Open input file failed\n";
   return 1; // abort the program
}

use your compiler's debugger and step through the program one line at a time, that way you can see how the program is being executed and the value of all variables.

Also remove the else on line 37. You want both if statements to be executed because they are calculating both the largest and smallest value.

line 28: put "\n" at the end of the line so make the output file easier to read.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.