I am struggling in this class and I am stumped on this problem trying to find the lowest value of the data file. I have added the GetLeast function and called it in the main function. I'm unsure of how to loop to find the smallest value. I started writing a while loop, but as stated before I do not know how to make it work correctly. I might be completely wrong in my approach so far. I'm not asking for someone to write everything for me, but to help me with starting the loop and calling it correctly.
Complete the program using functions.
The program should read all the values from the input file,
find the lowest value, and print the lowest value found.
*/
#include<iostream>
#include<fstream>
#include<string>
usingnamespace std;
/* TODO: Write the declaration (prototype) for void function GetLeast that
takes an ifstream parameter called infile as an input parameter
that is changed, and that has an int parameter called lowest
that returns a value.
Document the data flow for the parameters with appropriate comments.
*/
void getLeast(ifstream, int);
//------------------------------------------------------------------------------
int main()
{
int smallestValue = INT_MAX; // Initialize smallest to maximum range
ifstream dataFile; // File input stream
const string filename = "data.txt"; // Input file name
cout << "Opening file...";
dataFile.open(filename.c_str()); // Open file
if(dataFile) // File opened OK?
{
cout << "file opened." << endl
<< "Begin searching for lowest value...";
/* TODO: Call function GetLeast, passing the ifstream variable
and int variable as arguments.
*/
getLeast(dataFile, smallestValue);
cout << "done.\n" << endl; // Print result
cout << "The lowest value found was "
<< smallestValue << endl;
}
else // Problem opening file
{
cout << "could not find or open file: " << filename
<< endl;
}
dataFile.close();
cout << "\nEnd Program.\n"
<< endl;
return 0;
}
//------------------------------------------------------------------------------
/* TODO: Define function GetLeast so that it reads all of infile as a series
of int values and returns the lowest integer input from infile.
HINT: Declare a local variable to read each int value.
Write an event-controlled loop using EOF to control
the loop; Be sure to read the first value, test for
EOF, and just before the end of the loop, read the
next value. (The loop process requires a decision to
save the lowest value, if necessary, each iteration.)
*/
void getLeast(ifstream inFile, int lowest)
{
inFile.open("data.in");
int value;
while(inFile >> lowest)
{
int getLeast(ifstream & in)
{
int value ;
int lowest ;
in >> lowest ; // set lowest to the first value in the file.
while ( in >> value )
{
// I'm sure you can figure out what goes here.
}
return lowest ;
}