?
Solved

ifstream largest and smallest number

Posted on 2011-10-30
14
Medium Priority
?
415 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:businessesatoz
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 2

Expert Comment

by:adamnl
ID: 37052471
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
 

Author Comment

by:businessesatoz
ID: 37052487
in order to read something line by  line then i have to use the getline right?
0
 
LVL 2

Expert Comment

by:adamnl
ID: 37052494
Yes
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:businessesatoz
ID: 37052522
how would i do that? thanks..
0
 

Author Comment

by:businessesatoz
ID: 37052564
thanks i got it .. i did not use the getline though.
0
 
LVL 2

Expert Comment

by:adamnl
ID: 37052575
You're welcome :)
0
 

Author Comment

by:businessesatoz
ID: 37052647
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
 
LVL 31

Expert Comment

by:farzanj
ID: 37052679
Could you also post your nums.txt file?
0
 
LVL 31

Expert Comment

by:farzanj
ID: 37052701
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
 
LVL 31

Accepted Solution

by:
farzanj earned 1600 total points
ID: 37052735
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
 
LVL 31

Expert Comment

by:farzanj
ID: 37052737
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
 
LVL 7

Expert Comment

by:Hellmark
ID: 37053471
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
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 400 total points
ID: 37075485
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
 

Author Closing Comment

by:businessesatoz
ID: 37089663
thanks for the help. :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question