Complete the program using a function The program should rea
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>
 using namespace 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 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(<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
*/
 //------------------------------------------------------------------------------
/* 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 . . .
*/
Solution
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
 #include <fstream>
 #include <string>
 #include <climits>
 #include <cstdint>
using namespace std;
int getLeast(ifstream & in); // function declaration
int main() // driver method
 {
    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...\";
       
        smallestValue = getLeast(dataFile); // call the function and get the data
        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(); /// close the file
    cout << \"\ End Program.\ \"
    << endl;
    return 0;
 }
int getLeast(ifstream & in) // function initialisation
 {
    int value ; // locla variables
    int lowest ;
    in >> lowest ; // set lowest to the first value in the file.
    while ( in >> value )
    {
        if(value < lowest)   {   // Test for lowest value
            lowest = value;
        }
       in >> value;       // Read next value
    }
    return lowest ;
 }
OUTPUT :
Opening file...file opened.
 Begin searching for lowest value...done.
The lowest value found was 1
End Program.
 data.txt :
15
 10
 5
 6
 1
 20
 10
 25
 4
 7
Hope this is helpful.



