10 Replies - 265 Views - Last Post: 02 November 2012 - 04:20 PM Rate Topic: -----

#1 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Program not reading correct value from file

Posted 02 November 2012 - 01:47 PM

On line 6, the value comes out to be "value = -858993460". The first value in the file is 10, so why is it coming out to this absurd number?

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;
}


Here is the whole program if necessary.

#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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Program not reading correct value from file

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • Joined: 25-December 09

Re: Program not reading correct value from file

Posted 02 November 2012 - 01:55 PM

Okay let's look at the following snippet:
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;
}


Where in this function do you actually ever open a file? What is the file's name?

Edit: Yes posting the complete program was the right thing to do.

Jim

This post has been edited by jimblumberg: 02 November 2012 - 01:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Program not reading correct value from file

Posted 02 November 2012 - 02:02 PM

Yes! Thank you! I had been looking at this program for an hour trying to figure it out. It's always something so simple...
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • Joined: 25-December 09

Re: Program not reading correct value from file

Posted 02 November 2012 - 02:06 PM

But, should you even be opening the file in this function? Wouldn't it be better to pass a reference to the file stream you opened in main() instead of trying to reopen the file?

Jim
Was This Post Helpful? 1
  • +
  • -

#5 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Program not reading correct value from file

Posted 02 November 2012 - 03:15 PM

Hmmmm I see what you're saying. Not exactly sure how to do that reference that... I tried adding "ifstream" to the parameters of the prototype and then fileName to the GetLeast parameters but got no where. If you could guide me on doing that, that would be great.
Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4133
  • View blog
  • Posts: 12,865
  • Joined: 25-December 09

Re: Program not reading correct value from file

Posted 02 November 2012 - 03:18 PM

Show the code where you tried to pass the stream instance by reference. You may want to take a look at the function tutorials linked in my signature, they should help explain how to properly pass parameters to and from functions.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Program not reading correct value from file

Posted 02 November 2012 - 03:38 PM

I think I'm confusing myself. The point of the exercise was to make the prototype "void GetLeast()" a value returning function instead of a void function. Is it still possible to pass-by-reference without a void function?
Was This Post Helpful? 0
  • +
  • -

#8 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Program not reading correct value from file

Posted 02 November 2012 - 03:44 PM

Of course it is. However, if you need to ask such a thing, I'd strongly suggest you follow Jim's advice on those tutorials... ;) Understanding functions, returning and passing, well it is vital.
Was This Post Helpful? 1
  • +
  • -

#9 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Program not reading correct value from file

Posted 02 November 2012 - 03:52 PM

I got it to work now. I have only done pass by reference with void functions. I wasn't sure if you could do it with others.

Here is my final code:

#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(ifstream&);

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

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(dataFile);


		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;
}

Was This Post Helpful? 0
  • +
  • -

#10 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Program not reading correct value from file

Posted 02 November 2012 - 03:58 PM

To make your function also accept other input streams you could change your parameter type from std::ifstream& to std::istream& - that way (if only for debugging purposes) you could pass std::cin to it, or prepared std::istringstream... Or whatever derived input stream.
Was This Post Helpful? 0
  • +
  • -

#11 Reynolds773  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 29-October 12

Re: Program not reading correct value from file

Posted 02 November 2012 - 04:20 PM

Thanks, but I haven't been taught that yet, so I will not that much in depth. Thanks for the help everyone.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1