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?
#include<iostream>
#include<fstream>
#include<string>
usingnamespace 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;
}
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??
#include<iostream>
#include<fstream>
#include<string>
usingnamespace 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;
}
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.