Question: 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
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
/* 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 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. */
cout << "done. " << 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 << " End Program. " << endl;
return 0; }
//------------------------------------------------------------------------------
/* TODO: Define GetLeast(
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
*/
//------------------------------------------------------------------------------
/* 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 . . .
*/
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
