How to make Value Returning function

I keep getting an error that says "lowest is being used without being initialized". I understand what it means. The lowest variable has no value in the GetLeast function and is trying to be used there. Where do I define it?

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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

/*	TODO:	Modify as necessary to be a value returning function
			instead of a void function.
*/

int GetLeast();

//------------------------------------------------------------------------------

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:	Modify as necessary for a value returning function.
		*/

		smallestValue = GetLeast();


		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:	Modify as necessary for a value returning function
			instead of a void function.
*/

int GetLeast()
{
	ifstream infile;
	int lowest;
	int value;
	infile >> value;			// Priming read

	while(infile)				// Test for EOF
	{
		if(value < lowest)		// Test for lowest value
			lowest = value;

		infile >> value;		// Read next value
	}
	return lowest;
}
The issue is because of line 69.

if(value < lowest)

You are trying to test value against lowest without assigning lowest a value. The way I would fix it is add

lowest = value;

below line 65, basically setting the first "value" as the initial lowest.


Okay, I understand what you are saying. I used the debugger and at line 65 the value is reading "value = -858993460" which isn't right, because the first value in the file is 10. Did I declare the wrong ifstream??

This is the code with your added suggestion:

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
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

/*	TODO:	Modify as necessary to be a value returning function
			instead of a void function.
*/

int GetLeast();

//------------------------------------------------------------------------------

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:	Modify as necessary for a value returning function.
		*/

		smallestValue = GetLeast();


		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:	Modify as necessary for a value returning function
			instead of a void function.
*/

int GetLeast()
{
	ifstream infile;
	int lowest;
	int value;
	infile >> value;			// Priming read
	lowest = value;

	while(infile)				// Test for EOF
	{
		if(value < lowest)		// Test for lowest value
			lowest = value;

		infile >> value;		// Read next value
	}
	return lowest;
}
Last edited on
bump
Still can't figure out why the first value passed in is an absurd number on line 65.
Last edited on
The function has no idea what file you're talking about. You never open it in GetLeast or pass it in, so it doesn't know. I believe you can pass the dataFile stream to the function, then just use that rather than declaring it inside the function, though I think it has to be passed by reference so look out for that.
On line 62 you declared a new ifstream object, but didn't use it to open the file so it's not associated with anything. Not sure if it's even necessary or feasible to declare a 2nd one and associate it with the file you are working with. I would try removing lines 62 and changing lines 65 and 73 to read dataFile >> value.
Last edited on
dataFile is local to main(), you need to pass it to your function:
1
2
3
4
5
6
7
8
9
10
11
12
void myFunc(ifstream& input)
{
  int ch;
  while ((ch = input.get()) != ios::eof) cout << (char)ch;
}

int main(void)
{
  ifstream myFile("data.txt");
  if (myFile.is_open())  myFunc(myFile);
  return 0;
}
Last edited on
I just figured it out. I never actually opened the file in the function. Just had to add "infile.open("data.txt");" after line 61.

Thanks for the help!
Topic archived. No new replies allowed.