Help with file input, finding lowest value

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
 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>
using namespace 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)
	{
Last edited on
Anyone? I really need help getting started
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 ;
}
Topic archived. No new replies allowed.