trying to use a function to complete this program but cannot get it to execute properly

please tell me if you see where my mistake is in this program cannot get to execute

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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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 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 . . .

*/
Last edited on
Do you get a compilation error or a runtime error?
EDIT: OOPS!
Sorry, hasty answer! Let me delete it, since it is of no help!
Last edited on
Your loop contains TWO
in >> value;
One in the loop test, one in the body of the loop. The latter will never be tested against lowest.

If anything else is wrong it is probably where you have put the input file ... which we can't do anything about.


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
#include<iostream>
#include<fstream>
#include<string>
#include<climits>     // Need this for INT_MAX
#include<sstream>     // This just to fake the input
using namespace 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.
Last edited on
Topic archived. No new replies allowed.