I am new to C++ so I am trying to understand why the program behaves differently if you do this

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	ifstream fin;
	ofstream fout;

	fin.open("getfromthisfile.txt");
	if(fin.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}
	
	fout.open("sendtothisfile.txt");
	if(fin.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}
	
	int next;
	int smallest;
	int largest;
	
	fin >> next;
	
	smallest=largest=next;

	while (! fin.eof())
	{
		fin >> next;
		if(next<smallest)
		{
			smallest=next;
		}

		else if( largest < next)
		{
			largest=next;
		}
	}

	fout << "The largest number in getfromthisfile.txt is " << largest << endl;

	fout << "The smallest number in getfromthisfile.txt is " << smallest << endl;

	cout << "The largest number in getfromthisfile.txt is " << largest << endl;

	cout << "The smallest number in getfromthisfile.txt is " << smallest << endl;

	fin.close();
	fout.close();
	system("pause");
	return 0;
}

instead of this...

#include <fstream>
#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
	ifstream fin;
	ofstream fout;

	fin.open("getfromthisfile.txt");
	if(fin.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}
	
	fout.open("sendtothisfile.txt");
	if(fin.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}
	
	int next;
	int smallest;
	int largest;
	
	fin >> next;
	
	smallest=largest=next;

	while (! fin.eof())
	{
		fin >> next;
		if(next<smallest)
		{
			smallest=next;
		}

		else if( largest < next)
		{
			largest=next;
		}
                fin >> next;
	}

	fout << "The largest number in getfromthisfile.txt is " << largest << endl;

	fout << "The smallest number in getfromthisfile.txt is " << smallest << endl;

	cout << "The largest number in getfromthisfile.txt is " << largest << endl;

	cout << "The smallest number in getfromthisfile.txt is " << smallest << endl;

	fin.close();
	fout.close();
	system("pause");
	return 0;
}

When I do the first set of code, the program outputs the greatest value is 10 and the smallest value is -10.

When I do the second set of code, the program outputs the greatest value is 9 and the smallest value is -9.

Why does it do this?

Oh here is the data of the input file...
0 1 2 3 4 5 6 7 8 9 10
-1 0 1 2 3 4 5 6 7 8 9
-2 -1 0 1 2 3 4 5 6 7 8
-3 -2 -1 0 1 2 3 4 5 6 7
-4 -3 -2 -1 0 1 2 3 4 5 6
-5 -4 -3 -2 -1 0 1 2 3 4 5
-6 -5 -4 -3 -2 -1 0 1 2 3 4
-7 -6 -5 -4 -3 -2 -1 0 1 2 3
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2
-9 -8 -7 -6 -5 -4 -3 -2 -1 0 1
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0

and here is the data of the output file with the first set of code...

The largest number in getfromthisfile.txt is 10
The smallest number in getfromthisfile.txt is -10

and here is the data of the output file with the second set of code...

The largest number in getfromthisfile.txt is 9
The smallest number in getfromthisfile.txt is -9

I would appreciate a good explanation :)

Recommended Answers

All 5 Replies

You do not account for the second "fin >> next" in your second code.

Actually, the loops in both code snippets are wrong. Why? Because eof() doesn't work like that. It only detects eof-of-file AFTER an attempt has been made to read the last item in the file, so the last item will get processed twice.

A better way to code it is like this, which will exit the loop upon end-of-file and will not process the last item in the file twice.

while( fin >> next )
{
   // blabla
}

Actually, the loops in both code snippets are wrong. Why? Because eof() doesn't work like that. It only detects eof-of-file AFTER an attempt has been made to read the last item in the file, so the last item will get processed twice.

A better way to code it is like this, which will exit the loop upon end-of-file and will not process the last item in the file twice.

while( fin >> next )
{
   // blabla
}

But the first code works when I run it...

Which line of code are you saying does something bad? ( not that your wrong or anything :) )

Well rayborn66, BOTH of your code snippets are WRONG as Ancient Dragon already pointed out.

You should really go back to your former thread and there you should carefully study my suggested code to understand why I put fin >> next within while loop exactly in the place where it is placed. You can't change the order of the statements as much as you please.

-- tesu

Also just like to point out another flaw

fin.open("getfromthisfile.txt");
	if(fin.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}
	
	fout.open("sendtothisfile.txt");
	if(fin.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}

WHY are you checking if the istream failed again after opening an ostream, bad use of copy and paste this kind of mistake can also cause big problems as i learned the hard way not so long ago, copy+paste can be good and also soooooo bad.
You also didnt even change the output message but its all the same mistake really.

commented: good catch :) +28
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.