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.
#include <iostream>
#include <fstream>
#include <cstdlib>
usingnamespace 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;}
ifelse (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...
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.