ifstream largest and smallest number

Hello, i'm writing a basic program that currently reads a list of numbers from a file
the file contains ... the program should find the largest and smallest number and then display them to the screen.. not sure how to go about doing it . If someone can guide me through how to do it would be good hanks.
1
2
3
4
5

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;
void  file (ifstream &in_stream);
int main(void)
{
   ifstream read;
   ofstream out;

   read.open("nums.txt");
if(read.fail())
{
	cout <<"input file opening failed";
	exit(1);
}
 file(read);
 read.close();


}

void  file (ifstream &in_stream)
{
	int numbers;

in_stream >> numbers;
while(!in_stream.eof())
{
	cout<<numbers;
	in_stream >> numbers;
	
	
	
}

	
}

Open in new window

businessesatozAsked:
Who is Participating?
 
farzanjConnect With a Mentor Commented:
With a few more changes.  Let me know if you need something else.


#include <iostream>
#include <fstream>

using namespace std;

void read(ifstream &my_file);

int main(void)
{
	ifstream in_stream;
	in_stream.open("nums.txt");

	if(in_stream.fail())
	{
		//send error message to std error
		cerr << "unable to read file" << endl;
	}

	read(in_stream);
	return 0;
}

void read (ifstream &my_file)
{
	int largest, smallest, numbers;

	my_file >> numbers;

	//initialize the value of largest and smallest
	largest = smallest = numbers;
	while(! my_file.eof())
	{
		if(numbers > largest)
		{
			largest = numbers;
		}
		else if(numbers <smallest)
		{
			smallest = numbers;
		}
		my_file >> numbers;
	}
	cout << "Smallest number is : " << smallest << endl;
	cout << "Largest number is  : " << largest  << endl;
	return;
}

Open in new window

0
 
adamnlCommented:
Keep a variable for the largest number and for the smallest. Read through the file line by line and compare to the variables. If its smaller than the variable that holds your smallest value replacce it with the number you're reading. Same for largest but compare if its larger than.
0
 
businessesatozAuthor Commented:
in order to read something line by  line then i have to use the getline right?
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
adamnlCommented:
Yes
0
 
businessesatozAuthor Commented:
how would i do that? thanks..
0
 
businessesatozAuthor Commented:
thanks i got it .. i did not use the getline though.
0
 
adamnlCommented:
You're welcome :)
0
 
businessesatozAuthor Commented:
i realized i made a mistake... looks like i got the largest one to print fine but i cannot seem to print the smallest one.. here is my code below.


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

void read(ifstream &my_file);

int main(void)
{

ifstream in_stream;
in_stream.open("nums.txt");

if(in_stream.fail())
{
  cout <<"unable to read file";
}

read(in_stream);
}

void read (ifstream &my_file)
{

int largest=0;
int smallest=0;

int numbers;
my_file >> numbers;
 while(! my_file.eof())
 {
if(numbers > largest)
{
 largest=numbers;
}
else if(numbers <smallest)
{
smallest=numbers;
}


 my_file >> numbers;
 }
cout <<smallest <<endl;
cout <<largest;
}

Open in new window

0
 
farzanjCommented:
Could you also post your nums.txt file?
0
 
farzanjCommented:
Try it now.
#include <iostream>
#include <fstream>
using namespace std;

void read(ifstream &my_file);

int main(void)
{
	ifstream in_stream;
	in_stream.open("nums.txt");

	if(in_stream.fail())
	{
		cout <<"unable to read file";
	}

	read(in_stream);
}

void read (ifstream &my_file)
{
	int largest, smallest, numbers;

	my_file >> numbers;

	//initialize the value of largest and smallest
	largest = smallest = numbers;
	while(! my_file.eof())
	{
		if(numbers > largest)
		{
			largest = numbers;
		}
		else if(numbers <smallest)
		{
			smallest = numbers;
		}
		my_file >> numbers;
	}
	cout << "Smallest number is : " << smallest << endl;
	cout << "Largest number is  : " << largest  << endl;
	return;
}

Open in new window

0
 
farzanjCommented:
If you need to read one number at a time, you don't need getline.  If you want to read strings to the end of line and then convert them to numbers, then you can use getline.
0
 
HellmarkLinux Systems AdministratorCommented:
This sounds suspiciously like homework, so I'll not cite code. However, I will assist with theory.

Way I would do it, is have two variables, one for high and one for low. As it reads each number, compare against the existing value of the variables. If it is greater than the previous high, it becomes the new high, and vice versa for the low.
0
 
sarabandeConnect With a Mentor Commented:
don't use

while(! my_file.eof())

Open in new window


that would give an infinite loop in case there is a non-number in the file. then the eof condition never was reached.

use

while (my_file >> number)
{

Open in new window


and omit all other 'my_file >> number' statements.

the operator>> returns NULL (== false) in case of a fail or eof. so the while condition would be false in either case.

Sara
0
 
businessesatozAuthor Commented:
thanks for the help. :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.