Complete the program using a function.
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 the function
- Name: GetLeast
Parameter(s): an ifstream called infile (pass by reference - uses the &)
Returns: an int, the lowest value found in the file
HINT: The ifstream should be opened in main, before calling this function
*/
int getLeast(ifstream & in);
//------------------------------------------------------------------------------
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 dataFile ifstream variable
and assign the return value to the smallestValue variable.
*/
smallestValue = getLeast(dataFile);
cout << "done.\n" << endl;
cout << "The lowest value found was "
<< smallestValue << endl;
}
else
{
cout << "could not find or open file: " << filename
<< endl;
}
dataFile.close();
cout << "\nEnd Program.\n"
<< endl;
return 0;
}
//------------------------------------------------------------------------------
/* TODO: Define GetLeast(<params>)
Refer to the function prototype above for parameter and
return type info.
PSEUDOCODE:
Declare two int variables (you decide on appropriate variable names):
* 1 variable to hold each value as it is read from file
* 1 variable to hold the lowest value read so far (initialize to INT_MAX)
Read the first value from the file into the appropriate local variable
Repeat while not at end of file
- Test to see if the new value is smaller than the lowest read so far
If yes, save the new value to the lowest so far variable
Read the next value from the file
End the repeat
Return the smallest value that was read
*/
int getLeast(ifstream & in)
{
int value;
int lowest;
in >> lowest;
while (in >> value)
{
if (value < lowest)
{
lowest = value;
}
in >> value;
}
return lowest;
}
//------------------------------------------------------------------------------
/* Sample program output:
Opening file...file opened.
Begin searching for lowest value...done.
The lowest value found was -9122
End Program.
Press any key to continue . . .
*/
#include<iostream>
#include<fstream>
#include<string>
#include<climits> // Need this for INT_MAX
#include<sstream> // This just to fake the input
usingnamespace std;
int getLeast(istream & in); // use istream, not ifstream for greater generality
//------------------------------------------------------------------------------
int main()
{
int smallestValue = INT_MAX; // Initialize smallest to maximum range (NOT NECESSARY)
//ifstream dataFile; // File input stream
const string filename = "data.txt"; // Input file name
//cout << "Opening file...";
//dataFile.open(filename.c_str()); // Open file
stringstream dataFile( "-5 20 -10 12 3 4"); // Simulate an input file
if (dataFile) // File opened OK?
{
cout << "file opened." << endl
<< "Begin searching for lowest value...";
smallestValue = getLeast(dataFile);
cout << "done.\n\n";
cout << "The lowest value found was " << smallestValue << endl;
}
else
{
cout << "could not find or open file: " << filename << endl;
}
//dataFile.close();
cout << "\nEnd Program.\n\n";
}
//------------------------------------------------------------------------------
int getLeast(istream & in) // use istream, not ifstream for greater generality
{
int value;
int lowest;
in >> lowest;
while (in >> value)
{
if (value < lowest)
{
lowest = value;
}
// in >> value; //***** WOWSERS ... don't do this !!!
}
return lowest;
}
file opened.
Begin searching for lowest value...done.
The lowest value found was -10
End Program.