Read numbers from a txt file and find the largest and smallest

Im trying to write a program that reads in a text file of random numbers and then finds the largest and smallest numbers in the file. This is what I have.
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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void process(ifstream& fin, int& big, int& small);
//Function to read the numbers listed in a data file
//and find the biggest and smalles numbers in the file.
int main ()
{//Declare variables
ifstream fin;
int big, small;
//Input
fin.open("data.txt");
//Processing
process(fin, big, small);
//Output
if (fin.fail())
{cout<<"Input file failed to open.\n";
exit(1);}
else
{cout<< "The largest value is " << big;
cout<< "The smallest value is " << small;}
return 0;}
void process(ifstream& fin, int& big, int& small)
{int next;
fin>>big;
fin>>small;
fin.get(next);
while (big>>next)
{if (next >= big)
{big=next;}
if else (next<=small)
{next=small;}
fin.get(next);}}

but my compiler keeps telling me I have an error in my function call, that there is no function.
What did I do wrong?
I know there are a few other topics out there with pretty much the same question but nobody else seems to be using a function and I have to...
Last edited on
First of all I would have:

1
2
3
4
5
6
7
8
9
10
11
12
13
if(fin.fail())
{
    cout << "Input file failed to open.\n";
    return 1;
}
// You don't need an else because the return stops the function and the rest of the code is not read.

// Then process the file.
process(fin, big, small);

cout << "The largest value is " << big << endl;
cout << "The smallest value is " << small << endl;
return 0;


And secondly I would have the file opened in the process function not the the main function as well as the cout's.

I would also think there should be some conversion from char or string to int going on.

Look up atoi(). I am pretty sure this is you problem.
Last edited on
Topic archived. No new replies allowed.